7

NavigationService.GoBack(); 我在 viewmodel 中设置了一个变量,然后我想返回,当我返回时我想使用该变量,但似乎没有运行,我不想刷新该页面。

有任何想法吗?

在这里我正在设置变量,我想回到我的 MainPage。

void item_click(object sender, RoutedEventArgs e)
   {

     Button b = e.OriginalSource as Button;

     App.ViewModel.bName = b.Content.ToString();
     App.ViewModel.bID = b.Name;

     this.NavigationService.GoBack();

    }

在我的 MainPage 中,我想运行检查以查看变量是否已更改。

public MainPage()

    {
      /*
i don't want to run this actually, when i'm coming back from my secondarypage.

        InitializeComponent();
        InitializeSettings();

*/

            if (App.ViewModel.bName != null)
            {
                myButton.Content = App.ViewModel.bName;
                myButton.Name = App.ViewModel.bID;
            }
    }
4

2 回答 2

1

我没有理解所有内容,但是您是否尝试过使用“App”类来为所有页面而不是 ViewModel 声明一个全局变量?

添加也许:

利用:

(App)App.Current 

从此链接: 如何在 Windows Phone 的其他页面中引用属性、应用程序中的全局变量

于 2012-12-19T09:18:49.470 回答
1

哦,我很抱歉这个模糊的问题,但我解决了我的问题。

这是主页中需要的。

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {

        if (App.ViewModel.bName != null)
        {
            myButton.Content = App.ViewModel.bName;
            myButton.Name = App.ViewModel.bID;
        }


        base.OnNavigatedTo(e);

    }
于 2012-12-19T09:57:01.683 回答