使用编译绑定 (x:bind),为什么我必须调用 Bindings.Update()?

With compiled bindings (x:bind), why do I have to call Bindings.Update()?

我目前正在试验新的编译绑定,并且(再次)达到了我遗漏了一个谜题的地步:为什么我必须调用 Bindings.Update?到现在为止,我认为实施 INotifyPropertyChanged 就足够了吗?

在我的例子中,如果我调用这个神秘的方法(由编译绑定自动生成),GUI 只会显示正确的值。

我正在使用具有以下(此处已简化)xaml 语法的用户控件:

<UserControl>
  <TextBlock Text="x:Bind TextValue"/>
</UserControl>

其中 TextValue 是此用户控件的简单依赖项 属性。在页面中,我将此控件用作:

<Page>
  <SampleControl TextValue="{x:Bind ViewModel.Instance.Name}"/>
</Page>

其中:

加载 Instance 后,我为 Instance 引发了 属性 更改事件。我什至可以调试到该行,其中用户控件的依赖性 属性 TextValue 获得 正确的 值——但没有显示任何内容。只有当我调用 Bindings.Update() 时,才会显示该值。我在这里错过了什么?

更新

我也不和 {x:Bind ... Mode=OneWay} 一起工作。

更多代码

Person.cs:

using System.ComponentModel;
using System.Threading.Tasks;

namespace App1 {
    public class Person : INotifyPropertyChanged {
        public event PropertyChangedEventHandler PropertyChanged;

        private string name;
        public string Name { get {
                return this.name;
            }
            set {
                name = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Name"));
            }
        }
    }

    public class ViewModel : INotifyPropertyChanged {

        public event PropertyChangedEventHandler PropertyChanged;

        private Person instance;
        public Person Instance {
            get {
                return instance;
            }
            set {
                instance = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Instance"));
            }
        }

        public Task Load() {
            return Task.Delay(1000).ContinueWith((t) => {
                var person = new Person() { Name = "Sample Person" };                
                this.Instance = person;
            });
        }


    }
}

SampleControl.cs:

<UserControl
    x:Class="App1.SampleControl"
    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"
    d:DesignHeight="100"
    d:DesignWidth="100">

    <TextBlock Text="{x:Bind TextValue, Mode=OneWay}"/>

</UserControl>

SampleControl.xaml.cs:

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace App1 {
    public sealed partial class SampleControl : UserControl {

        public SampleControl() {
            this.InitializeComponent();
        }

        public string TextValue {
            get { return (string)GetValue(TextValueProperty); }
            set { SetValue(TextValueProperty, value); }
        }

        public static readonly DependencyProperty TextValueProperty =
            DependencyProperty.Register("TextValue", typeof(string), typeof(SampleControl), new PropertyMetadata(string.Empty));

    }
}

MainPage.xaml:

<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">

    <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <local:SampleControl TextValue="{x:Bind ViewModel.Instance.Name, Mode=OneWay}"/>
    </StackPanel>
</Page>

MainPage.xaml.cs:

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace App1 {

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.DataContext = new ViewModel();
            this.Loaded += MainPage_Loaded;
            this.InitializeComponent();
        }

        public ViewModel ViewModel {
            get {
                return DataContext as ViewModel;
            }
        }

        private void MainPage_Loaded(object sender, RoutedEventArgs e) {
            ViewModel.Load();
            Bindings.Update(); /* <<<<< Why ????? */
        }
    }
}

再更新一次

我更新了 Load 方法来使用任务(见上面的代码)!

虽然 "traditional" 绑定默认为 "one-way"(或在某些情况下为双向),但已编译的绑定默认为 "one-time"。只需在设置绑定时更改模式即可:

<TextBlock Text="{x:Bind TextValue, Mode=OneWay}" />

首先,x:Bind默认的绑定方式是OneTime,需要按照上面的回答修改成OneWay才可以调用RaisePropertyChanged方法。

您的数据绑定代码似乎有问题。 请粘贴所有相关代码,以便我们查看此问题的根源。

最后我自己发现了错误:我使用基于任务的操作来加载我的视图模型,这导致由不正确的线程设置依赖项 属性(我认为)。如果我通过调度程序设置 Instance 属性 它会起作用。

    public Task Load() {
        return Task.Delay(1000).ContinueWith((t) => {
            var person = new Person() { Name = "Sample Person" };
            Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
            () => {
                this.Instance = person;
            });                
        });
    }

但也不例外,就是gui没有显示值!

有时您要显示的数据在您的页面加载和呈现几秒钟后才可用(例如从服务器或数据库返回)。如果您在 background/async 进程中调用数据,释放 UI 以在不挂起的情况下进行渲染,则尤其如此。

到目前为止有意义吗?

现在创建绑定;让我们这样说:

<TextBlock Text="{x:Bind ViewModel.User.FirstName}" />

您的代码隐藏中的 ViewModel 属性 的值将具有真实值并且可以很好地绑定。另一方面,您的用户将没有值,因为它尚未从服务器返回。结果那个和用户的 FirstName 属性 都不能显示,对吧?

那么你的数据就更新了。

您可能会认为,当您将 User 对象的值设置为真实对象时,您的绑定会自动更新。特别是如果您花时间将其设为 INotifyPropertyChanged 属性,对吗?对于传统的 {Binding} 也是如此,因为默认绑定模式是 OneWay。

什么是单向绑定模式?

OneWay 绑定模式意味着您可以更新实现 INotifyPropertyChanged 的​​后端模型属性,绑定到 属性 的 UI 元素将反映 data/value 更改。太棒了。

为什么不起作用?

不是因为{x:Bind}不支持Mode=OneWay,而是因为它默认为Mode=OneTime。回顾一下,传统的 {Binding} 默认为 Mode=OneWay,编译后的 {x:Bind} 默认为 Mode=OneTime。

什么是一次性绑定模式?

OneTime 绑定模式意味着您只绑定到基础模型一次,在具有绑定的 UI 元素的 load/render 时。这意味着如果您的基础数据尚不可用,它就无法显示该数据,一旦数据可用,它就不会显示该数据。为什么?因为 OneTime 不监听 INotifyPropertyChanged。它只在加载时读取。

Modes (from MSDN): For OneWay and TwoWay bindings, dynamic changes to the source don't automatically propagate to the target without providing some support from the source. You must implement the INotifyPropertyChanged interface on the source object so that the source can report changes through events that the binding engine listens for. For C# or Microsoft Visual Basic, implement System.ComponentModel.INotifyPropertyChanged. For Visual C++ component extensions (C++/CX), implement Windows::UI::Xaml::Data::INotifyPropertyChanged.

如何解决这个问题?

有几种方法。第一个也是最简单的方法是将绑定从 ="{x:Bind ViewModel.User.FirstName} 更改为 ="{x:Bind ViewModel.User.FirstName, Mode=OneWay}。这样做将监视 INotifyPropertyChanged 事件。

This is the right time to warn you that using OneTime by default is one of the many ways {x:Bind} tries to improve performance of binding. That's because OneTime is the fastest possible with the least memory reqs. Changing your binding to OneWay undermines this, but it might be necessary for your app.

另一种解决此问题并仍然保持 {x:Bind} 开箱即用的性能优势的方法是在视图模型完全准备好要显示的数据后调用 Bindings.Update();。如果您的工作是异步的,这很容易 - 但是,就像上面的示例一样,如果您不能确定计时器可能是您唯一可行的选择。

That sucks of course because a timer implies clock time, and on slow devices like a phone, that clock time might not properly apply. This is something every developer will have to work out specific to their app - that is to say, when is your data fully loaded and ready?

我希望这能解释正在发生的事情。

祝你好运!