来自PCL项目的xamarin.ios中的调用方法

问题描述 投票:0回答:1

AppDelegate类的我的Xamarin.iOS项目中我有方法:

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项目中调用它?

xamarin.forms xamarin.ios
1个回答
1
投票

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)使用Dependency Service从PCL项目调用您的自定义实现,以找到Interface实现。

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

d)执行你的逻辑。

这就是你所需要的一切。

© www.soinside.com 2019 - 2024. All rights reserved.