gpt4 book ai didi

c# - Xamarin ContentView 构造函数

转载 作者:太空宇宙 更新时间:2023-11-03 12:15:32 25 4
gpt4 key购买 nike

我正在创建 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! "),
};
}

救命啊!我怎样才能让他们显示通过构造函数传递的数据?

最佳答案

如果您可以提供您的 ListView 的 xaml 和 DataTemplate,我们可以为您提供更好的答案。

但是,我的猜测是您的 ListView 的 ItemsSource 应该设置为 ViewModel : INotifyPropertyChanged 的列表,其中包含您的属性而不是您的 ContentView,并且您的 ContentView 应该是绑定(bind)到该 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" />
...

示例 View 模型:

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>

关于c# - Xamarin ContentView 构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49940043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com