【问题标题】:Prism: View is not updated after setting value in ViewModel?棱镜:在 ViewModel 中设置值后视图没有更新?
【发布时间】:2019-12-02 06:00:09
【问题描述】:

我无法从 ViewModel 将值绑定到 View,我可以看到值正在设置 (ServiceList)

这里是 Xaml

XAML:

<Label
                    Grid.Row="0"
                    FontFamily="{StaticResource BoldFont}"
                    FontSize="{StaticResource NormalFontSize}"
                    HorizontalOptions="StartAndExpand"
                    Text="{Binding Heading, Mode=TwoWay}"
                    TextColor="Black"
                    VerticalOptions="CenterAndExpand" />

PageViewModel:

    private string _Heading;
        public string Heading
        {
            get { return _Heading; }
            set { _Heading = value; SetProperty(ref _Heading, value); }
        }

async void LoadData(Service service)
        {
            Dictionary<string, string> data = new Dictionary<string, string>();
            if (App.Current.Properties.ContainsKey("user_id"))
                data.Add("user_id", App.Current.Properties["user_id"].ToString());
            data.Add("subcategory", service.id.ToString());
            data.Add("city", "ABC");
            UserResponse = await HomeService.GetServices(data);
            if (UserResponse.status == 200)
            {
                Heading = "529 Interior Decorators in Las Vegas";
            }
        }

【问题讨论】:

    标签: xamarin mvvm xamarin.forms prism


    【解决方案1】:

    这是因为 SetProperty 只有在值实际发生变化时才会引发 PropertyChanged。通过预先设置它,您可以防止这种情况发生,并且视图无法知道它应该更新。

    你想写:

    public string Heading
    {
        get { return _Heading; }
        set { SetProperty(ref _Heading, value); }
    }
    

    注意缺少的_Heading = value;

    【讨论】:

      猜你喜欢
      • 2021-11-01
      • 1970-01-01
      • 2013-08-18
      • 2018-12-13
      • 1970-01-01
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多