我来自WPF / Prism背景,但是我非常喜欢X:Bind提供的功能。使用Prism.Uno时如何使x:Bind与我的ViewModel一起使用?
[我有棱镜:ViewModelLocator.AutoWireViewModel =“ True”,但在理解设计时它的工作原理时,我似乎缺少一些东西。
谢谢G
x:Bind
的使用要求绑定路径植根于视图中。
要使用DataContext,您需要使其可通过视图键入,如下所示:
public partial class MyControl : INotifyPropertyChanged
{
#if !HAS_UNO
// Uno already defines this event (it will be removed
// in the future to be aligned properly with WinUI)
public event PropertyChangedEventHandler PropertyChanged;
#endif
public MainPage()
{
this.InitializeComponent();
DataContextChanged +=
(s, e) => PropertyChanged?.Invoke(
this,
new PropertyChangedEventArgs(nameof(ViewModel)));
}
public MyViewModel ViewModel => DataContext as MyViewModel;
}