重定向后静态变量不更新

时间:2012-03-11 18:41:05

标签: c# silverlight windows-phone-7

第一个Form2想要更新类文件

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

类文件,表单1从类文件中检索,这意味着我必须只更新类文件,当它从form2重定向到form1时,它将重新加载

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

Form1(未更新,该类似乎仍然保留较旧的值)

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

在我开始添加更多变量并在表单2中修复列表之后,每个人都工作正常,它没有用,这没有任何意义。当我调试它时,在表单2中,它显示AddButton = 3 in form 2

在调试过程中,我切换到class1.cs,值没有更新(可能不是实时)

无论如何,当它到达从class1.cs检索的form1时,它没有检索到我期待的值

谢谢你的帮助! :)

2 个答案:

答案 0 :(得分: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 :(得分:1)

您可以尝试切换这些语句的顺序,因此在重定向之前进行更新:

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