C# 通过INotifyPropertyChanged进行MvvmCross自定义绑定

C# 通过INotifyPropertyChanged进行MvvmCross自定义绑定,c#,data-binding,xamarin,mvvmcross,C#,Data Binding,Xamarin,Mvvmcross,我也有类似的情况,但仍保持联系。不过,试图通过InotifyProperty解决这一问题的做法发生了变化 我的代码如下: set.Bind(txtSearch).For(x => x.Text).To(x => x.SearchText); public event PropertyChangedEventHandler PropertyChanged; protected IMvxMainThreadDispatcher Dispatcher {

我也有类似的情况,但仍保持联系。不过,试图通过InotifyProperty解决这一问题的做法发生了变化

我的代码如下:

set.Bind(txtSearch).For(x => x.Text).To(x => x.SearchText);
    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);
            });
    }
其中txtSearch是我的UISearchBar的自定义包装。因此,我不能从MvxNotifyPropertyChanged继承,因为我已经从UIView继承了(包装器是view)

文本属性为:

 public string Text { get
    {
        return _search.Text;
    } set
    {
        _search.Text = value;
        RaisePropertyChanged(() => Text);
    }
}
我在搜索栏上启动它,改变文本(这是有效的)

我还添加了以下内容:

set.Bind(txtSearch).For(x => x.Text).To(x => x.SearchText);
    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);
            });
    }
public event propertychangedventhandler PropertyChanged;
受保护的IMvxMainThreadDispatcher调度程序
{
获取{return MvxMainThreadDispatcher.Instance;}
}
受保护的void InvokeOnMainThread(操作)
{
if(调度程序!=null)
Dispatcher.RequestMainThreadAction(操作);
}
受保护的void RaisePropertyChanged(表达式属性)
{
var name=this.GetPropertyNameFromExpression(属性);
RaisePropertyChanged(名称);
}
受保护的void raiseProperty已更改(字符串whichProperty)
{
var changedArgs=新属性ChangedEventArgs(whichProperty);
RaisePropertyChanged(变更号);
}
受保护的void raiseProperty已更改(PropertyChangedEventArgs changedArgs)
{
//在进入多线程之前检查订阅
if(PropertyChanged==null)
返回;
调用主线程(
() =>
{
var handler=PropertyChanged;
if(处理程序!=null)
处理者(本,变更码);
});
}
但当一切都变为RaisePropertyChanged时,我看到PropertyChanged是空的(因此,似乎没有为我的对象订阅任何代码)。当然,这不会进一步发出通知

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


谢谢大家!

INotifyPropertyChanged
在ViewModel端用于属性更改

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


默认情况下,视图端不提供INotifyPropertyChanged——因为没有现成的视图对象支持INotifyPropertyChanged,所以在任何MvvmCross View平台内尝试绑定到它是没有意义的

但是,绑定系统是可扩展的-因此,如果任何人想要编写基于
inotifPropertyChanged
的视图,并且想要为视图端包含自定义
inotifPropertyChanged
绑定,那么他们可以按照类似于的步骤和以下链接的示例来完成

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