2012-06-07 62 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)); 
}