C# 在NavigationService.GoBack上使用变量

C# 在NavigationService.GoBack上使用变量,c#,windows-phone-8,C#,Windows Phone 8,NavigationService.GoBack();我在viewmodel中设置了一个变量,然后我想返回,当我返回时,我想使用该变量,但似乎什么都没有运行,我不想刷新该页面 有什么想法吗 在这里我设置变量,我想回到我的主页 void item_click(object sender, RoutedEventArgs e) { Button b = e.OriginalSource as Button; App.ViewModel.bName = b.Content

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

有什么想法吗

在这里我设置变量,我想回到我的主页

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();

    }
在我的主页中,如果变量发生了变化,我想对se进行检查

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;
            }
    }

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

或许可以加上:

使用:

通过此链接:

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

这是主页上需要的

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);

    }

你能发布一些代码吗?你的问题有点含糊好吧,我已经编辑了我的帖子,你的bName和bID所在的viewmodel类是否有可能在你离开页面返回主页时被破坏?一个新的实例是由同一个类组成的,这意味着你的变量又是空的,你的结果也将是空的。不,这不是问题:P看,我想在mainpage中加载变量中的新值,而不刷新整个页面(mainpage)啊,我的错。我没有忘记你的问题sry我的问题有点模糊,但我想从主页导航到第二页,然后我想返回而不刷新主页,但我想在主页中更改。“然后我想返回而不刷新主页,但我想在主页中更改。”好的,没问题,但是你需要在主页上做什么更改呢?我很高兴听到这个消息!祝未来好运;-)
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);

    }