gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:12:39 30 4
gpt4 key购买 nike

我目前正在试验新的编译绑定(bind),并且(再次)达到了我在拼图中遗漏了一个小问题:为什么我必须调用 Bindings.Update?直到现在,我还认为实现 INotifyPropertyChanged 就足够了吗?

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

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

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

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

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

哪里:

  • ViewModel 是在 InitializeComponent() 运行之前设置的标准属性
  • Instance 是一个实现 INotifyPropertyChanged
  • 的简单对象

加载 Instance 后,我为 Instance 引发了一个属性更改事件。我什至可以调试到用户控件的依赖属性 TextValue 获得 correct 值的行——但没有显示任何内容。只有当我调用 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 方法以使用任务(参见上面的代码)!

最佳答案

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

到目前为止有意义吗?

现在创建一个绑定(bind);让我们这样说:

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

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

然后您的数据会更新。

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

什么是OneWay绑定(bind)模式?

OneWay 绑定(bind)模式意味着您可以更新实现 INotifyPropertyChanged 的​​后端模型属性,并且绑定(bind)到该属性的 UI 元素将反射(reflect)数据/值更改。太棒了。

为什么不起作用?

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

什么是一次性绑定(bind)模式?

一次性绑定(bind)模式意味着您仅在使用绑定(bind)加载/呈现 UI 元素时绑定(bind)到基础模型一次。这意味着如果您的基础数据尚不可用,它就无法显示该数据,一旦数据可用,它就不会显示该数据。为什么?因为 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.

如何解决这个问题?

有几种方法。第一个也是最简单的方法是将绑定(bind)从 ="{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} 开箱即用的性能优势的另一种方法是在您的 View 模型完全完成后调用 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?

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

祝你好运!

关于c# - 使用编译绑定(bind) (x :bind), 为什么我必须调用 Bindings.Update()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33070705/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com