2012-03-11 51 views
2

首先窗體2要更新類文件後沒有更新的靜態變量重定向

private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative)); 
      Class1.AddButton = 1; 
      Class1.ChiefAnswer = "Done"; 
     } 

類文件,表格1從類文件中檢索,這意味着我必須只更新類文件,當它從窗口2到Form1重定向,它會重新加載

private static int AddBtn = 0; 
public static int AddButton 
     { 
      get { return AddBtn; } 
      set { AddBtn = value; } 
     } 

Form1中(不更新,類似乎仍持有舊的值)

if (Class1.AddButton == 3) 
{ 
    MakeButton(); 
} 

在第一次,我開始添加更多的變量並修復我的表單2中的列表後,每個人都工作正常,但沒有任何意義。當我調試它,在形式2中,示出了Add按鈕= 3的內部形式2

儘管調試回事,我切換到的Class1.cs,值心不是更新(可在其非實時)

無論如何,當它到達form1從class1.cs中檢索,它不檢索我期待的值

感謝您的幫助! :)

+0

你爲什麼不只是使用一個查詢字符串?你現在有什麼不會支持墓碑(你只是存儲一個int) - 但使用Querystring將 – 2012-03-11 22:36:35

回答

3

查詢字符串的方法

NavigationService.Navigate(new Uri("/PanoramaPage1.xaml?selected=item2", UriKind.Relative)); 

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
    { 
     string selected = String.Empty; 

     //check to see if the selected parameter was passed. 
     if (NavigationContext.QueryString.ContainsKey("selected")) 
     { 
      //get the selected parameter off the query string from MainPage. 
      selected = NavigationContext.QueryString["selected"]; 
     } 

     //did the querystring indicate we should go to item2 instead of item1? 
     if (selected == "item2") 
     { 
      //item2 is the second item, but 0 indexed. 
      myPanorama.DefaultItem = myPanorama.Items[1]; 
     } 
     base.OnNavigatedTo(e); 
    } 
1

你可以嘗試切換這些語句的順序,所以重定向之前做了更新:​​

Class1.AddButton = 1; 
Class1.ChiefAnswer = "Done"; 
NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative)); 
+0

嗨,我試過了,它並不會幫助 – CodeGuru 2012-03-12 03:33:15