Binding MvvmCross绑定到InteractionRequest

Binding MvvmCross绑定到InteractionRequest,binding,modal-dialog,mvvmcross,Binding,Modal Dialog,Mvvmcross,我正试图在iOS中使用mvvmcross实现一个模式确认弹出窗口,遵循Stuart在中的大纲。尝试将InteractionRequest绑定到事件处理程序时,出现以下异常: Problem seen during binding execution for to ConfirmationInteraction - problem TargetInvocationException: Exception has been thrown by the target of an invocation.

我正试图在iOS中使用mvvmcross实现一个模式确认弹出窗口,遵循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 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)));
        }
    }
}
在视图模型中,我们将请求设置为:

private InteractionRequest<Confirmation> _confirmCancelInteractionRequest = new InteractionRequest<Confirmation>();
    public IInteractionRequest ConfirmCancelInteractionRequest
    {
        get
        {
            return _confirmCancelInteractionRequest;
        }
    }
视图中的事件处理程序如下所示:

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();
...
set.Bind().For(my=>my.ConfirmationInteraction).To(customer=>customer.ConfirmCancelInteractionRequest);
set.Apply();
});
}

堆栈的空引用异常

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 
位于Cirrious.CrossCore.WeakSubscription.MvxWeakEventSubscription`2[System.Object,System.EventArgs]…ctor(System.Object source,System.Reflection.EventInfo sourceEventInfo,System.EventHandler`1 targetEventHandler)[0x00000]in:0
位于Cirrious.CrossCore.WeakSubscription.MvxGeneralEventSubscription..ctor(System.Object source、System.Reflection.EventInfo、System.EventHandler`1 EventHandler)[0x00000]中:0
在Cirrium.CrossCore.WeakSubscription.MvxWeakSubscriptionExtensionMethods.WeakSubscribe(System.Reflection.EventInfo EventInfo,System.Object source,System.EventHandler`1 EventHandler)[0x00000]中:0
仅当
\u confirmationInteraction.GetType().GetEvent(“Raise”)
返回
null

所以。。。我认为问题可能是您的活动被称为
Raised


如果有帮助:

  • MvvmCross的3.0.13版本中包含了一些用于交互的初始帮助-例如,see-我确实希望并期望在未来的版本中我们将以此为基础
  • 对于简单的“消息框”交互,@BrianChance提供了这个fab插件-

我也回去更正了原来的答案-希望这能帮助其他人(谢谢!)谢谢斯图尔特。绑定时,我现在得到的类型不匹配。
code
对象类型System.EventHandler无法转换为目标类型:System.EventHandler`1[[QuickAssessment.Core.Interaction.Interaction.Interaction RequestedEventTargets,MyProject,Version=1.0.0,Culture=neutral,PublicKeyToken=null]]at System.Reflection.Binder.ConvertValue(System.Object值、System.Type类型、System.Globalization.CultureInfo区域性、布尔精确匹配)[0x00027]在/Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/Binder.cs:93抱歉,在我完成之前,enter键似乎要提交注释。这看起来像是类型不匹配,尽管InteractionRequestedEventTargets确实继承自System.EventArgs。无论如何,我将看看前面提到的一些其他解决方案目前可能符合要求。再次感谢。消息会告诉您问题所在-请使用
doconfimation(objects s,eventargs args)
    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();
        }; 
    }
this.DelayBind(() =>
        {
            var set = this.CreateBindingSet<CustomerElement, CustomerViewModel>();
            ...
            set.Bind().For(my => my.ConfirmationInteraction).To(customer => customer.ConfirmCancelInteractionRequest);
            set.Apply();
        });
    }
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