0

我想问你,你怎么看,从第二页到第一页的最佳方法是什么?我使用这样的东西。(MVVM)

第二页:

public partial class AddProfilePageView : PhoneApplicationPage
{
    public AddProfilePageView()
    {
        InitializeComponent();
        DataContext = new AddProfileViewModel();
    }

    public AddProfileViewModel ViewModel { get { return DataContext as AddProfileViewModel; } }}

第一页:

public partial class ProfilesPageView : PhoneApplicationPage
{

    public ProfilesPageView()
    {
        InitializeComponent();
        DataContext = new ProfilesViewModel();
    }

    public ProfilesViewModel ViewModel
    {
        get { return DataContext as ProfilesViewModel; }
    }}

AddProfileViewModel() 类具有绑定到 xaml 中的控件的属性。我需要从这个页面获取数据到第一页 ProfilesPageView。

我的解决方案是:

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
    {
        var content = e.Content as ProfilesPageView;
        if (content != null && ViewModel.IsOk)
        {
                content.ViewModel.ProfilesList.Add(ViewModel.ProfileRecord);

        }
    }

所以你怎么看?如何获取数据是很好的解决方案吗?谢谢

4

1 回答 1

0

实际上,您并没有尝试将数据从一页获取到另一页。
在第二个/详细信息页面上,您正在添加一个新项目,然后当您返回第一个/主页面时,您希望它更新显示的数据以显示新项目。

我假设AddProfileViewModel并且ProfilesViewModel都坚持到磁盘/隔离存储,所以你可以在返回时刷新主/列表/第一页。

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    // ...

    if (e.NavigationMode == NavigationMode.Back)
    {
        (this.DataContext as ProfilesViewModel).Refresh();
    }
}
于 2013-03-11T15:06:14.143 回答