在OnNavigatedTo中更改模型后,Prism Xamarin Forms View不会更新 [英] Prism Xamarin Forms View is not updated after changing the model in OnNavigatedTo

查看:159
本文介绍了在OnNavigatedTo中更改模型后,Prism Xamarin Forms View不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Prism 6.3.0

Prism 6.3.0

我导航到一个View,并在OnNavigatedTo方法中设置了Model的某些属性.问题在于,设置模型的属性后,视图不会更新.

I'm navigating to a View and I'm settings some properties of the Model in the OnNavigatedTo method. The problem is that the view is not updated after setting the properties of the model.

我的模特看起来像这样

public class Person
{
    public string Name { get; set; }
    public string Email { get; set; }
}

在我的ViewModel中

In my ViewModel

public Person Model
{
    get => _model;
    set => SetProperty(ref _model, value);
}

OnNavigatedTo方法是

The OnNavigatedTo method is

public void OnNavigatedTo(NavigationParameters parameters)
{
    var personModel = (Persom)parameters["Model"];

    _model.Email = personModel.Email;
}

在XAML中

<Entry Text="{Binding Person.Email, Mode=TwoWay}">
</Entry>

我也尝试了OnNavigatingTo方法,但没有得到预期的结果.那么,当我导航到视图时,该怎么做才能用新数据更新视图?

I also tried the OnNavigatingTo method but I didn't have the expected results. So what should I do in order to update the view with the new data when I'm navigating to the view?

推荐答案

使用设置器而不是私有变量来设置模型.

Set the model using the setter instead of the private variable.

public void OnNavigatedTo(NavigationParameters parameters)
{
    var personModel = (Person)parameters["Model"];

    Model.Email = personModel.Email;
}

此外,您的XAML也应从Person.Email更改为Model.Email

Also, your XAML should be change from Person.Email to Model.Email

===========编辑===========

=========== EDITED ============

您仅设置了不调用SetProperty的模型电子邮件,因此视图将不会使用新的电子邮件集进行更新.为了更新视图,您需要将Person模型设置为以下内容:

You only set the email of model which do not call SetProperty, so the views will not get updated with the new email set. In order to have the views update, you will need to set the Person model to the following:

public class Person : BindableBase
{
    public string Name { get; set; }

    private string _email;
    public string Email
    {
        get { return _email; }
        set { SetProperty(ref _email, value); }
    }
}

或者您可以设置Person对象,而不仅仅是在OnNavigatedTo中设置电子邮件.

Or instead of just setting the email in OnNavigatedTo, you can set the Person object.

public void OnNavigatedTo(NavigationParameters parameters)
{
    var personModel = (Person)parameters["Model"];

    Model = personModel;
}

这篇关于在OnNavigatedTo中更改模型后,Prism Xamarin Forms View不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆