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

问题描述

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

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类型。使用DataType时需要x:Bind属性。 如果我删除属性并按如下所示替换绑定,则它不会崩溃,但也不会呈现:

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

列表正确显示

解决方法

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

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

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

如果在C ++ / WinRT中使用{Binding}扩展名,则应将BindableAttribute属性添加到要与{Binding}标记扩展名一起使用的任何运行时类中。有关此的更多详细信息,您可以参考此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>());

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...