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;

在 App() 之前我放了这个:

公共静态条目 E;

然后在 App() 中:

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

E = new Entry();

InitializeComponent();

然后我的用户界面是两页。一是表格输入数据,二是读取。在应用程序栏按钮下,我有:

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 什么也没给我...

4

2 回答 2

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));
}
于 2012-06-07T14:09:34.327 回答
0

另一种选择是放弃您基本上仅用于将值从一页传递到下一页的全局变量。

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

private void ApplicationBarIconButton_Click(object sender, System.EventArgs e)
{
    this.NavigationService.Navigate(new Uri("/Page2.xaml?title=TitleTextBox.Text&comment=CommentTextBox.Text", UriKind.Relative));
}
于 2012-06-07T22:32:02.367 回答