共用方式為


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

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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.

    Imports Microsoft.Phone.Tasks
    
    using Microsoft.Phone.Tasks;
    
    Imports Microsoft.Phone.Wallet
    Imports System.Windows.Media.Imaging
    
    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.

    Dim addWalletItemTask As AddWalletItemTask = New AddWalletItemTask()
    
    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.

    AddHandler addWalletItemTask.Completed, AddressOf addWalletItemTask_Completed
    
    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.

Wallet Item Type

Required Fields

PaymentInstrument

DisplayName, PaymentInstrumentKinds, Logo99x99, Logo159x159, Logo336x336

WalletTransactionItem

DisplayName, Logo99x99, Logo159x159, Logo336x336

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. ``` vb Dim card As PaymentInstrument = New PaymentInstrument("FabrikamBankCard") card.DisplayName = "Fabrikam Bank" card.CustomerName = "Chris Preston" card.ExpirationDate = New DateTime(2014, 12, 15) card.PaymentInstrumentKinds = PaymentInstrumentKinds.CreditAndDebit Dim bmp As BitmapImage = 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 card.Logo99x99 = New BitmapImage(New Uri("/images/BrandImageSmall.png", UriKind.RelativeOrAbsolute)) card.Logo159x159 = New BitmapImage(New Uri("/images/BrandImageMedium.png", UriKind.RelativeOrAbsolute)) card.Logo336x336 = New BitmapImage(New Uri("/images/BrandImageLarge.png", UriKind.RelativeOrAbsolute)) addWalletItemTask.Item = card addWalletItemTask.Show() ``` ``` csharp 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(); ```
  1. 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");
        } 
    }
    

See Also

Reference

AddWalletItemTask

Completed