C# Xamarin ContentView构造函数
我正在创建ContentView的一个子类,C# Xamarin ContentView构造函数,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我正在创建ContentView的一个子类,UserPostView,用作ListView中的数据模板。我通过创建UserPostView的新实例,动态地将项目添加到ListView的ItemSource中 <Image x:Name="PictureView" Source="Logo" BackgroundColor="#ddd" /> <Label x:Name="NameView" Text="Name" TextColor="#444" FontAttributes=
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" />
但是当我在ListView中使用它时,它不会显示传递的数据,而是显示原始数据。(Text=“Name”…)
救命啊!如何让它们显示通过构造函数传递的数据 如果您能提供ListView的xaml和DataTemplate,我们可以为您提供更好的答案 但是,我猜ListView的
项资源应设置为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 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>
public TimelinePage()
{
初始化组件();
ImageSource pic=ImageSource.FromUri(新Uri(“https://tse1.mm.bing.net/th?id=OIP.acchQ02P8HmrmwbjCTwG5AHaHa&pid=Api"));
_listView.ItemsSource=新的ObservableCollection
{
新用户postViewModel{Picture=ImageSource.FromResource(“徽标”),PostOwner=“Kevin Wang”,PostOwnerLocation=“SHDR”},
新用户postViewModel{Picture=pic,PostOwner=“Lily”,PostOwnerLocation=“SHDR”},
新用户postViewModel{Picture=pic,PostOwner=“Zhang Shuo”,PostOwnerLocation=“FRDL”},
};
}
//注意:在页面中,ListView数据模板
非常感谢您花时间帮助我!但这些代码都是我的课程。ListView是Xamarin.Forms组件。我想找到一种不使用绑定和东西的更简单的方法。;)但我不确定这是否可能,现在系统似乎忽略了从构造函数传递的值。我指的是未提供的TimelinePage
的xaml代码,应该显示如何在该页面上设置ListView。如果你想让你的UserPostView
保持原样,把你的ListView
切换到StackLayout
,然后把每个UserPostView
作为一个孩子单独添加进去。啊哈,好主意!也许ListView不适合这种情况?顺便说一句,这是我的代码:
是的,您的默认数据模板不';没有传递任何构造函数参数,其中ListView的绑定应该在ViewModel中发挥作用,以及为什么只显示默认值。此外,只要您有一定数量的视图来显示don';不需要回收。它在UserPostView.xaml中显示原始数据,NameView
显示Name
,而不是Lily
或Kevin Wang
。
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>