为什么 TextBlock Text 在 ViewModel 中没有更新

本文关键字:更新 ViewModel TextBlock Text 为什么 | 更新日期: 2023-09-27 18:33:00

我正在尝试通过XML序列化和独立存储将一些值发布到下一个屏幕。值从文本框发布,也从文本块发布到下一个屏幕的 textBlocks 与按钮单击事件。但是 TextBlock 的文本不会发布到下一个屏幕,只会发布文本框文本。如果我调试并检查,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"/>

为什么 TextBlock Text 在 ViewModel 中没有更新

绑定表达式中存在拼写错误。应该是

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

而不是

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

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

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

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

文本框绑定双向不会更新,直到焦点丢失 WP7

试试这个。代码片段[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}"