2013-10-11 7 views
2

Je tente d'implémenter une popup de confirmation modale en utilisant mvvmcross dans iOS, en suivant le plan de Stuart au this question. Je reçois l'exception suivante lors d'une tentative de lier le InteractionRequest au gestionnaire de l'événement:MvvmCross bind to InteractionRequest

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

L'exception intérieure est:

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 

La plupart de la plomberie est identique à la question de débordement de pile référencé ci-dessus, mais Je vais poster ici pour être complet:

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; 
    } 
} 

Le 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))); 
     } 
    } 
} 

La classe de confirmation:

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

Dans le modèle de vue, nous avons créé la demande comme si:

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

dans la vue, nous avons créé l'abonnement événement:

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); 

     } 
    } 

Le gestionnaire d'événements dans la vue ressemble à ceci:

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(); 
     }; 
    } 

Enfin la liaison, qui est à l'intérieur du constructeur pour la vue. La vue est un élément de dialogue MT, d'où le DelayBind():

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

Répondre

0

L'exception de référence null avec la pile

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 

ne peut se produire si _confirmationInteraction.GetType().GetEvent ("Raise") retours null

Alors ... Je pense que le problème pourrait être que votre événement est appelé Raised


Si elle aide:

+0

Je suis aussi allé en arrière et corrigées la réponse originale aussi - j'espère que cela aidera les autres (Merci!) – Stuart

+0

Merci Stuart. Ce que je reçois maintenant une incompatibilité de type lors de la liaison. – user2871327

+0

'code' Type d'objet System.EventHandler ne peut pas être converti en type cible: System.EventHandler'1 [[QuickAssessment.Core.Interaction.InteractionRequestedEventArgs, MyProject, Version = 1.0.0.0, Culture = neutre, PublicKeyToken = null]] \t à System.Reflection.Binder.ConvertValue (valeur System.Object, type System.Type, culture System.Globalization.CultureInfo, Boolean exactMatch) [0x00027] dans/Developer/MonoTouch/Source/mono/mcs/class/corlib/System. Réflexion/Binder.cs: 93 – user2871327