编译绑定(x:Bind)是否需要ViewModel从DependencyObject派生?

时间:2018-08-16 15:53:29

标签: c# xaml mvvm uwp catel

我试图在通过Catel MVVM Framework实现的项目上使用编译的绑定(应该没关系)。但是,在生成的代码中,我得到了编译器错误,因为我的ViewModel没有实现DependencyObjects通常公开的某些功能:

  

CS1061'MainViewModel'不包含'RegisterPropertyChangedCallback'的定义,并且找不到扩展方法'RegisterPropertyChangedCallback'接受类型为'MainViewModel'的第一个参数(您是否缺少using指令或程序集引用?)

也:

  

CS0039无法通过引用转换,装箱转换,拆箱转换,换行转换或空类型转换将类型“ Windows.UI.Xaml.DependencyObject”转换为“ CatelCompiledBindingTest.ViewModels.MainViewModel”

x:Bind编译器生成的引发错误的代码如下:

public void DependencyPropertyChanged_VM_Visible(global::Windows.UI.Xaml.DependencyObject sender, global::Windows.UI.Xaml.DependencyProperty prop)
{
    MainView_obj1_Bindings bindings = TryGetBindingObject();
    if (bindings != null)
    {
        global::CatelCompiledBindingTest.ViewModels.MainViewModel obj = sender as global::CatelCompiledBindingTest.ViewModels.MainViewModel;
        if (obj != null)
        {
            bindings.Update_VM_Visible(obj.Visible, DATA_CHANGED);
        }
    }
}
private global::CatelCompiledBindingTest.ViewModels.MainViewModel cache_VM = null;
private long tokenDPC_VM_Visible = 0;
public void UpdateChildListeners_VM(global::CatelCompiledBindingTest.ViewModels.MainViewModel obj)
{
    if (obj != cache_VM)
    {
        if (cache_VM != null)
        {
            ((global::System.ComponentModel.INotifyPropertyChanged)cache_VM).PropertyChanged -= PropertyChanged_VM;
            cache_VM.UnregisterPropertyChangedCallback(global::CatelCompiledBindingTest.ViewModels.MainViewModel.VisibleProperty, tokenDPC_VM_Visible);
            cache_VM = null;
        }
        if (obj != null)
        {
            cache_VM = obj;
            ((global::System.ComponentModel.INotifyPropertyChanged)obj).PropertyChanged += PropertyChanged_VM;
            tokenDPC_VM_Visible = obj.RegisterPropertyChangedCallback(global::CatelCompiledBindingTest.ViewModels.MainViewModel.VisibleProperty, DependencyPropertyChanged_VM_Visible);
        }
    }
}

MainViewModel非常简单(不包括Catel样板代码):

public class MainViewModel : ViewModelBase
{
    public MainViewModel()
    {
        ToggleVisibility = new Command(OnToggleVisibilityExecute);
        AddText = new Command(OnAddTextExecute);
        SomeText = "";
    }

    //ViewModel properties

    public string SomeText
    {
        get { return GetValue<string>(SometextProperty); }
        set { SetValue(SometextProperty, value); }
    }

    public bool Visible
    {
        get { return GetValue<bool>(VisibleProperty); }
        set { SetValue(VisibleProperty, value); }
    }

    //ViewModel Commands

    public Command ToggleVisibility { get; private set; }

    public void OnToggleVisibilityExecute()
    {
        Visible = !Visible;
    }

    public Command AddText { get; private set; }

    public void OnAddTextExecute()
    {
        SomeText += "Random text! ";
    }
}

视图如下:

<controls:Page
    x:Class="CatelCompiledBindingTest.Views.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:CatelCompiledBindingTest.Views"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:controls="using:Catel.Windows.Controls"
    xmlns:converters="using:Catel.MVVM.Converters"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Page.Resources>
        <converters:BooleanToCollapsingVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    </Page.Resources>

    <Grid>
        <Grid.KeyboardAccelerators>
            <KeyboardAccelerator Invoked="{x:Bind VM.OnToggleVisibilityExecute}" Key="T" Modifiers="Control"/>
            <KeyboardAccelerator Invoked="{x:Bind VM.OnAddTextExecute}" Key="Add" Modifiers="Control"/>
        </Grid.KeyboardAccelerators>

        <Border Width="500" Height="500" Visibility="{x:Bind VM.Visible, Mode=OneWay}" Background="SlateGray">
            <TextBlock Style="{ThemeResource HeaderTextBlockStyle}" Text="{x:Bind VM.SomeText, Mode=OneWay}"/>
        </Border>

    </Grid>
</controls:Page>

具有下面的代码才能使用编译的绑定:

public sealed partial class MainView : Page
{
    public MainView()
    {
        this.InitializeComponent();
        VM = DataContext as MainViewModel;
    }

    public MainViewModel VM { get; set; }
}

现在的问题是:在编译器生成的DependencyObject中,为什么编译器假定MainViewModel的类型为DependencyPropertyChanged_VM_Visible?根据{{​​3}},ViewModel只是需要实现IPropertyChanged

如何解决此问题(除了再次诉诸{Binding}外)?我需要在ViewModel中包括哪些命名空间才能使已编译的绑定起作用?

1 个答案:

答案 0 :(得分:0)

它假定它是一个依赖项属性,因为在视图unsigned上声明了rvalues属性。您应该将VM属性定义为依赖项属性,以便可以对其进行绑定(因此,该属性不是值/类型,而是属性本身)。