【问题标题】:MvvmCross custom binding via INotifyPropertyChanged通过 INotifyPropertyChanged 的​​ MvvmCross 自定义绑定
【发布时间】:2013-06-19 11:58:22
【问题描述】:

我的情况与this 类似,但在联系中。尝试通过 INotifyPropertyChanged 处理。

我的代码如下:

set.Bind(txtSearch).For(x => x.Text).To(x => x.SearchText);

其中 txtSearch 是我自定义的 UISearchBar 包装器。所以,我不能从 MvxNotifyPropertyChanged 继承,因为我已经从 UIView 继承(包装器是视图)。

文本属性为:

 public string Text { get
    {
        return _search.Text;
    } set
    {
        _search.Text = value;
        RaisePropertyChanged(() => Text);
    }
}

我在 SearchBar 文本更改时触发它(有效)。

我还添加了以下内容:

    public event PropertyChangedEventHandler PropertyChanged;

    protected IMvxMainThreadDispatcher Dispatcher
    {
        get { return MvxMainThreadDispatcher.Instance; }
    }

    protected void InvokeOnMainThread(Action action)
    {
        if (Dispatcher != null)
            Dispatcher.RequestMainThreadAction(action);
    }
    protected void RaisePropertyChanged<T>(Expression<Func<T>> property)
    {
        var name = this.GetPropertyNameFromExpression(property);
        RaisePropertyChanged(name);
    }

    protected void RaisePropertyChanged(string whichProperty)
    {
        var changedArgs = new PropertyChangedEventArgs(whichProperty);
        RaisePropertyChanged(changedArgs);
    }

    protected void RaisePropertyChanged(PropertyChangedEventArgs changedArgs)
    {
        // check for subscription before going multithreaded
        if (PropertyChanged == null)
            return;

        InvokeOnMainThread(
            () =>
            {
                var handler = PropertyChanged;

                if (handler != null)
                    handler(this, changedArgs);
            });
    }

但是当一切都到达 RaisePropertyChanged 时,我看到 PropertyChanged 是空的(因此,似乎没有为我的对象订阅任何代码)。当然,这不会进一步通知。

我有类似的情况,但是从 MvxNotifyPropertyChanged 直接继承了一些对象,这似乎工作正常。这是否意味着,MvvmCross 只能处理此类对象,而不能处理通常使用 INotifyPropertyChanged 的​​对象?

谢谢!

【问题讨论】:

    标签: c# data-binding xamarin mvvmcross


    【解决方案1】:

    INotifyPropertyChanged 用于 ViewModel 端进行属性更改。

    在视图方面,MvvmCross 在 Windows 上使用 DependencyProperty 绑定,在 Xamarin 平台上使用 C# 方法、属性和事件。

    INotifyPropertyChanged 在 View 端默认不提供 - 因为没有现成的 View 对象支持INotifyPropertyChanged,所以在任何 MvvmCross View 平台中尝试绑定它是没有意义的。

    但是,绑定系统是可扩展的 - 因此,如果有人想要编写基于 INotifyPropertyChanged 的视图并希望在视图端包含自定义 INotifyPropertyChanged 绑定,那么他们可以按照类似于 In MvvmCross how do I do custom bind properties 的步骤进行操作以及以下从https://speakerdeck.com/cirrious/custom-bindings-in-mvvmcross链接的示例

    如果他们想为 View 端编写一个基于 INotifyPropertyChanged 的系统,那么我确信这可以使用自定义绑定方法来实现——但这不是我个人做过的事情。我希望这样的自定义绑定也适用于INotifyPropertyChangedMvxNotifyPropertyChanged(因为MvxNotifyPropertyChanged 实现了INotifyPropertyChanged) - 但我想这将由作者来决定它的机制。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-13
      • 2016-11-13
      相关资源
      最近更新 更多