Prism:在 ViewModel 中设置值后视图未更新?
Prism: View is not updated after setting value in ViewModel?
我无法将值绑定到 ViewModel 中的视图,我可以看到值正在设置 (ServiceList)
这是Xaml
XAML:
<Label
Grid.Row="0"
FontFamily="{StaticResource BoldFont}"
FontSize="{StaticResource NormalFontSize}"
HorizontalOptions="StartAndExpand"
Text="{Binding Heading, Mode=TwoWay}"
TextColor="Black"
VerticalOptions="CenterAndExpand" />
页面视图模型:
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";
}
}
这是因为 SetProperty
只有在值实际发生变化时才会引发 PropertyChanged
。通过预先设置它,您可以防止这种情况发生,并且视图不知道它应该更新。
您想写:
public string Heading
{
get { return _Heading; }
set { SetProperty(ref _Heading, value); }
}
注意缺失的_Heading = value;
我无法将值绑定到 ViewModel 中的视图,我可以看到值正在设置 (ServiceList)
这是Xaml
XAML:
<Label
Grid.Row="0"
FontFamily="{StaticResource BoldFont}"
FontSize="{StaticResource NormalFontSize}"
HorizontalOptions="StartAndExpand"
Text="{Binding Heading, Mode=TwoWay}"
TextColor="Black"
VerticalOptions="CenterAndExpand" />
页面视图模型:
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";
}
}
这是因为 SetProperty
只有在值实际发生变化时才会引发 PropertyChanged
。通过预先设置它,您可以防止这种情况发生,并且视图不知道它应该更新。
您想写:
public string Heading
{
get { return _Heading; }
set { SetProperty(ref _Heading, value); }
}
注意缺失的_Heading = value;