【问题标题】:ReactiveUI 6.0 and WinForms bindingReactiveUI 6.0 和 WinForms 绑定
【发布时间】: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

给定this extension

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


【解决方案1】:

最简单的方法是使用 ReactiveUI 绑定 - 在您的 Form 上实现 IViewFor&lt;TViewModel&gt;,然后您将获得几个新方法,例如 BindOneWayBind

查看https://github.com/reactiveui/ReactiveUI/blob/docs/docs/basics/bindings.md了解更多信息

【讨论】:

  • 保罗,感谢您的回复。是否有任何示例如何将转换器与绑定相关联?谷歌搜索 ReactiveUI 转换提示,但在前三页找不到任何内容。
  • 你需要实现IBindingTypeConverter并通过Locator.CurrentMutable.Register注册
  • 我明白,那部分。我正在寻找的是转换器代码的示例,以及它应该如何与视图集成。那部分不清楚。另外,我喜欢 WPF 方法,其中有 Convert 和 CovertBack。我认为意图更加清晰。
  • 链接断开。 :-(
猜你喜欢
  • 2016-09-23
  • 1970-01-01
  • 1970-01-01
  • 2017-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多