使用WR
l.以下是有关如何购买应用内购买的示
例:
@H_
618_23@#include <window
s.h>
#include <Window
s.service
s.Store.h>
#include <wr
l.h>
using namespace ABI::Windows::Foundation;
using namespace ABI::Windows::
services::Store;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
#define
checkHr(hr) do
{ if (
Failed(hr)) __debugbreak(
); } while
(false)
const wchar_t kItemFriendlyName[] = L"10 coins";
const wchar_t kItemStor
EID[] = L"ten_coins";
void OnPurcha@L_
674_11@perationDone(IAsyncOperation<StorePurchaseResult*>* operation,AsyncStatus status
);
void Purchase10Coins()
{
ComPtr<IStoreCo
ntextStatics> storeCo
ntextStatics;
auto hr = RoGetActivationFactory(H
StringReference(L"Window
s.service
s.Store.StoreCo
ntext").Get(),__uuidof(storeCo
ntextStatics),&storeCo
ntextStatics
);
checkHr(hr
);
ComPtr<IStoreCo
ntext> storeCo
ntext;
hr = storeCo
ntextStatics->GetDefault(&storeCo
ntext
);
checkHr(hr
);
ComPtr<IStorePurchasePropertiesFactory> purchasePropertiesFactory;
hr = RoGetActivationFactory(H
StringReference(L"Window
s.service
s.Store.StorePurchaseProperties").Get(),__uuidof(purchasePropertiesFactory),&purchasePropertiesFactory
);
checkHr(hr
);
ComPtr<IStorePurchaseProperties> purchaseProperties;
hr = purchasePropertiesFactory->Create(H
StringReference(kItemFriendly
Name).Get(),&purchaseProperties
);
checkHr(hr
);
ComPtr<IAsyncOperation<StorePurchaseResult*>> purcha@L_
674_11@peration;
hr = storeCo
ntext->
requestPurchaseWithPurchasePropertiesAsync(H
StringReference(kItemStor
EID).Get(),purchasePropertie
s.Get(),&purcha@L_
674_11@peration
);
checkHr(hr
);
// Change the following line to call Call
BACk<IAsyncOperationCompletedHandler<StorePurchaseResult*>> if
you want the call
BACk to happen
BACk on the UI thread
// Implemen
Ting FtmBase allows it to fire on the thread the operation finished
auto onCompletedCall
BACk = Call
BACk<Implements<RuntimeClassFlags<ClassicCom>,IAsyncOperationCompletedHandler<StorePurchaseResult*>,FtmBase>>(
[](IAsyncOperation<StorePurchaseResult*>* operation,AsyncStatus status)
{
OnPurcha@L_
674_11@perationDone(operation,status
);
return S_OK;
}
);
hr = purcha@L_
674_11@peration->put_Completed(onCompletedCall
BACk.Get()
);
checkHr(hr
);
}
void OnPurcha@L_
674_11@perationDone(IAsyncOperation<StorePurchaseResult*>* operation,AsyncStatus status)
{
if (status
!= AsyncStatus::Completed)
{
// It
Failed for some reason. Find out why.
ComPtr<IAsyncInfo> asyncInfo;
auto hr = operation->QueryInterface(__uuidof(asyncInfo),&asyncInfo
);
checkHr(hr
);
HRESULT errorCode;
hr = asyncInfo->get_ErrorCode(&errorCod
E);
checkHr(hr
);
// Do something with the errorCode
// Return once error is handled
return;
}
ComPtr<IStorePurchaseResult> purchaseResult;
auto hr = operation->GetResults(&purchaseResult
);
checkHr(hr
);
StorePurchaseStatus purchaseStatus;
hr = purchaseResult->get_Status(&purchaseStatus
);
checkHr(hr
);
switch (purchaseStatus)
{
case StorePurchaseStatus_Succeeded:
case StorePurchaseStatus_AlreadyPurchased:
//
success. Product was purchased
break;
case StorePurchaseStatus_NotPurchased:
// User canceled the purchase
break;
case StorePurchaseStatus_NetworkError:
// The device
Could not reach windows store
break;
case StorePurchaseStatus_ServerError:
// Something broke on the server
break;
}
}
以下是检查应用程序是否正在试用的方法:
@H_
618_23@void
checkI
Strial(std::function<void(bool)> onCompleted)
{
ComPtr<IStoreCo
ntextStatics> storeCo
ntextStatics;
auto hr = RoGetActivationFactory(H
StringReference(L"Window
s.service
s.Store.StoreCo
ntext").Get(),&storeCo
ntextStatics
);
checkHr(hr
);
ComPtr<IStoreCo
ntext> storeCo
ntext;
hr = storeCo
ntextStatics->GetDefault(&storeCo
ntext
);
checkHr(hr
);
ComPtr<IAsyncOperation<StoreAppLicense*>> getLicen@L_
674_11@peration;
hr = storeCo
ntext->GetAppLicenseAsync(&getLicen@L_
674_11@peration
);
checkHr(hr
);
hr = getLicen@L_
674_11@peration->put_Completed(Call
BACk<Implements<RuntimeClassFlags<ClassicCom>,IAsyncOperationCompletedHandler<StoreAppLicense*>,FtmBase>>(
[onCompleted
{ std::move(onCompleted) }](IAsyncOperation<StoreAppLicense*>* operation,AsyncStatus status)
{
if (status
!= AsyncStatus::Completed)
{
// It
Failed for some reason. Find out why.
ComPtr<IAsyncInfo> asyncInfo;
auto hr = operation->QueryInterface(__uuidof(asyncInfo),&asyncInfo
);
checkHr(hr
);
HRESULT errorCode;
hr = asyncInfo->get_ErrorCode(&errorCod
E);
checkHr(hr
);
// Do something with the errorCode
// Return once error is handled
return S_OK;
}
ComPtr<IStoreAppLicense> appLicense;
auto hr = operation->GetResults(&appLicens
E);
checkHr(hr
);
Boolean isActive,i
Strial =
false;
hr = appLicense->get_IsActive(&isActiv
E);
checkHr(hr
);
if (isActiv
E)
{
hr = appLicense->get_I
Strial(&i
Strial
);
checkHr(hr
);
}
onCompleted(static_cast<bool>(isActiv
E));
return S_OK;
}).Get()
);
checkHr(hr
);
}