如何为我的项目添加DependencyService?

时间:2019-06-26 17:50:33

标签: c# xamarin

我想将此DependencyService添加到我的项目中,但我不知道如何正确执行。

https://github.com/jamesmontemagno/InAppBillingPlugin/blob/master/docs/SecuringPurchases.md

我的解决方案中有三个项目,iOS项目,Android项目和SharedCode项目。

1)我将类Verify_Android添加到我的Android项目中。

2)我将类Verify_iOS添加到我的iOS项目中。

3)我将接口IVerify添加到了我的SharedCode项目中。

但是我收到一些错误消息,我不知道自己在做什么错。

public Task<bool> VerifyPurchase()
  

错误CS0106:修饰符“公共”对此项目无效

     

错误CS0531:“ IVerify.VerifyPurchase()”:接口成员无法   有一个定义

 public class Verify_Android : IVerify
  

错误CS0535:“ Verify_Android”未实现接口成员   'IVerify.VerifyPurchase()'

public class Verify_iOS : IVerify
  

错误CS0535:“ Verify_iOS”未实现接口成员“ IVerify.VerifyPurchase()”

此外,我不知道如何从位于SharedCode项目中的类调用该接口。如何调用该界面?

IVerify:

using System.Threading.Tasks;
using Plugin.InAppBilling.Abstractions;

namespace SharedProject
{
public interface IVerify : IInAppBillingVerifyPurchase
{
    public Task<bool> VerifyPurchase()
    {
    }
}
}

Verify_Android:

using System.Threading.Tasks;
using AndroidProject.Android;
using SharedProject;

[assembly: Xamarin.Forms.Dependency(typeof(Verify_Android))]
namespace AndroidProject.Android
{
public class Verify_Android : IVerify
{
    public Verify_Android()
    {
    }

    const string key1 = @"XOR_key1";
    const string key2 = @"XOR_key2";
    const string key3 = @"XOR_key3";

    public string SignedDataverify = "", Signatureverify = "";

    public Task<bool> VerifyPurchase(string signedData, string signature, string productId = null, string transactionId = null)
    {
        var key1Transform = Plugin.InAppBilling.InAppBillingImplementation.InAppBillingSecurity.TransformString(key1, 1);
        var key2Transform = Plugin.InAppBilling.InAppBillingImplementation.InAppBillingSecurity.TransformString(key2, 2);
        var key3Transform = Plugin.InAppBilling.InAppBillingImplementation.InAppBillingSecurity.TransformString(key3, 3);
        SignedDataverify = signedData;
        Signatureverify = signature;
        return Task.FromResult(Plugin.InAppBilling.InAppBillingImplementation.InAppBillingSecurity.VerifyPurchase(key1Transform + key2Transform + key3Transform, signedData, signature));
    }
}
}

Verify_iOS:

using System.Threading.Tasks;
using iOSProject.iOS;
using SharedProject;

[assembly: Xamarin.Forms.Dependency(typeof(Verify_iOS))]
namespace iOSProject.iOS
{
public class Verify_iOS : IVerify
{
    public Verify_iOS()
    {
    }

    const string key1 = @"XOR_key1";
    const string key2 = @"XOR_key2";
    const string key3 = @"XOR_key3";

    public string SignedDataverify = "", Signatureverify = "";

    public Task<bool> VerifyPurchase(string signedData, string signature, string productId = null, string transactionId = null)
    {
        SignedDataverify = signedData;
        Signatureverify = signature;
        return Task.FromResult(true);
    }
}
}

1 个答案:

答案 0 :(得分:0)

  

此外,我不知道如何从一个   位于我的SharedCode项目中。如何调用该界面?

您可以使用:

DependencyService.Get<IVerify>().VerifyPurchase(signedData, signature);

此外,您可以使用以下命令更新接口声明:

public interface IVerify : IInAppBillingVerifyPurchase
{
    Task<bool> VerifyPurchase();
}

,然后在购买期间为您的实例设置路径:

var purchase = await CrossInAppBilling.Current.PurchaseAsync(productId, ItemType.InAppPurchase, "apppayload", DependencyService.Get<IVerify>().VerifyPurchase(signedData, signature));