How to use the add Wallet item task for Windows Phone 8

May 10, 2014

Applies to: Windows Phone 8 and Windows Phone Silverlight 8.1 only

Use the add Wallet item task to launch the Wallet application and allow the user to add the supplied item to his or her Wallet. If the user completes the task, an event is raised and the event handler receives an TaskResult value indicating whether the item was successfully added to the Wallet. This is the only way for apps to add a PaymentInstrument or a WalletTransactionItem. To add a Deal, use the SaveAsync() method. For more information on creating applications that use Wallet, see Wallet for Windows Phone 8.

By using Choosers, you help provide a consistent user experience throughout the Windows Phone platform. For more information, see Launchers and Choosers for Windows Phone 8.

To use the add Wallet item task

  1. Add the following statement to your code.

    
    using Microsoft.Phone.Tasks;
    
    
    
    
    using Microsoft.Phone.Wallet;
    using System.Windows.Media.Imaging;
    
    
    
  2. Declare the task object. It must have page scope, so declare it in your page before the constructor.

    
    AddWalletItemTask addWalletItemTask = new AddWalletItemTask();
    
    
    
  3. Add the following code to your page constructor. This code initializes the task object, and identifies the method to run after the user completes the task.

    
    addWalletItemTask.Completed += addWalletItemTask_Completed;
    
    
    
  4. Instantiate one of the supported objects that inherit from WalletItem and set the Item property of the AddWalletItemTask object. Only PaymentInstrument and WalletTransactionItem objects are supported for this chooser. The following properties must be set before calling AddWalletItemTask.Show or an InvalidOperationException will be thrown.

    Add the following code to your application wherever you need it, such as in a button click event. To test this procedure, you can put the code in the page constructor. This is the code to launch the task.

    
    
    
                PaymentInstrument card = new PaymentInstrument("FabrikamBankCard");
                card.DisplayName = "Fabrikam Bank";
                card.CustomerName = "Chris Preston";
                card.ExpirationDate = new DateTime(2014, 12, 15);
                card.PaymentInstrumentKinds = PaymentInstrumentKinds.CreditAndDebit;
    
                BitmapImage bmp = new BitmapImage(new Uri("/images/BrandImageSmall.png", UriKind.RelativeOrAbsolute));
                bmp.CreateOptions = BitmapCreateOptions.None;
                card.Logo99x99 = bmp;
    
                bmp = new BitmapImage(new Uri("/images/BrandImageMedium.png", UriKind.RelativeOrAbsolute));
                bmp.CreateOptions = BitmapCreateOptions.None;
                card.Logo159x159 = bmp;
    
                bmp = new BitmapImage(new Uri("/images/BrandImageLarge.png", UriKind.RelativeOrAbsolute));
                bmp.CreateOptions = BitmapCreateOptions.None;
                card.Logo336x336 = bmp;           
    
                addWalletItemTask.Item = card;
    
                addWalletItemTask.Show();
    
    
    
    
  5. Add the code for the completed event handler to your page. This code runs after the user completes the task. The result is an AddWalletItemResult object that contains the name and address of the contact.

    
    void addWalletItemTask_Completed(object sender, AddWalletItemResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            MessageBox.Show(e.Item.Id + " was added to your Wallet");
        } 
    }
    
    
    

Show:
© 2014 Microsoft