2012-06-07 65 views
0

我想写一个简单的方法来写入/读取WP7中的对象元素。有些东西工作不正常。我的思维方式和我已经做的是这样的:写入/读取对象

首先我创建一个代表我的对象的类。我添加静态字符串只是为了看看是否一切正常:

namespace SimpleObject.Objects 
{ 
    public class Entry 
    { 
     public string entrytitle { get; set; } 
     public string entrycomment { get; set; } 
     public string entrycat = "works"; 

     public Entry() { } 
     public Entry(string Entrytitle, string Entrycomment, string Entrycat) 
     { 

      this.entrytitle = Entrytitle; 
      this.entrycomment = Entrycomment; 
      this.entrycat = Entrycat; 
     } 

     public string entry { get; set; } 

    } 
} 

然后,正如我在一些文章中,我需要在App.xaml.cs一些改变阅读在这里,我们然后去:

使用SimpleObject.Objects;

之前应用程序()我把这个:

公共静态条目E;

然后在应用程序()这样的:

UnhandledException += new EventHandler<ApplicationUnhandledExceptionEventArgs>(Application_UnhandledException); 

E = new Entry(); 

InitializeComponent(); 

然后我的UI为两页。一种是输入数据的形式,另一种是读取数据。在应用程序栏按钮,我有:

private void ApplicationBarIconButton_Click(object sender, System.EventArgs e) 
     { 
      Entry E = new Entry 
      { 
       entrytitle = TitleTextBox.Text, 
       entry = CommentTextBox.Text, 
      }; 

      this.NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.Relative)); 
      MessageBox.Show("Category added!"); 

     } 

最后页面,目前的研究结果:

private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      TextBlock1.Text = App.E.entrycat; 
      TextBlock2.Text = App.E.entrytitle; 
     } 

其次TextBlock的给我什么...

回答

0

你永远设置全局静态值。在您按一下按钮,它应该是这样的:

private void ApplicationBarIconButton_Click(object sender, System.EventArgs e) 
{ 
    App.E.entrytitle = TitleTextBox.Text, 
    App.E.entrycat = CommentTextBox.Text, 

    this.NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.Relative)); 
} 
+0

在此更改后,它们均未给出任何结果。 – dargod

+0

我只能建议使用调试器。我的App.E的价值观是否被设置在第1页?当你碰到第2页的Button时他们还在吗?除非别的东西是覆盖值(或E本身),或者应用程序逻辑不完全是你发布的,那么它应该工作。 – ctacke

+0

一般我想学习如何管理WP中的数据。你可以推荐任何好的教程? – dargod

0

另一个选择是放弃它你基本上只能使用通过从一个页面到下一个值的全局变量。

您可以像查询字符串一样在查询字符串值中执行此操作,并在您的页面加载处理程序中选取它们。

private void ApplicationBarIconButton_Click(object sender, System.EventArgs e) 
{ 
    this.NavigationService.Navigate(new Uri("/Page2.xaml?title=TitleTextBox.Text&comment=CommentTextBox.Text", UriKind.Relative)); 
}