FieldBinding和弱引用

时间:2014-10-10 17:21:42

标签: binding mvvmcross weak-references

我愿意使用通知属性更改机制的INC<T>版本,它比Property + Field + RaisePropertyChanged版本更简洁。

但是,我要说这个ViewModel:

public abstract class PageViewModel : MvxViewModel
{
    /// <summary>
    ///     The is loading
    /// </summary>
    public readonly INC<bool> IsLoading = new NC<bool>();

    /// <summary>
    ///     The subtitle
    /// </summary>
    public readonly INC<string> Subtitle = new NC<string>();

    /// <summary>
    ///     The title
    /// </summary>
    public readonly INC<string> Title = new NC<string>();

现在,假设我在我的Android Activity中,我想订阅这些属性:

public partial class MainView : IFragmentHost
{
    private void Subscribe(PageViewModel viewModel)
    {
        viewModel.Title.Changed += (xx) => Whatever;
    }

在第二个问题上,WeakSubscribe对他们来说会很好:

 viewModel.Title.WeakSubscri...
嗯,很奇怪,我没有自动完成。

让我们看看MvxWeakSubscriptionExtensionMethods:

public static class MvxWeakSubscriptionExtensionMethods
{
    public static MvxNotifyPropertyChangedEventSubscription WeakSubscribe(this INotifyPropertyChanged source, EventHandler<PropertyChangedEventArgs> eventHandler)
    {
         return new MvxNotifyPropertyChangedEventSubscription(source, eventHandler);
    }

    public static MvxValueEventSubscription<T> WeakSubscribe<T>(this EventInfo eventInfo, object source, EventHandler<MvxValueEventArgs<T>> eventHandler)
    {
         return new MvxValueEventSubscription<T>(source, eventInfo, eventHandler);
    }

现在是INC<T>

public interface INC<T> : INotifyChange<T>, INotifyChange

那么,有没有办法弱订阅INC<T>属性?

1 个答案:

答案 0 :(得分:0)

插件本身使用弱引用进行绑定:

  IDisposable _subscription = NotifyChangeEventInfo.WeakSubscribe(_notifyChange, NotifyChangeOnChanged); 

其中NotifyChangeOnChanged有签名:

  protected abstract void NotifyChangeOnChanged(object sender, EventArgs eventArgs); 

来自https://github.com/MvvmCross/MvvmCross/blob/3.2/Plugins/Cirrious/FieldBinding/Cirrious.MvvmCross.Plugins.FieldBinding/MvxNotifyChangeFieldSourceBinding.cs#L38