c# – 使用ReactiveUI的BindTo()更新XAML属性会生成警告

我正在尝试更新视图的XAML中元素的属性:

this.WhenAnyValue(x => x.ViewModel.IsEnabled).BindTo(this, x => x.MyButton.IsEnabled);

这可以按预期工作,但是,它会在运行时生成警告:

POCOObservableForProperty: rx_bindto_test.MainWindow is a POCO type and won’t send change notifications, WhenAny will only return a single value!

我可以通过将表达式更改为:来消除警告:

this.WhenAnyValue(x => x.ViewModel.IsEnabled).Subscribe(b => MyButton.IsEnabled = b);

但我仍然想知道为什么它与BindTo()无法正常工作.

编辑:即使是常规的Bind和OneWayBind也会生成此警告.

>我在这里做错了什么?
>是否真的有必要将ViewModel定义为View上的依赖属性以便能够观察它? (当我在View上声明它作为常规属性时,ReactiveUI会生成相同的POCO警告)我不能简单地使它继承自ReactiveObject,因为C#不支持多重继承.

MainWindow.xaml.cs

public partial class MainWindow : Window, IViewFor<MyViewModel>, IEnableLogger {
    public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register("ViewModel",
        typeof(MyViewModel), typeof(MainWindow));

    public MyViewModel ViewModel {
        get { return (MyViewModel)GetValue(ViewModelProperty); }
        set { SetValue(ViewModelProperty, value); }
    }

    object IViewFor.ViewModel {
        get { return ViewModel; }
        set { ViewModel = (MyViewModel)value; }
    }

    public MainWindow() {
        InitializeComponent();

        this.WhenAnyValue(x => x.ViewModel).BindTo(this, x => x.DataContext);

        this.WhenAnyValue(x => x.ViewModel.IsEnabled).BindTo(this, x => x.MyButton.IsEnabled);

        ViewModel = new MyViewModel();
        ViewModel.IsEnabled = true;
    }
}

MainWindow.xaml

<Window x:Class="rx_bindto_test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button x:Name="MyButton">My Button</Button>
    </Grid>
</Window>

MyViewModel.cs

public class MyViewModel : ReactiveObject, IEnableLogger {
    private bool isEnabled;

    public bool IsEnabled {
        get { return isEnabled; }
        set { this.RaiseAndSetIfChanged(ref isEnabled, value); }
    }
}

最佳答案 我认为混淆来自于你在“MyButton”解析时收到警告,而不是在ViewModel上.

MyButton是一个“常量”对象,没有任何生命周期(既不是INPC也不是DependencyObject),因此您可以安全地忽略此警告.

或者,您可以注册下面的额外属性解析器,它的行为类似于FrameworkElement的每个内部字段的POCO(减去警告),它非常接近XAML的每个控件(我相信):

Locator.CurrentMutable.Register(() => new CustomPropertyResolver(), typeof(ICreatesObservableForProperty));

public class CustomPropertyResolver : ICreatesObservableForProperty
{
    public int GetAffinityForObject(Type type, string propertyName, bool beforeChanged = false)
    {
        if (!typeof(FrameworkElement).IsAssignableFrom(type))
            return 0;
        var fi = type.GetTypeInfo().GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)
          .FirstOrDefault(x => x.Name == propertyName);

        return fi != null ? 2 /* POCO affinity+1 */ : 0;
    }

    public IObservable<IObservedChange<object, object>> GetNotificationForProperty(object sender, System.Linq.Expressions.Expression expression, bool beforeChanged = false)
    {
        var foo = (FrameworkElement)sender;
        return Observable.Return(new ObservedChange<object, object>(sender, expression), new DispatcherScheduler(foo.Dispatcher))
            .Concat(Observable.Never<IObservedChange<object, object>>());
    }
}
点赞