棱镜:在ViewModel中设置值后视图是否未更新?

时间:2019-07-24 06:51:14

标签: xamarin mvvm xamarin.forms prism

我无法将值从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";
            }
        }

1 个答案:

答案 0 :(得分:3)

这是因为SetProperty仅在值实际更改时才提高PropertyChanged。通过预先设置,可以防止这种情况,并且视图无法知道应该更新。

您要写:

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

请注意缺少的_Heading = value;