我想单击列表视图的视单元中的包和图像来更改标签文本和图像。我正在将命令用于click事件,并在扩展InotifyPropertyChange的ViewModelClass中实现了该命令。单击图像或标签后,我的视图没有更新。请帮助。
公共类BookPageViewModel:INotifyPropertyChanged, { #region字段 私有ObservableCollection feeds = new ObservableCollection();
public ObservableCollection<FeedsModel> Feeds {
get
{
return feeds;
}
set {
if(value != feeds)
{
feeds = value;
OnPropertyChanged("Feeds");
}
}
}
private ICommand onLikePostClicked;
public ICommand OnLikePostClicked
{
get { return onLikePostClicked; }
}
public DayBookPageViewModel(Page page, SfListView sfListView)
{
GetAllFeeds(Settings.UserIdSettings);
this.page = page;
this.sfListView = sfListView;
#region Commands execution
onLikePostClicked = new Command((e) =>
{
var item = (e as FeedsModel);
if (!string.IsNullOrEmpty(item.Love))
{
bool like_status = false;
string[] strarray = item.Love.Split(',');
for (int i = 0; i < strarray.Length; i++)
{
if (strarray[i] == Settings.UserIdSettings)
{
like_status = true;
break;
}
else
{
like_status = false;
}
}
if(like_status)
{
// unlike
if (!NetworkConnectionClass.GetInstance().IsConnectedToInternet())
ShowAlerts.GetInstance().ACRLibAlert(Strings.InternetAlert, Strings.Ok);
else
{
if (item != null)
{
Device.BeginInvokeOnMainThread(delegate
{
item.LikeImageSource = ImageSource.FromFile("like_hover");
item.LikeLabelColor = (Color)Application.Current.Resources["defaultLabels_color"];
OnPropertyChanged("Feeds");
});
DislikeApi dislikePostApi = new DislikeApi(this, this);
dislikePostApi.DisLikePost(Settings.UserIdSettings, item.postId.ToString());
}
}
}
else
{
// like
if (!NetworkConnectionClass.GetInstance().IsConnectedToInternet())
ShowAlerts.GetInstance().ACRLibAlert(Strings.InternetAlert, Strings.Ok);
else
{
if (item != null)
{
//Todo;
//********* UI not updating : need to fixed ***********
Device.BeginInvokeOnMainThread(delegate
{
item.LikeImageSource = ImageSource.FromFile("like");
item.LikeLabelColor = (Color)Application.Current.Resources["appThemeColor"];
OnPropertyChanged("Feeds");
});
LikePostApi likePostApi = new LikePostApi(this, this);
likePostApi.LikePost(Settings.UserIdSettings, item.postId.ToString());
}
}
}
}
else
{
if (!NetworkConnectionClass.GetInstance().IsConnectedToInternet())
ShowAlerts.GetInstance().ACRLibAlert(Strings.InternetAlert, Strings.Ok);
else
{
if (item != null)
{
//Todo;
//********* UI not updating : need to fixed ***********
Device.BeginInvokeOnMainThread( delegate
{ if (!item.Love.Contains(Settings.UserIdSettings))
{
likeimageSource = ImageSource.FromFile("like");
item.LikeImageSource = ImageSource.FromFile("like");
item.LikeLabelColor = (Color)Application.Current.Resources["appThemeColor"];
OnPropertyChanged("Feeds");
});
#endregion
}
}
// This is inside the viewcell of a listview
<Label x:Name="likeLabel" TextColor="{Binding LikeLabelColor, Mode=TwoWay}" Text="Like " LineBreakMode="CharacterWrap" FontSize="12" Grid.Row="0" Grid.Column="1">
<Label.Margin>
<OnPlatform x:TypeArguments="Thickness">
<On Platform="iOS" Value="0,15.5,0,0" />
<On Platform="Android" Value="0,14,0,0" />
</OnPlatform>
</Label.Margin>
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Path=BindingContext.OnLikePostClicked, Source={x:Reference customListView}}" CommandParameter ="{Binding .}" NumberOfTapsRequired="1" />
</Label.GestureRecognizers>
</Label>
答案 0 :(得分:1)
您需要在INotifyPropertyChanged
对象上实现FeedModel
。这就是发生更改的地方,因此您希望在其中发生任何事情时通知UI。
如果您想节省很多样板代码,请查看PropertyChanged.Fody,它会在编译时为您生成INotifyPropertyChanged
代码。