0

我正在尝试通过 XML 序列化和独立存储将一些值发布到下一个屏幕。值通过按钮单击事件从文本框和文本块发布到下一个屏幕的文本块。但是 TextBlock 的文本不会发布到下一个屏幕,只会发布 textBox 文本。如果我调试和检查,XML 显示为“”,其中没有加载任何字符串。它也没有约束力。为什么?

//MainPage.xaml
        <StackPanel>
                <Button Margin="10"
                Content="Simple Sample"
                Name="btnSimple"
                Click="btnSimple_Click">                    

                </Button>
                <TextBlock TextWrapping="Wrap"   Text="{Binding FirstName, Mode=TwoWay}"/>           
                <TextBlock TextWrapping="Wrap" Text="{Binding MyText, Mode=TwoWay}"/>         

            </StackPanel>

 //MainPage.xaml.cs
        using System;
        using System.Windows;
        using Microsoft.Phone.Controls;    
        using System.IO;
        using System.IO.IsolatedStorage;
        using System.Text;
        using System.Xml.Serialization;       

        namespace WPIsolatedStorage
        {
          public partial class MainPage : PhoneApplicationPage
          {
            // Constructor
              private LastUser _User = new LastUser();
              private const string USER_KEY = "LastUser";
            public MainPage()
            {
              InitializeComponent();
            }

            private void GetUser()
            {
                string xml;

                xml = IsolatedStorageSettings.ApplicationSettings[USER_KEY].ToString();
                using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(xml)))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(LastUser));
                    _User = (LastUser)serializer.Deserialize(ms);
                }
            }

            private void btnSimple_Click(object sender, RoutedEventArgs e)
            {
              NavigationService.Navigate(new Uri("/PageSimple.xaml", UriKind.Relative));
            }

            private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
            {
                if (IsolatedStorageSettings.ApplicationSettings.Contains(USER_KEY))
                    GetUser();

                this.DataContext = _User;            

            }
          }
        }

 //LastUser.cs
    using System.ComponentModel;

    namespace WPIsolatedStorage
    {       

      public class LastUser : INotifyPropertyChanged
      {
        #region INotifyPropertyChanged Event   
        public event PropertyChangedEventHandler PropertyChanged;   
        protected void RaisePropertyChanged(string propertyName)
        {
          if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }   
        #endregion

        #region Private Variables
        private string mFirstName = string.Empty;

        private string mTextBlock = string.Empty;
        #endregion

        #region Public Variables
        public string FirstName
        {
          get { return mFirstName; }
          set
          {
            if (mFirstName != value)
            {
              mFirstName = value;
              RaisePropertyChanged("FirstName");
            }
          }
        }

        public string MyText
    {
        get { return this.mTextBlock; }
        set
        {
            this.mTextBlock = value;
            this.RaisePropertyChanged("MyText");

        }
    }}
    } 

//SeconPage.xaml

 <TextBox Grid.Column="1"
               Grid.Row="0"
               Text="{Binding FirstName, Mode=TwoWay}"
               Name="txtFirstName" />

<TextBlock Grid.Column="1" Name="MyTextBlock" HorizontalAlignment="Left" Margin="12,178,0,-224" Grid.Row="4" TextWrapping="Wrap" Text="{Binding MyText, Mode=TwoWay}" VerticalAlignment="Top" Height="93" Width="191" FontSize="36"/>
4

2 回答 2

1

您的绑定表达式中有错字。应该

<TextBlock Grid.Column="1" .... Text="{Binding MyText}" ../>

代替

<TextBlock Grid.Column="1" .... Text="{Binding Mytext}" ../>

我的第二个假设是:您在代码中使用了简单的绑定

<TextBlock TextWrapping="Wrap" Text="{Binding MyText, Mode=TwoWay}"/>

但在 Windows Phone 中,绑定仅在您没有集中控制后才起作用。您可以在此处找到讨论和一些解决方案:

TextBox Binding TwoWay 在焦点丢失 WP7 之前不会更新

于 2013-09-27T15:25:02.633 回答
1

试试这个。 代码片段[C#]:

   public class Data : INotifyPropertyChanged
        {
            private int customerID;

            public int CustomerID
            {
                get { return customerID; }
                set { customerID = value; OnPropertyChanged("CustomerID"); }
            }
      public event PropertyChangedEventHandler PropertyChanged;

            public void OnPropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    var e = new PropertyChangedEventArgs(propertyName);
                    handler(this, e);
                }
            }

代码片段[XAML]:

   <TextBlock Content="{Binding CustomerID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
于 2013-09-28T04:06:46.150 回答