locked
C#.NET DataContract to C++/CX DataContract RRS feed

  • Question

  • Hi Microsoft experts,

    I am a .NET developer and new to C++/Windows 8 App/Windows Phone 8.

    I am trying to port C# Windows 8 App to C++ Windows Phone 8 App.

    Please help porting C#.NET code to C++/CX:

    Here's the example code snippet (C#.NET):

    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Runtime.InteropServices.WindowsRuntime;
    using System.Runtime.Serialization;

    namespace PP.Checkout
     {
         /// <summary>
         /// Model that defines an item included in a checkout transaction.
         /// </summary>
         [DataContract]
         public sealed class Item
         {

            [DataMember]
             internal string name = string.Empty;

            [DataMember]
             internal string description = string.Empty;

            [DataMember]
             internal string id = string.Empty;

            [DataMember]
             internal string price = string.Empty;

            [DataMember]
             internal string tax = string.Empty;

            [DataMember]
             internal uint quantity = 1;

            [DataMember]
             internal Dictionary<string, string> metadata = new Dictionary<string, string>();

            /// <summary>
             /// The name of the item.
             /// </summary>
             public string Name
             {
                 get { return name; }
                 internal set { name = value; }
             }

            /// <summary>
             /// An item description.
             /// </summary>
             public string Description
             {
                 get { return description; }
                 internal set { description = value; }
             }

            /// <summary>
             /// An item ID
             /// </summary>
             public string ID
             {
                 get { return id; }
                 internal set { id = value; }
             }

            /// <summary>
             /// The price of the item. A positive number which cannot exceed $10,000 USD in any currency. It
             /// includes no currency symbol. It must have 2 decimal places, the decimal separator must be a
             /// period (.), and the optional thousands separator must be a comma (,).<br />
             /// NOTE: Currency for a given item is determined by the <see cref="PP.Checkout.Purchase"/>
             /// it is associated with.
             /// </summary>
             public string Price
             {
                 get { return price; }
                 internal set { price = value; }
             }

            /// <summary>
             /// The optional tax associated with this item. A positive number which cannot exceed $10,000 USD in any currency. It
             /// includes no currency symbol. It must have 2 decimal places, the decimal separator must be a
             /// period (.), and the optional thousands separator must be a comma (,).<br />
             /// NOTE: Currency for a given item is determined by the <see cref="PP.Checkout.Purchase"/>
             /// it is associated with.
             /// </summary>
             public string Tax
             {
                 get { return tax; }
                 internal set { tax = value; }
             }

            /// <summary>
             /// The quantity of this item involved in the given transaction. Defaults to 1.
             /// </summary>
             public uint Quantity
             {
                 get { return quantity; }
                 internal set { quantity = value; }
             }

            /// <summary>
             /// Extra information associated with this item.<br/>
             /// The following keys and values are currently supported.
             /// <list type="table">
             ///     <term>type: "Digital"</term>
             ///     <description>Identifies the current item as a Digital Goods item. Not setting, or setting this value
             ///     to anything other than Digital uses the "Physical" goods item category.</description>
             /// </list>
             /// </summary>
             public IDictionary<string, string> Metadata
             {
                 get { return new ReadOnlyDictionary<string, string>(metadata); }
             }

            /// <summary>
             /// Allows extra information to be associated with this item.<br/>
             /// The following keys and values are currently supported.
             /// <list type="table">
             ///     <term>type: "Digital"</term>
             ///     <description>Identifies the current item as a Digital Goods item. Not setting, or setting this value
             ///     to anything other than Digital uses the "Physical" goods item category.</description>
             /// </list>
             /// </summary>
             /// <param name="key">the name of the metadata (e.g. "type")</param>
             /// <param name="value">the value of the associated parameter (e.g. "Digital")</param>
             internal void AddMetadata(string key, string value)
             {
                 metadata.Add(key, value);
             }
         }
     }

    Here's the attempted C++ code in vain (Please pardon my C++ ignorance: Error 1 error C1114: 'c:\windows\microsoft.net\framework\v4.0.30319\system.runtime.serialization.dll' : WinRT does not support #using of a managed assembly):

    //.h file code:

    using namespace System;
    using namespace System::Collections::Generic;
    using namespace System::Collections::ObjectModel;
    using namespace

    System::Runtime::InteropServices::WindowsRuntime;
    using namespace System::Runtime::Serialization;

    namespace PP
     {
      namespace Checkout
      {
       /// <summary>
       /// Model that defines an item included in a checkout transaction.
       /// </summary>
       [DataContract]
       public ref class Item sealed
       {

      internal:
        [DataMember]
        String ^name;

       [DataMember]
        String ^description;

       [DataMember]
        String ^id;

       [DataMember]
        String ^price;

       [DataMember]
        String ^tax;

       [DataMember]
        UInt32 quantity;

       [DataMember]
        Dictionary<String^, String^> ^metadata;

       /// <summary>
        /// The name of the item.
        /// </summary>
       public:
        property String ^Name
        {
         String ^get();
        internal:
         void set(String ^value);
        }

       /// <summary>
        /// An item description.
        /// </summary>
        property String ^Description
        {
         String ^get();
        internal:
         void set(String ^value);
        }

       /// <summary>
        /// An item ID
        /// </summary>
        property String ^ID
        {
         String ^get();
        internal:
         void set(String ^value);
        }

       /// <summary>
        /// The price of the item. A positive number which cannot exceed $10,000 USD in any currency. It
        /// includes no currency symbol. It must have 2 decimal places, the decimal separator must be a
        /// period (.), and the optional thousands separator must be a comma (,).<br />
        /// NOTE: Currency for a given item is determined by the <see cref="PP.Checkout.Purchase"/>
        /// it is associated with.
        /// </summary>
        property String ^Price
        {
         String ^get();
        internal:
         void set(String ^value);
        }

       /// <summary>
        /// The optional tax associated with this item. A positive number which cannot exceed $10,000 USD in any currency. It
        /// includes no currency symbol. It must have 2 decimal places, the decimal separator must be a
        /// period (.), and the optional thousands separator must be a comma (,).<br />
        /// NOTE: Currency for a given item is determined by the <see cref="PP.Checkout.Purchase"/>
        /// it is associated with.
        /// </summary>
        property String ^Tax
        {
         String ^get();
        internal:
         void set(String ^value);
        }

       /// <summary>
        /// The quantity of this item involved in the given transaction. Defaults to 1.
        /// </summary>
        property UInt32 Quantity
        {
         UInt32 get();
        internal:
         void set(UInt32 value);
        }

       /// <summary>
        /// Extra information associated with this item.<br/>
        /// The following keys and values are currently supported.
        /// <list type="table">
        ///     <term>type: "Digital"</term>
        ///     <description>Identifies the current item as a Digital Goods item. Not setting, or setting this value
        ///     to anything other than Digital uses the "Physical" goods item category.</description>
        /// </list>
        /// </summary>
        property IDictionary<String^, String^> ^Metadata
        {
         IDictionary<String^, String^> ^get();
        }

       /// <summary>
        /// Allows extra information to be associated with this item.<br/>
        /// The following keys and values are currently supported.
        /// <list type="table">
        ///     <term>type: "Digital"</term>
        ///     <description>Identifies the current item as a Digital Goods item. Not setting, or setting this value
        ///     to anything other than Digital uses the "Physical" goods item category.</description>
        /// </list>
        /// </summary>
        /// <param name="key">the name of the metadata (e.g. "type")</param>
        /// <param name="value">the value of the associated parameter (e.g. "Digital")</param>
       internal:
        void AddMetadata(String ^key, String ^value);

      private:
        void InitializeInstanceFields();

    public:
        Item()
        {
         InitializeInstanceFields();
        }
       };
      }
     }

    //.cpp file code:

     using namespace System;
     using namespace System::Collections::Generic;
     using namespace System::Collections::ObjectModel;
     using namespace System::Runtime::InteropServices::WindowsRuntime;
     using namespace System::Runtime::Serialization;

    namespace PP
     {
      namespace Checkout
      {

      String ^Item::Name::get()
       {
        return name;
       }

      void Item::Name::set(String ^value)
       {
        name = value;
       }

      String ^Item::Description::get()
       {
        return description;
       }

      void Item::Description::set(String ^value)
       {
        description = value;
       }

      String ^Item::ID::get()
       {
        return id;
       }

      void Item::ID::set(String ^value)
       {
        id = value;
       }

      String ^Item::Price::get()
       {
        return price;
       }

      void Item::Price::set(String ^value)
       {
        price = value;
       }

      String ^Item::Tax::get()
       {
        return tax;
       }

      void Item::Tax::set(String ^value)
       {
        tax = value;
       }

      UInt32 Item::Quantity::get()
       {
        return quantity;
       }

      void Item::Quantity::set(UInt32 value)
       {
        quantity = value;
       }

      IDictionary<String^, String^> ^Item::Metadata::get()
       {
        return gcnew ReadOnlyDictionary<String^, String^> (metadata);
       }

      void Item::AddMetadata(String ^key, String ^value)
       {
        metadata->Add(key, value);
       }

      void Item::InitializeInstanceFields()
       {
        name = "";
        description = "";
        id = "";
        price = "";
        tax = "";
        quantity = 1;
        metadata = gcnew Dictionary<String^, String^>();
       }
      }
     }

    Please help with the corresponding C++/CX code.


    Thanks





    • Edited by recherche Sunday, May 5, 2013 6:00 PM typo
    Friday, May 3, 2013 6:52 PM

Answers

All replies