【发布时间】:2014-07-15 21:37:53
【问题描述】:
现在有了 ReactiveUI 6.0 has been released,我向社区提出一个问题:绑定 ReactiveObject(s) 和 Windows Form(s) 的最佳或最有效的方法是什么。
这是我目前所拥有的:
我的模型
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));
我开始喜欢这个,但是其他人如何看待这三种方法中的任何一种?
从技术上讲,Form 变成了 View 和 Controller 或 Presenter,而 Model 仍然是分离的,不知道它的消费者。这会是首选方法吗?
更新 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;
}
}
我喜欢这种方法的优雅。
还有其他意见吗?
【问题讨论】:
-
看起来你可以很容易地为此创建一个扩展方法。
标签: c# winforms reactiveui