Xamarin ContentView 构造函数
Posted
技术标签:
【中文标题】Xamarin ContentView 构造函数【英文标题】:Xamarin ContentView Constructors 【发布时间】:2018-04-20 10:54:08 【问题描述】:我正在创建 ContentView 的子类UserPostView
以用作 ListView 中的数据模板。我通过创建UserPostView
的新实例将项目动态添加到ListView 的ItemSource。
<Image x:Name="PictureView" Source="Logo" BackgroundColor="#ddd" />
<Label x:Name="NameView" Text="Name" TextColor="#444" FontAttributes="Bold" Grid.Row="0" Grid.Column="1" Margin="10" />
<Label x:Name="LocationNameView" Text="Location" TextColor="#666" Grid.Row="0" Grid.Column="2" YAlign="Center" />
<Label x:Name="DeptNameView" Text="Dept" TextColor="#666" Grid.Row="0" Grid.Column="3" YAlign="Center" />
在代码中:
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace CMCityClient.Views
public partial class UserPostView : ContentView
public ImageSource Picture
get return PictureView.Source;
set PictureView.Source = value;
public string PostOwner
get return NameView.Text;
set NameView.Text = value;
public string PostOwnerLocation
get return LocationNameView.Text;
set LocationNameView.Text = value;
// ...
// ...
public UserPostView(ImageSource picture, string owner, string loc, string dept, string text)
InitializeComponent();
Picture = picture;
PostOwner = owner;
PostOwnerLocation = loc;
PostOwnerDept = dept;
PostText = text;
public UserPostView(ImageSource picture, string owner, string loc, string dept, string text, ImageSource image)
InitializeComponent();
Picture = picture;
PostOwner = owner;
PostOwnerLocation = loc;
PostOwnerDept = dept;
PostText = text;
PostImage = image;
但是当我在我的 ListView 中使用它时,它不会显示传递的数据,而是显示原始数据。 (文本=“名称”...)
public TimelinePage()
InitializeComponent();
ImageSource pic = ImageSource.FromUri(new Uri("https://tse1.mm.bing.net/th?id=OIP.acchQ02P8HmrmwbjCTwG5AHaHa&pid=Api"));
timeline.ItemsSource = new UserPostView[]
new UserPostView(ImageSource.FromResource("Logo"), "Kevin Wang", "SHDR", "AOP", "hey"),
new UserPostView(pic, "Lily", "SHDR", "ENT", "good"),
new UserPostView(pic, "Zhang Shuo", "FRDL", "ENT", "Salut! "),
;
帮助!我怎样才能让他们显示通过构造函数传递的数据?
【问题讨论】:
它在 UserPostView.xaml 中显示原始数据,NameView
显示Name
而不是Lily
或Kevin Wang
。
【参考方案1】:
如果您可以提供您的 ListView 的 xaml 和 DataTemplate,我们可以为您提供更好的答案。
但是,我的猜测是您的 ListView 的 ItemsSource
应该设置为 ViewModel : INotifyPropertyChanged
的列表,其中包含您的属性而不是 ContentView,并且您的 ContentView 应该绑定到该 ViewModel。例如:
public partial class UserPostView : ContentView
public UserPostView()
InitializeComponent();
//NOTE: in UserPostView.xaml
<Image Source="Binding Picture" BackgroundColor="#ddd" />
<Label Text="Binding PostOwner" TextColor="#444" FontAttributes="Bold" Margin="10" />
<Label Text="Binding PostOwnerLocation" TextColor="#666" YAlign="Center" />
...
示例视图模型:
public class UserPostViewModel : INotifyPropertyChanged
ImageSource _picture;
public ImageSource Picture
get return _picture;
set
_picture = value;
OnPropertyChanged(nameof(Picture));
string _postOwner;
public string PostOwner
get return _postOwner;
set
_postOwner = value;
OnPropertyChanged(nameof(PostOwner));
string _postOwnerLocation;
public string PostOwnerLocation
get return _postOwnerLocation;
set
_postOwnerLocation = value;
OnPropertyChanged(nameof(PostOwnerLocation));
....
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
在您的页面中:
public TimelinePage()
InitializeComponent();
ImageSource pic = ImageSource.FromUri(new Uri("https://tse1.mm.bing.net/th?id=OIP.acchQ02P8HmrmwbjCTwG5AHaHa&pid=Api"));
_listView.ItemsSource = new ObservableCollection<UserPostViewModel>
new UserPostViewModel Picture = ImageSource.FromResource("Logo"), PostOwner="Kevin Wang", PostOwnerLocation="SHDR",
new UserPostViewModel Picture = pic, PostOwner="Lily", PostOwnerLocation="SHDR",
new UserPostViewModel Picture = pic, PostOwner="Zhang Shuo", PostOwnerLocation="FRDL",
;
//NOTE: In your Pages ListView DataTemplate
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<local:UserPostView/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
【讨论】:
非常感谢您花时间帮助我!但是这些代码都是我的课程。 ListView 是 Xamarin.Forms 组件。我想找到一种不使用绑定和东西的更简单的方法。 ;) 但我不确定这是否可能,目前系统似乎忽略了我从构造函数传递的值。 我指的是您的TimelinePage
的 xaml 代码,该代码未提供,应该显示您如何在该页面上设置 ListView。如果您想保持 UserPostView
类的原样,请将您的 ListView
切换为 StackLayout
并单独将每个 UserPostView
添加为子类。
啊哈,好主意!也许 ListView 不适合这样的事情?
顺便说一下这是我的代码:<ListView x:Name="timeline" SeparatorColor="#dadada" HasUnevenRows="true"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <views:UserPostView /> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView>
是的,您的默认 DataTemplate 没有传递任何构造函数参数,对于 ListView 来说,绑定应该与 ViewModel 一起发挥作用,以及为什么只显示您的默认值。此外,只要您需要显示不需要回收的少量视图,StackLayout 就可以了。以上是关于Xamarin ContentView 构造函数的主要内容,如果未能解决你的问题,请参考以下文章
如何在具有棱镜的Xamarin表单中为contentview创建单独的视图模型?