XamlReader 无法解析具有 DataType 属性 的 DataTemplate

XamlReader fails to parse DataTemplate with DataType property

我正在尝试使用 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 调用抛出异常:

The property 'DataType' was not found in type 'DataTemplate'. [Line: 8 Position: 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>());

列表正确呈现。

The property 'DataType' was not found in type 'DataTemplate'. [Line: 8 Position: 17]

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

If I remove the property and replace the binding as below it doesn't crash but doesn't render either:

如果您在 C++/WinRT 中使用 {Binding} 扩展,您应该将 BindableAttribute 属性添加到您想要使用 {Binding} 标记扩展的任何运行时 class。关于这方面的更多细节,你可以参考这篇document。在这种情况下,您需要在 .idl 文件中添加 [bindable] 才能使用绑定。例如:

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>());