Windows 8 文本框中的双向绑定在失去焦点后未更新

Windows 8 文本框中的双向绑定在失去焦点后未更新,windows-8,Windows 8,在下面的示例中,当我在文本框中键入一个新字符串并将其制表时,文本块将被更新,但文本框保留我键入的值,而不是使用修改后的字符串进行更新。你知道如何改变这种行为吗 <Page x:Class="App1.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/200

在下面的示例中,当我在文本框中键入一个新字符串并将其制表时,文本块将被更新,但文本框保留我键入的值,而不是使用修改后的字符串进行更新。你知道如何改变这种行为吗

<Page
        x:Class="App1.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App1"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">

        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}" Margin="106,240,261,187">
            <StackPanel>
                <TextBox Text="{Binding MyProp, Mode=TwoWay}"/>
                <TextBlock Text="{Binding MyProp}"/>
            </StackPanel>
        </Grid>
    </Page>

public class ViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public ViewModel()
        {
            MyProp = "asdf";
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        protected bool SetField<T>(ref T field, T value, string propertyName)
        {
            if (EqualityComparer<T>.Default.Equals(field, value)) return false;
            field = value;
            OnPropertyChanged(propertyName);
            return true;
        }

        private string m_myProp;

        public string MyProp
        {
            get { return m_myProp; }
            set
            {
                m_myProp = value + "1";
                OnPropertyChanged("MyProp");
            }
        }
    }

公共类视图模型:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
公共视图模型()
{
MyProp=“asdf”;
}
受保护的虚拟void OnPropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(handler!=null)handler(这是新的PropertyChangedEventArgs(propertyName));
}
受保护的布尔设置字段(参考T字段、T值、字符串属性名称)
{
if(EqualityComparer.Default.Equals(字段,值))返回false;
字段=值;
OnPropertyChanged(propertyName);
返回true;
}
私有字符串m_myProp;
公共字符串MyProp
{
获取{return m_myProp;}
设置
{
m_myProp=值+1;
OnPropertyChanged(“MyProp”);
}
}
}

您看到的行为在某种程度上是预期的行为

当您从文本框中取出制表符时,绑定将调用MyProp setter。当您调用OnPropertyChanged()时,您仍然处于原始绑定的上下文中,只有其他绑定会收到更改通知。(要验证它,请在Getter上设置一个断点,并查看它仅在调用OnPropertyChanged后命中一次。解决方法是在初始绑定完成更新后调用OnPropertyChanged,这是通过调用方法async而不是等待它返回来实现的

将对OnPropertyChanged(“MyProp”)的调用替换为:


谢谢你的提示!Windows Phone 7没有这个“问题”。我最终得到了以下代码:CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,新的Windows.UI.Core.DispatchedHandler(()=>{MyProp=newValue;});很好的解释!我使用SynchronizationContext.Current.Post方法在绑定上下文之外“重新排队”工作,因为我的视图模型没有调度程序。
Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new 
Windows.UI.Core.DispatchedHandler(() => { OnPropertyChanged("MyProp"); }));