Windows Phone 8.1 RT视图未更新(MVVM)

时间:2014-12-06 21:12:46

标签: xaml mvvm windows-phone-8.1

我正在为用户设计个人资料页面,以便他们编辑个人信息。我正在使用PersonViewModel(包含当前签名的当前人员)来显示有关用户的当前信息。用于编辑用户信息的字段绑定到验证模型。按下'执行更改'按钮后,我得到服务器的响应(HTTPStatusCode Ok +更改的用户对象),我根据更改改变现有对象的字段。然后我使用setter注入来更新我的PersonViewModel ...在调试时,我可以看到我的对象都是最新的,但我的视图仍然显示旧信息......我做错了什么?`

这是我按下按钮执行更改时执行的代码:

    private async void ChangeInfoButton(object sender, RoutedEventArgs e)
    {
        User user;
        List<ErrorInfo> errors;
        if (_profileInformationValidationModel.TryGetUser(out user, out errors))
        {
            var response = await Session.Instance.DataProvider.UpdaterUserInfo(user);

            if (response.IsSuccess)
            {
                /*SignedInUserInfo = AlteredUserInfo*/
                Session.Instance.User.Information = user.Information;  
                _personViewModel.SetPerson(user.Information);
                var d1 = new MessageDialog("Uw gegevens werden succesvol gewijzigd.");
                d1.ShowAsync();

                AnnulInfoButton(sender, e);
            }

` 这是PersonViewModel:

public class PersonViewModel
{
    private Person _person;

    public void SetPerson(Person p)
    {
        _person = p;
    }

    public PersonViewModel(Person person)
    {
        _person = person;
    }

    public string Street
    {
        get { return _person.Street; }
    }

    public string HouseNumber
    {
        get { return _person.HouseNumber; }
    }

    public string Bus
    {
        get { return _person.Bus; }
    }

    public string Email
    {
        get { return _person.Email; }
    }

1 个答案:

答案 0 :(得分:0)

您的视图模型应实现INotifyPropertyChanged界面。

使用像MVVM Light这样的框架来完成大部分工作。

您可以使用NuGet将其添加到项目中。

这是您的模型和视图模型的外观:

public class Person
{

    public string Street { get; set; }

    public string HouseNumber { get; set; }

    public string Bus { get; set; }

    public string Email { get; set; }
}

public class PersonViewModel : System.ComponentModel.INotifyPropertyChanged
{
    private Person _person;

    public void SetPerson(Person person)
    {
        _person = person;
        Street = person.Street;
        HouseNumber = person.HouseNumber;
        Bus = person.Bus;
        Email = person.Email;
    }

    public PersonViewModel(Person person)
    {
        SetPerson(person);
    }

    #region Street (INotifyPropertyChanged Property)
    private string _street;

    public string Street
    {
        get { return _street; }
        set
        {
            if (_street != value)
            {
                _street = value;
                RaisePropertyChanged("Street");
            }
        }
    }
    #endregion


    #region HouseNumber (INotifyPropertyChanged Property)
    private string _houseNumber;

    public string HouseNumber
    {
        get { return _houseNumber; }
        set
        {
            if (_houseNumber != value)
            {
                _houseNumber = value;
                RaisePropertyChanged("HouseNumber");
            }
        }
    }
    #endregion

    #region Bus (INotifyPropertyChanged Property)
    private string _bus;

    public string Bus
    {
        get { return _bus; }
        set
        {
            if (_bus != value)
            {
                _bus = value;
                RaisePropertyChanged("Bus");
            }
        }
    }
    #endregion


    #region Email (INotifyPropertyChanged Property)
    private string _email;

    public string Email
    {
        get { return _email; }
        set
        {
            if (_email != value)
            {
                _email = value;
                RaisePropertyChanged("Email");
            }
        }
    }
    #endregion


    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string p)
    {
        var propertyChanged = PropertyChanged;
        if (propertyChanged != null)
        {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(p));
        }
    }


}