ReactiveUI 6.0 and WinForms binding
本文关键字:WinForms binding and ReactiveUI | 更新日期: 2023-09-27 18:13:10
现在ReactiveUI 6.0已经发布了,我有一个问题要问社区:绑定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));
我开始喜欢这个方法了,但是其他人怎么看这三种方法呢?
从技术上讲,表单变成了视图和控制器或呈现器,而模型仍然是分开的,并且不知道它的消费者。这是首选的方法吗?
更新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;
}
}
我喜欢这种方法的优雅。
还有其他意见吗?
最简单的方法是使用ReactiveUI绑定-在你的表单上实现IViewFor<TViewModel>
,然后你会得到几个新的方法,如Bind
和OneWayBind
。
查看https://github.com/reactiveui/ReactiveUI/blob/docs/docs/basics/bindings.md获取更多信息