pcl项目中的Xamarin.ios调用方法



在我的xamarin.ios项目中, AppDelegate class i有方法:

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
    Hub = new SBNotificationHub(Constants.ListenConnectionString, Constants.NotificationHubName);
    Hub.UnregisterAllAsync (deviceToken, (error) => {
        if (error != null)
        {
            System.Diagnostics.Debug.WriteLine("Error calling Unregister: {0}", error.ToString());
            return;
        }
        NSSet tags = null; // create tags if you want
        Hub.RegisterNativeAsync(deviceToken, tags, (errorCallback) => {
            if (errorCallback != null)
                System.Diagnostics.Debug.WriteLine("RegisterNativeAsync error: " + errorCallback.ToString());
        });
    });
}

我如何从我的PCL项目中调用它?

a)在您的PCL项目中创建接口:

namespace YourNamespace.Interfaces
{
    public interface IRemoteNotifications
    {
        void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken);
    }
}

b)创建一个将在iOS项目中实现此接口的类,重新读取具有所需属性的类:

[assembly: Dependency(typeof(RemoteNotifications))]
namespace YourNamespace.iOS.DependencyService
{
    public class RemoteNotifications : IRemoteNotifications
    {
        public void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        {
            // Place your custom logic here
        }
    }
}

c)使用依赖项服务从PCL项目调用您的自定义实现以找到接口实现。

DependencyService.Get<IRemoteNotifications>().RegisteredForRemoteNotifications(application, deviceToken);

d)执行您的逻辑。

这就是您所需要的。

最新更新