我正在尝试使用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>());
列表正确显示。
答案 0 :(得分:2)
在类型'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>());