2012-12-19 31 views
7

NavigationService.GoBack();我在viewmodel中設置了一個變量,然後我想回去,當我回來時我想使用該變量,但似乎沒有運行,我不想刷新該頁面。使用NavigationService上的變量。返回

任何想法?

這裏我設置變量,我想回到我的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; 
      } 
    } 
+1

你能發表一些你的代碼嗎?你的問題有點含糊 –

+0

當然好,我已經編輯了我的帖子 – user1845258

+0

當你離開頁面並返回主頁面時,你的bName和bID所在的viewmodel類是否存在隱形?而一個新的實例是由同一個類組成的,這意味着你的變量又是空的,你的結果也會如此。 –

回答

1

我不明白一切,但你的「應用程序」類試圖聲明一個變量全球的所有網頁,而不是視圖模型?

添加可能:

用途:

(App)App.Current 

從這個鏈接: How to reference properties, global variable in app from other page for windows phone

+0

sry我的問題有點模糊,但我想從我的主頁導航到我的次頁面,然後我想回去而不刷新主頁面,但我想在主頁面中更改。 「 – user1845258

+0

」然後我想回去不刷新主頁,但我想在主頁上更改。「好的沒問題,但是你需要在主頁上做些什麼改變? –

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

    } 
+0

我很高興聽到!祝未來好運;-) –