UWP - C++/CX 中的 ListView 绑定
UWP - ListView binding in C++/CX
这个题目在c#中似乎很容易,但我需要在c++/cx中处理它。我想用 'CItem' 的 list/vector 填充列表视图。所以我试过了:
主页XAML:
<ListView x:Name="mainListView" HorizontalAlignment="Stretch" VerticalAlignment="Center" Margin="20,0,20,6" >
<ListView.ItemTemplate>
<DataTemplate>
<Grid Height="40" Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border Background ="Gray" Width="50" Height="50" >
<Image Source="{Binding Image}" Stretch="UniformToFill" ></Image>
</Border>
<StackPanel>
<TextBlock Text="{Binding Hostname}" TextWrapping="NoWrap"/>
</StackPanel>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
主页Class:
Windows::UI::Xaml::Interop::IBindableVector^ test;
CItem^ item = ref new
CItem("name", "Assets/ic_help");
test->Append(item);
mainListView->ItemsSource = test;
CItem.h:
#pragma once
#include "pch.h"
ref class CItem sealed
{
private:
Platform::String^ name;
Platform::String^ image;
public:
CItem(Platform::String^ name, Platform::String^ image) :
name {name}, image {image} {}
property Platform::String^ Name
{
Platform::String^ get() { return this->name;}
}
property Platform::String^ Image
{
Platform::String^ get() { return this->image;}
}
};
这段代码的问题是我在 IBindableVector^ 上得到了一个空指针,因为我不知道如何初始化它。我试过使用普通的字符串列表/向量,但它在 C++ 中不起作用。那么,您是否有适合我的模板或提示我如何轻松解决此问题。我只是想拥有一个自定义项目的容器,每次我获得新数据时,我都想再次设置列表视图的 ItemSource。我想使用 ->ItemSource 进行绑定而不是 viewcontroller 模式的原因是我想稍后将列表视图传递给生成数据并更新列表的线程(就像我在 C# 中所做的那样)它正在工作)。
My Problem with this code is that I get a nullpointer on the IBindableVector^ because I do not know how to initialize it.
IBindableVector
represents a writeable vector collection of objects that is bindable. You need to initialize the IBindableVector
as a vector collection,例如初始化IBindableVector
如下,你会发现绑定有效:
Windows::UI::Xaml::Interop::IBindableVector^ test;
test=ref new Platform::Collections::Vector<CItem^>();
更多详情请参考XamlBind official sample。
在您的初始实施中,您需要将 BindableAttribute
添加到您的 CItem
class:
[Windows::UI::Xaml::Data::Bindable]
public ref class CItem sealed
如果你这样做,你的矢量类型就不必是
Windows::UI::Xaml::Interop::IBindableVector^ test;
您可以简单地使用
Platform::Collections::Vector<CItem^>^ test;
至少这对我有用!
这个题目在c#中似乎很容易,但我需要在c++/cx中处理它。我想用 'CItem' 的 list/vector 填充列表视图。所以我试过了:
主页XAML:
<ListView x:Name="mainListView" HorizontalAlignment="Stretch" VerticalAlignment="Center" Margin="20,0,20,6" >
<ListView.ItemTemplate>
<DataTemplate>
<Grid Height="40" Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border Background ="Gray" Width="50" Height="50" >
<Image Source="{Binding Image}" Stretch="UniformToFill" ></Image>
</Border>
<StackPanel>
<TextBlock Text="{Binding Hostname}" TextWrapping="NoWrap"/>
</StackPanel>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
主页Class:
Windows::UI::Xaml::Interop::IBindableVector^ test;
CItem^ item = ref new
CItem("name", "Assets/ic_help");
test->Append(item);
mainListView->ItemsSource = test;
CItem.h:
#pragma once
#include "pch.h"
ref class CItem sealed
{
private:
Platform::String^ name;
Platform::String^ image;
public:
CItem(Platform::String^ name, Platform::String^ image) :
name {name}, image {image} {}
property Platform::String^ Name
{
Platform::String^ get() { return this->name;}
}
property Platform::String^ Image
{
Platform::String^ get() { return this->image;}
}
};
这段代码的问题是我在 IBindableVector^ 上得到了一个空指针,因为我不知道如何初始化它。我试过使用普通的字符串列表/向量,但它在 C++ 中不起作用。那么,您是否有适合我的模板或提示我如何轻松解决此问题。我只是想拥有一个自定义项目的容器,每次我获得新数据时,我都想再次设置列表视图的 ItemSource。我想使用 ->ItemSource 进行绑定而不是 viewcontroller 模式的原因是我想稍后将列表视图传递给生成数据并更新列表的线程(就像我在 C# 中所做的那样)它正在工作)。
My Problem with this code is that I get a nullpointer on the IBindableVector^ because I do not know how to initialize it.
IBindableVector
represents a writeable vector collection of objects that is bindable. You need to initialize the IBindableVector
as a vector collection,例如初始化IBindableVector
如下,你会发现绑定有效:
Windows::UI::Xaml::Interop::IBindableVector^ test;
test=ref new Platform::Collections::Vector<CItem^>();
更多详情请参考XamlBind official sample。
在您的初始实施中,您需要将 BindableAttribute
添加到您的 CItem
class:
[Windows::UI::Xaml::Data::Bindable]
public ref class CItem sealed
如果你这样做,你的矢量类型就不必是
Windows::UI::Xaml::Interop::IBindableVector^ test;
您可以简单地使用
Platform::Collections::Vector<CItem^>^ test;
至少这对我有用!