【问题标题】:XamlReader fails to parse DataTemplate with DataType propertyXamlReader 无法使用 DataType 属性分析 DataTemplate
【发布时间】:2021-01-08 04:19:56
【问题描述】:

我正在尝试使用 cpp/winrt 为 ListView 动态创建 DataTemplate

auto template_src = R"(
    <DataTemplate 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:MyNamespace"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        x:DataType="local:MyListItem"
    >
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{x:Bind MyProperty}" HorizontalAlignment="Left"/>
        </StackPanel>
    </DataTemplate>
)";
auto tmpl = winrt::Windows::UI::Xaml::Markup::XamlReader::Load(winrt::to_hstring(template_src)).as<winrt::Windows::UI::Xaml::DataTemplate>();

Load 调用抛出异常:

在类型“DataTemplate”中找不到属性“DataType”。 [行:8 位置:17]

MyListItem 类型是自定义的 winrt 类型。使用 x:Bind 时需要 DataType 属性。 如果我删除该属性并按如下方式替换绑定,它不会崩溃但也不会呈现:

auto template_src = R"(
    <DataTemplate 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:MyNamespace"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
    >
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding MyProperty}" HorizontalAlignment="Left"/>
        </StackPanel>
    </DataTemplate>
)";
auto tmpl = winrt::Windows::UI::Xaml::Markup::XamlReader::Load(winrt::to_hstring(template_src)).as<winrt::Windows::UI::Xaml::DataTemplate>();

如果我在 Xaml 中声明模板:

<Page.Resources>
    <DataTemplate x:Key="ListTemplate" x:DataType="local:MyListItem">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{x:Bind MyProperty, Mode=OneWay}" HorizontalAlignment="Left"/>
        </StackPanel>
    </DataTemplate>
</Page.Resources>

并加载它

Resources().Lookup(winrt::box_value(L"ListTemplate")).as<winrt::Windows::UI::Xaml::DataTemplate>());

列表正确呈现。

【问题讨论】:

    标签: uwp winrt-xaml uwp-xaml c++-winrt


    【解决方案1】:

    在类型“DataTemplate”中找不到属性“DataType”。 [线:8 位置:17]

    基于document,它提到我们不能在代码中创建 {x:Bind} 绑定,因此在关于 template_src 的代码中使用 {x:Bind} 会导致异常。您可以使用 {Binding} 替换 {x:Bind}。

    如果我删除该属性并替换下面的绑定,它不会 崩溃但也不渲染:

    如果您在 C++/WinRT 中使用 {Binding} 扩展,则应将 BindableAttribute 特性添加到要使用 {Binding} 标记扩展的任何运行时类。有关此的更多详细信息,您可以参考此document。在这种情况下,您需要在 .idl 文件中添加 [bindable] 才能使用 Binding。例如:

    Model.idl:

    [bindable]
    runtimeclass BookSku : Windows.UI.Xaml.Data.INotifyPropertyChanged
    {
        String Title;
    }
    

    要渲染列表,请检查以下代码:

    Page.cpp

    auto template_src = R"(
        <DataTemplate 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="using:MyNamespace"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        >
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Title}" HorizontalAlignment="Left"/>
            </StackPanel>
        </DataTemplate>
    )";
    hstring str = winrt::to_hstring(template_src);
    auto tmpl = winrt::Windows::UI::Xaml::Markup::XamlReader::Load(str);
    
    myListView().ItemsSource(MyListItem());
    myListView().ItemTemplate(tmpl.try_as<DataTemplate>());
    

    【讨论】:

      猜你喜欢
      • 2016-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      • 2012-04-17
      • 2011-11-02
      相关资源
      最近更新 更多