如果我有以下视图模型
class Foo : INotifyPropertyChanged {
ISubject<string> Name { ... }
}
和一些想象的XAML代码
<TextBox Text="{my:Subscribe Path=Name}/>
我希望双向绑定能够表现出来
>在UI中更新文本框时调用Subject.onNext
>通过订阅Subject.Subscribe更新文本框
由于WPF只支持INPC,我的想法是创建一个代理INPC对象
通过标记扩展名
class WPFSubjectProxy : INotifyPropertyChanged{
string Value { ... }
}
代理将如此连接到主题
subject.Subscribe(v=>proxy.Value=v);
proxy
.WhenAny(p=>p.Value, p.Value)
.Subscribe(v=>subject.OnNext(v))
注意WhenAny是用于订阅的ReactiveUI帮助程序
INPC活动.
但后来我需要生成一个绑定并返回
通过标记扩展.
我知道我想做什么,但无法弄明白
标记扩展魔术将它们放在一起.
最佳答案 如果没有具体看到你正在努力解决的问题,很难说,但也许
this有帮助吗?
编辑
我(bradgonesurfing)提出的解决方案是由于指针在下面
分配正确答案.
节点
和实施代码.它依赖于ReactiveUI和私有库中的辅助函数,用于将ISubject绑定到INPC支持对象上的可变属性
using ReactiveUI.Subjects;
using System;
using System.Linq;
using System.Reactive.Subjects;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;
namespace ReactiveUI.Markup
{
[MarkupExtensionReturnType(typeof(BindingExpression))]
public class SubscriptionExtension : MarkupExtension
{
[ConstructorArgument("path")]
public PropertyPath Path { get; set; }
public SubscriptionExtension() { }
public SubscriptionExtension(PropertyPath path)
{
Path = Path;
}
class Proxy : ReactiveObject
{
string _Value;
public string Value
{
get { return _Value; }
set { this.RaiseAndSetIfChanged(value); }
}
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
var pvt = serviceProvider as IProvideValueTarget;
if (pvt == null)
{
return null;
}
var frameworkElement = pvt.TargetObject as FrameworkElement;
if (frameworkElement == null)
{
return this;
}
object propValue = GetProperty(frameworkElement.DataContext, Path.Path);
var subject = propValue as ISubject<string>;
var proxy = new Proxy();
Binding binding = new Binding()
{
Source = proxy,
Path = new System.Windows.PropertyPath("Value")
};
// Bind the subject to the property via a helper ( in private library )
var subscription = subject.ToMutableProperty(proxy, x => x.Value);
// Make sure we don't leak subscriptions
frameworkElement.Unloaded += (e,v) => subscription.Dispose();
return binding.ProvideValue(serviceProvider);
}
private static object GetProperty(object context, string propPath)
{
object propValue = propPath
.Split('.')
.Aggregate(context, (value, name)
=> value.GetType()
.GetProperty(name)
.GetValue(value, null));
return propValue;
}
}
}