现在ReactiveUI 6.0 has been released,我对公共性有一个问题:绑定ReactiveObject和Windows窗体的最佳或最有效的方法是什么。
这是我到目前为止所做的:
我的模特
namespace WindowsFormsApplication1
{
#region
using System;
using System.Reactive.Linq;
using ReactiveUI;
#endregion
public class ClockModel : ReactiveObject
{
#region Fields
private DateTime currentDateTime;
#endregion
#region Constructors and Destructors
public ClockModel() { Observable.Interval(TimeSpan.FromSeconds(1)).Subscribe(s => this.CurrentDateTime = DateTime.Now); }
#endregion
#region Public Properties
public DateTime CurrentDateTime
{
get { return currentDateTime; }
set { this.RaiseAndSetIfChanged(ref currentDateTime, value); }
}
#endregion
}
}
我唯一的表单
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
readonly ClockModel model = new ClockModel();
public Form1()
{
InitializeComponent();
this.label1.DataBindings.Add("Text", model, "CurrentDateTime");
}
}
}
它确实有效,但我不会因为它们的字符串名称引用每一侧的属性而感到兴奋。有没有更好的方法来实现这一目标?
更新1
public class NameOf<T>
{
public static string Property<TProp>(Expression<Func<T, TProp>> expression)
{
var body = expression.Body as MemberExpression;
if (body == null)
throw new ArgumentException("'expression' should be a member expression");
return body.Member.Name;
}
}
这种形式比上一种形式更令人满意吗?
this.label1.DataBindings.Add(NameOf<Form1>.Property(e => e.label1.Text), model, NameOf<ClockModel>.Property(p => p.CurrentDateTime));
更新2
另一个扩展名:
public static class Extensions
{
public static string GetPropertyName<T,TProp>(this T obj, Expression<Func<T, TProp>> expression)
{
var body = expression.Body as MemberExpression;
if (body == null)
throw new ArgumentException("'expression' should be a member expression");
return body.Member.Name;
}
}
现在我可以使用:
this.label1.DataBindings.Add(this.GetPropertyName(e => e.label1.Text), model, model.GetPropertyName(p => p.CurrentDateTime));
或者这个:
this.label2.DataBindings.Add(this.label2.GetPropertyName(e => e.Text), model, model.GetPropertyName(p => p.Ticks));
我开始喜欢这个,但其他人怎么看待这三种方法中的任何一种?
从技术上讲,表格变成了视图和控制器或演示者,而模型仍然是分开的,并且不了解其消费者。这会是首选方法吗?
更新3
这可能会使一些纯粹主义者心脏病发作... 基于Paul Betts的建议:
namespace WindowsFormsApplication1
{
#region
using System.Windows.Forms;
using ReactiveUI;
#endregion
public partial class Form1 : Form, IViewFor<ClockModel>
{
#region Constructors and Destructors
public Form1()
{
InitializeComponent();
this.ViewModel = new ClockModel();
this.Bind(ViewModel, vm => vm.CurrentDateTime, v => v.label1.Text);
this.Bind(ViewModel, vm => vm.Ticks, v => v.label2.Text);
}
#endregion
#region Public Properties
public ClockModel ViewModel { get; set; }
#endregion
#region Explicit Interface Properties
object IViewFor.ViewModel
{
get { return ViewModel; }
set { ViewModel = value as ClockModel; }
}
#endregion
}
}
有趣的行为变化:在更新3之前,时间显示为秒。我只能说这是由于第三种方法中使用的默认ReactiveUI转换器。我必须要了解更多。
public class DateTimeStringConverter : IBindingTypeConverter
{
public int GetAffinityForObjects(Type fromType, Type toType) { return 1; }
public bool TryConvert(object @from, Type toType, object conversionHint, out object result) {
result = ((DateTime)@from).ToString("F");
return true;
}
}
我喜欢这种方法的优雅。
还有其他意见吗?
答案 0 :(得分:2)
最简单的方法是使用ReactiveUI绑定 - 在表单上实现IViewFor<TViewModel>
,然后您将获得几个新方法,例如Bind
和OneWayBind
。
查看https://github.com/reactiveui/ReactiveUI/blob/docs/docs/basics/bindings.md了解详情