2
我試圖在iOS中使用mvvmcross實現模式確認彈出窗口,其後跟着斯圖爾特的大綱this question。我得到試圖將InteractionRequest綁定到事件處理程序時出現以下異常:MvvmCross綁定到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();
});
}
我也回去和糾正原來的答案也 - 希望這會幫助其他人(謝謝!) – Stuart
謝謝斯圖爾特。綁定時我現在得到的類型不匹配。 – user2871327
'代碼'對象類型System.EventHandler無法轉換爲目標類型:System.EventHandler'1 [[QuickAssessment.Core.Interaction.InteractionRequestedEventArgs,MyProject,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]] \t在/ Developer/MonoTouch/Source/mono/mcs/class/corlib/System中的System.Reflection.Binder.ConvertValue(System.Object值,System.Type類型,System.Globalization.CultureInfo culture,Boolean exactMatch)[0x00027]。反射/捆紮機。cs:93 – user2871327