【问题标题】:Xamarin Froms : Label text and Image not updating on UI on click of an Label inside a viewcell of a ListviewXamarin Forms:单击Listview的视图单元格内的标签时,标签文本和图像未在UI上更新
【发布时间】:2019-01-14 10:54:35
【问题描述】:

我想通过单击 listview 的 viewcell 中的包和图像来更改标签文本和图像。我正在使用单击事件的命令,并在我的 ViewModelClass 中实现了它,它扩展了 InotifyPropertyChange。单击图像或标签时,我的视图未更新。请帮助。

公共类 BookPageViewModel : INotifyPropertyChanged, { #region 字段 private 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>

【问题讨论】:

    标签: listview mvvm xamarin.forms


    【解决方案1】:

    您需要在您的FeedModel 对象上实现INotifyPropertyChanged。这就是发生变化的地方,因此您希望在其中发生任何事情时通知 UI。

    如果您想为自己节省大量样板代码,请查看PropertyChanged.Fody,它会在编译时为您生成INotifyPropertyChanged 代码。

    【讨论】:

    • 嗨!感谢您的快速回复 。我已经在我的 ViewModel 类上实现了 INotifyPropertyChanged,那么为什么还要在我的模型类上实现它呢?
    • 因为我提到的原因:您正在更改模型中的值,而不是视图模型中的值。这有点难以解释,但是您正在更改模型字段中的值。如果您希望该更改显示在您的 UI 中,请在您的模型上实现 INotifyPropertyChanged。希望这是有道理的。
    • 我明白你的意思,它解决了我的问题。非常感谢:)
    • @SeaasiaCreativeCrew 太棒了!请不要忘记接受作为答案!
    猜你喜欢
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    • 2020-08-17
    相关资源
    最近更新 更多