MvvmCross绑定到InteractionRequest

时间:2013-10-11 15:03:05

标签: binding modal-dialog mvvmcross

我正试图在iOS中使用mvvmcross实现模态确认弹出窗口,遵循this question中的Stuart大纲。尝试将InteractionRequest绑定到事件处理程序时,我收到以下异常:

Problem seen during binding execution for to ConfirmationInteraction - problem
TargetInvocationException: Exception has been thrown by the target of an invocation.

内部例外是:

ArgumentNullException: missing source event info in MvxWeakEventSubscription
Parameter name: sourceEventInfo
at Cirrious.CrossCore.WeakSubscription.MvxWeakEventSubscription`2[System.Object,System.EventArgs]..ctor (System.Object source, System.Reflection.EventInfo sourceEventInfo, System.EventHandler`1 targetEventHandler) [0x00000] in <filename unknown>:0 
at Cirrious.CrossCore.WeakSubscription.MvxGeneralEventSubscription..ctor (System.Object source, System.Reflection.EventInfo eventInfo, System.EventHandler`1 eventHandler) [0x00000] in <filename unknown>:0 
at Cirrious.CrossCore.WeakSubscription.MvxWeakSubscriptionExtensionMethods.WeakSubscribe (System.Reflection.EventInfo eventInfo, System.Object source, System.EventHandler`1 eventHandler) [0x00000] in <filename unknown>:0 

大多数管道与上面引用的堆栈溢出问题相同,但为了完整性,我将在此处发布:

public class InteractionRequestedEventArgs : EventArgs
{
    public Action Callback 
    { 
        get; 
        private set; 
    }

    public object Context 
    { 
        get; 
        private set; 
    }

    public InteractionRequestedEventArgs(object context, Action callback)
    {
        Context = context;
        Callback = callback;
    }
}

InteractionRequest:

public class InteractionRequest<T> : IInteractionRequest
{
    public event EventHandler<InteractionRequestedEventArgs> Raised;

    public void Raise(T context, Action<T> callback)
    {
        var handler = this.Raised;
        if (handler != null)
        {
            handler(
                this, 
                new InteractionRequestedEventArgs(
                context, 
                () => callback(context)));
        }
    }
}

确认类:

public class Confirmation
{
    public string Message { get; private set; }
    public bool Confirmed { get; set; }
    public Confirmation(string message)
    {
        Message = message;
    }
}

在视图模型中,我们将请求设置为:

private InteractionRequest<Confirmation> _confirmCancelInteractionRequest = new InteractionRequest<Confirmation>();
    public IInteractionRequest ConfirmCancelInteractionRequest
    {
        get
        {
            return _confirmCancelInteractionRequest;
        }
    }

在视图中我们设置了事件订阅:

    private MvxGeneralEventSubscription _confirmationSubscription;
    private IInteractionRequest _confirmationInteraction;
    public IInteractionRequest ConfirmationInteraction
    {
        get { 
            return _confirmationInteraction; 
        }
        set
        {
            if (_confirmationInteraction == value)
                return;
            if (_confirmationSubscription != null)
                _confirmationSubscription.Dispose();
            _confirmationInteraction = value;
            if (_confirmationInteraction != null)
                _confirmationSubscription = _confirmationInteraction
                    .GetType ()
                    .GetEvent ("Raise")
                    .WeakSubscribe(_confirmationInteraction, DoConfirmation);

        }
    }

视图中的事件处理程序如下所示:

private void DoConfirmation(object s, EventArgs args)
    {
        var iArgs = (InteractionRequestedEventArgs)args;
        var confirmation = (Confirmation)iArgs.Context;

        var alert = new UIAlertView(); 
        alert.Title = "Bazinga"; 
        alert.Message = confirmation.Message; 

        alert.AddButton("Yes"); 
        alert.AddButton("No"); 

        alert.Clicked += (sender, e) => { 
            var alertView = sender as UIAlertView; 
                            // do something with the event
            iArgs.Callback();
        }; 
    }

最后是绑定,它位于视图的构造函数内部。视图是MT对话框元素,因此是DelayBind():

this.DelayBind(() =>
        {
            var set = this.CreateBindingSet<CustomerElement, CustomerViewModel>();
            ...
            set.Bind().For(my => my.ConfirmationInteraction).To(customer => customer.ConfirmCancelInteractionRequest);
            set.Apply();
        });
    }

1 个答案:

答案 0 :(得分:0)

带有堆栈的空引用异常

at Cirrious.CrossCore.WeakSubscription.MvxWeakEventSubscription`2[System.Object,System.EventArgs]..ctor (System.Object source, System.Reflection.EventInfo sourceEventInfo, System.EventHandler`1 targetEventHandler) [0x00000] in <filename unknown>:0 
at Cirrious.CrossCore.WeakSubscription.MvxGeneralEventSubscription..ctor (System.Object source, System.Reflection.EventInfo eventInfo, System.EventHandler`1 eventHandler) [0x00000] in <filename unknown>:0 
at Cirrious.CrossCore.WeakSubscription.MvxWeakSubscriptionExtensionMethods.WeakSubscribe (System.Reflection.EventInfo eventInfo, System.Object source, System.EventHandler`1 eventHandler) [0x00000] in <filename unknown>:0 

只有在_confirmationInteraction.GetType ().GetEvent ("Raise")返回null

时才会出现

所以...我认为问题可能是你的事件被称为Raised


如果有帮助: