C# ReactiveUI 6.0和WinForms绑定
现在有了ReactiveUI 6.0,我向社区提出了一个问题:绑定ReactiveObject和Windows窗体的最佳或最有效的方法是什么 以下是我到目前为止的情况: 我的模型 我唯一的表格 它确实有效,但我对通过字符串名称引用每一方的属性并不感到兴奋。有没有更好的方法来实现这一点 更新1 鉴于: 或者这个:C# ReactiveUI 6.0和WinForms绑定,c#,winforms,reactiveui,C#,Winforms,Reactiveui,现在有了ReactiveUI 6.0,我向社区提出了一个问题:绑定ReactiveObject和Windows窗体的最佳或最有效的方法是什么 以下是我到目前为止的情况: 我的模型 我唯一的表格 它确实有效,但我对通过字符串名称引用每一方的属性并不感到兴奋。有没有更好的方法来实现这一点 更新1 鉴于: 或者这个: this.label2.DataBindings.Add(this.label2.GetPropertyName(e => e.Text), model, model.GetPro
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
}
}
我喜欢这种优雅的方式
还有其他意见吗?最简单的方法是使用反应式UI绑定-在表单上实现
IViewFor
,然后您将获得几种新方法,如Bind
和OneWayBind
查看以了解更多信息看起来您可以轻松地为此创建扩展方法。Paul,感谢您的回复。有没有什么例子可以说明如何将转换器与绑定相关联?谷歌搜索了ReactiveUI conversionHint,但在前三页上找不到任何内容。您需要实现
IBindingTypeConverter
,并通过Locator.CurrentMutable.register注册它。我知道,这部分。我要寻找的是转换器代码的示例,以及它应该如何与视图集成。这一部分还不清楚。另一方面,我喜欢WPF方法,其中有Convert和CovertBack。我认为其意图更加明确。这里有一个简单的例子:断开的链接:-(
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));
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));
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
}
}
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;
}
}