locked
C++/CX Generic Delegates

    Question

  • Hola,

    Am a .NET developer and new to C++/CX. How do you create template event handlers and delegates (generic event handlers and delegates) in C++/CX. There aren't enough examples on C++/CX Templates in MSDN.

    Please help .NET developers (like me) to pitch in to C++/CX. And I love Modern Cpp and Windows Store apps.

    I would appreciate if you help me with code rather than referring me to Stack Overflow etc.

    This is an earnest request because "Googling with Bing" didn't help!!!

    Here's the C# Code Snippet:

     

    public sealed class BuyNow { public event EventHandler<StartEventArgs> Start; public event EventHandler<AuthEventArgs> Auth; public event EventHandler<CancelEventArgs> Cancel; public event EventHandler<ErrorEventArgs> Error; public event EventHandler<CompleteEventArgs> Complete; private delegate T Generator<T>(); private void OnStart() { Generator<StartEventArgs> generator = () => { return new StartEventArgs(); }; Emit(generator); } private void OnAuth(string token) { Generator<AuthEventArgs> generator = () => { return new AuthEventArgs(token); }; Emit(generator); } private void OnCancel(string token) { Generator<CancelEventArgs> generator = () => { return new CancelEventArgs(token); }; Emit(generator); } private void OnError(ErrorType type, string name, string message) { OnError(type, name, message, null); } private void OnError(ErrorType type, string name, string message, string detail) { Generator<ErrorEventArgs> generator = () => { return new ErrorEventArgs(type, name, message, detail); }; Emit<ErrorEventArgs>(generator); } private void OnComplete(string token, string transactionId, string payerId) { Generator<CompleteEventArgs> generator = () => { return new CompleteEventArgs(token, transactionId, payerId); }; EmitAndComplete<CompleteEventArgs>(generator); } private void EmitAndComplete<T>(Generator<T> generator) { } private void Emit<T>(Generator<T> generator) { TypeInfo info = this.GetType().GetTypeInfo(); TypeInfo genericType = typeof(T).GetTypeInfo(); // Get all the available events foreach (EventInfo eventInfo in info.DeclaredEvents) { // Get the type arguments for the current event (really, there should only be one) Type[] typeParameters = eventInfo.EventHandlerType.GetTypeInfo().GenericTypeArguments; foreach (Type parameterType in typeParameters) { // If the type parameter matches the type we're trying to emit... if (parameterType.GetTypeInfo().IsAssignableFrom(genericType)) { // Get the field of the same name so we can access the token table FieldInfo eventField = info.GetDeclaredField(eventInfo.Name); EventRegistrationTokenTable<EventHandler<T>> tokenTable = eventField.GetValue(this) as EventRegistrationTokenTable<EventHandler<T>>; if (tokenTable != null) { // Events are registered, so generate our event args, invoke, and break out EventHandler<T> handler = tokenTable.InvocationList; T eventArgs = generator(); handler.DynamicInvoke(new object[] { this, eventArgs }); goto complete; } } } } complete: { } } }

    public enum ErrorType
    {
        UNKNOWN = 0,
        HTTP = 1,
        API = 2,
        NETWORK = 3
    }

    public sealed class StartEventArgs
    {

    }

    public sealed class AuthEventArgs
    {
        public string Token { get; private set; }

        public AuthEventArgs(string token)
        {
            Token = token;
        }
    }

    public sealed class CancelEventArgs
    {
        public string Token { get; private set; }

        public CancelEventArgs(string token)
        {
            Token = token;
        }
    }

    public sealed class ErrorEventArgs
    {
        public ErrorType Type { get; private set; }

        public string Code { get; private set; }

        public string Message { get; private set; }

        public string Detail { get; private set; }

        public ErrorEventArgs(ErrorType type, string name, string message, string detail)
        {
            Type = type;
            Code = name;
            Message = message;
            Detail = detail;
        }
    }

    public sealed class CompleteEventArgs
    {
        public string Token { get; private set; }
        public string TransactionID { get; private set; }
        public string PayerID { get; private set; }

        public CompleteEventArgs(string token, string transactionId, string payerId)
        {
            Token = token;
            TransactionID = transactionId;
            PayerID = payerId;
        }
    }

     

    Referring to MSDN http://msdn.microsoft.com/en-us/library/windows/apps/hh755799.aspx:

    I tried the following:

    ref class BuyNow; public delegate void EventHandler(BuyNow^ sender, Platform::String^ s); public ref class BuyNow sealed { private: static const Uri^ PROXY_URI; static const Uri^ SANDBOX_PROXY_URI; Boolean buyNowUseSandbox; Boolean buyNowHasError; Purchase^ purchaseRequest; public: BuyNow(String^ merchantID); virtual ~BuyNow(void); BuyNow(){} event EventHandler^ Start; event EventHandler^ Auth; event EventHandler^ Cancel; event EventHandler^ Error; event EventHandler^ Complete; void OnStart() { //Do something.... // ...then fire the event: Start(this, L"Event Started."); } void OnAuth() { //Do something.... // ...then fire the event: Auth(this, L"Event Authorized."); } void OnCancel() { //Do something.... // ...then fire the event: Cancel(this, L"Event Cancelled."); } void OnError() { //Do something.... // ...then fire the event: Error(this, L"Event Error."); } void OnComplete() { //Do something.... // ...then fire the event: Complete(this, L"Event Completed."); } };

    public enum class ErrorType
    {
     UNKNOWN = 0,
     HTTP = 1,
     API = 2,
     NETWORK = 3
    };

    public ref class StartEventArgs sealed
    {

    };

    public ref class AuthEventArgs sealed
    {  
     private:
      String^ authEventArgsToken;

     public:
      AuthEventArgs(String^ token)
      {
       Token = token;
      }
      property String^ Token
      {
       String^ get()
       {
        return authEventArgsToken;
       }
       void set(String^ value)
       {
        authEventArgsToken = value;
       }
      }
    };

    public ref class CancelEventArgs sealed
    {  
     private:
      String^ cancelEventArgsToken;

     public:
      CancelEventArgs(String^ token)
      {
       Token = token;
      }
      property String^ Token
      {
       String^ get()
       {
        return cancelEventArgsToken;
       }
       void set(String^ value)
       {
        cancelEventArgsToken = value;
       }
      }
    };

    public ref class ErrorEventArgs sealed
    {  
     private:
      ErrorType errorEventArgsType;
      String^ errorEventArgsCode;
      String^ errorEventArgsMessage;
      String^ errorEventArgsDetail;

     public:
      ErrorEventArgs(ErrorType type, String^ name, String^ message, String^ detail)
      {
        Type = type;
        Code = name;
        Message = message;
        Detail = detail;
      }
      property ErrorType Type
      {
       ErrorType get()
       {
        return errorEventArgsType;
       }
       void set(ErrorType value)
       {
        errorEventArgsType = value;
       }
      }

      property String^ Code
      {
       String^ get()
       {
        return errorEventArgsCode;
       }
       void set(String^ value)
       {
        errorEventArgsCode = value;
       }
      }

      property String^ Message
      {
       String^ get()
       {
        return errorEventArgsMessage;
       }
       void set(String^ value)
       {
        errorEventArgsMessage = value;
       }
      }

      property String^ Detail
      {
       String^ get()
       {
        return errorEventArgsDetail;
       }
       void set(String^ value)
       {
        errorEventArgsDetail = value;
       }
      }
    };


     

    Thursday, May 23, 2013 10:55 AM

All replies