1、下载OpenXLive SDK 按照官网教程安装,在安装文件夹内可以找到OpenXlive个SDK教程。
2、直接将\OpenXLivePaymentSDK_WP_1.0.3.1_MSI\Payment\PaymentSLSample 内的Controls、Converter、Themes、文件夹以及内的文件
CustomerInfo.cs、ShopPage.xaml.cs、PurchasedPage.xaml.cs
3、再就是添加C# 与C++的互相调用类,可以参考http://www.cnblogs.com/zhxilin/archive/2013/03/20/2971331.html的方法。
附上 XLiveCallback.cs
namespace PhoneDirect3DXamlAppInterop
{
public sealed class XLiveCallback : ICallback
{
private MainPage m_mainPage = null;
public void SetMainPage(MainPage page)
{
m_mainPage = page;
}
public void payment(int price, int quantity, string goodId)
{
m_mainPage.OnCreateShopPage();
}
}
}
ICallback.h
namespace PhoneDirect3DXamlAppComponent
{
namespace OpenXLiveHelper
{
// A callback interface for C# code to implement.
public interface class ICallback
{
void payment(int price, int quantity, Platform::String^ goodId);
};
}
}
XLiveDelegate.h 文件
#include "ICallback.h"
namespace PhoneDirect3DXamlAppComponent
{
namespace OpenXLiveHelper
{
// A native class to pass and store an ICallback instance, calling C# code via the ICallback object.
[Windows::Foundation::Metadata::WebHostHidden]
public ref class XLiveDelegate sealed
{
public:
XLiveDelegate();
// Gets the single XLiveDelegate instance.
static XLiveDelegate^ GetInstance();
// Set callback into the XLiveDelegate instance.
void SetCallback(ICallback^ callback);
void SetCallback_buySucc(Platform::String^ goodId,int nCount);
Platform::String^ getChooseGoodsID();
// Gets or sets the callback.
property ICallback^ GlobalCallback;
void pay(int price, int quantity, Platform::String^ goodId, int token);
private:
static XLiveDelegate^ m_Instance;
int m_Token;
Platform::String^ m_goodId;
};
}
}
//cpp
#include "pch.h"
#include "XLiveDelegate.h"
#include "GameStore.h"
#include "GameData.h"
namespace PhoneDirect3DXamlAppComponent
{
namespace OpenXLiveHelper
{
XLiveDelegate::XLiveDelegate()
{
}
XLiveDelegate^ XLiveDelegate::GetInstance()
{
if (m_Instance == nullptr)
{
m_Instance = ref new XLiveDelegate();
}
return m_Instance;
}
void XLiveDelegate::SetCallback(ICallback^ callback)
{
GlobalCallback = callback;
}
Platform::String^ XLiveDelegate::getChooseGoodsID()
{
return m_goodId;
}
void XLiveDelegate::SetCallback_buySucc(Platform::String^ goodId, int nCount)
{
GameStoreLayer* pStoreLayer = (GameStoreLayer*)GameData::getSharedGameData()->getDataPoint(DT_Shop);
if (pStoreLayer)
{
pStoreLayer->buySuccess(goodId,nCount);
}
}
XLiveDelegate^ XLiveDelegate::m_Instance;
void XLiveDelegate::pay(int price, int quantity, Platform::String^ goodId, int token)
{
if (GlobalCallback)
{
m_goodId = goodId;
m_Token = token;
GlobalCallback->payment(price, quantity, goodId);
}
}
void XLiveDelegate::exitGame()
{
if (GlobalCallback)
GlobalCallback->exitGame();
}
void XLiveDelegate::openUrl()
{
if (GlobalCallback)
GlobalCallback->openUrl();
}
}
}
// MainPage.cs 下面添加一个调用ShopPage.xml 的逻辑
public void OnCreateShopPage()
{
if (XLivePayment.IsValid)
{
this.Dispatcher.BeginInvoke(() =>
{
this.NavigationService.Navigate(new Uri("/ShopPage.xaml", UriKind.Relative));
});
}
}
//此处是调用的充值返回数据;
internal void UseAsset()
{
var assets = XLivePayment.CurrentService.Customer.Assets;
foreach (var item in assets)
{
System.Diagnostics.Debug.WriteLine(item.ToString());
// 根据需要调用Asset.Use方法使用该资产。
switch (item.ProductInfo.Type)
{
case ProductType.Durable:
// 耐用品会在资产列表中永久保存,应该直接给予用户,不需要调用Use方法。
break;
case ProductType.Consumable:
case ProductType.Once:
item.Use((s, e) =>
{
if (e.Result.ReturnValue)
{
//使用到游戏内。
XLiveDelegate.GetInstance().SetCallback_buySucc(item.ProductInfo.ProductId, Convert.ToInt32(e.Result.Tag));
// //只有在使用成功后,才应该将商品或道具给予用户。
// //使用成功,玩家获得相应商品或道具。
// //==========================================
// //警告:必须将使用的数量算上,否则会导致玩家购买的商品与实际得到的不一致,e.Result.Tag保存了使用数量
// // 此时玩家应该得到的商品为:item.ProductInfo 乘以 Convert.ToInt32(e.Result.Tag)
}
});
break;
default:
break;
}
}
}
}
// 在返回后台以后或者关闭第三方支付页面时,cocos2d-x2.2.4 wp8 会在reloadAllTexture 会崩溃,所以注释下面代码。可以解决。
void Direct3DInterop::Disconnect()
{
// m_renderer->Disconnect();
}