xamarin.forms - 从 PCL 项目调用 xamarin.ios 中的方法

标签 xamarin.forms xamarin.ios

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

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 项目中创建一个接口(interface):

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

b) 创建一个类来在您的 iOS 项目中实现此接口(interface),请记住使用所需的属性来装饰该类:

[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 项目调用自定义实现来定位接口(interface)实现。

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

d) 执行你的逻辑。

这就是您所需要的。

关于xamarin.forms - 从 PCL 项目调用 xamarin.ios 中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54987569/

相关文章:

ios - 为什么在使用 Storyboard时会反复调用 AwakeFromNib?

xamarin.ios - MonoTouch.对话框 : Advanced Editing

xcode - 如何提高 iOS 7 中工具栏和导航栏上按钮的点击能力

iphone - 尝试为 iPhone 构建 "The application was terminated by a signal: SIGHUP"

xamarin - Xamarin Forms View 不使用自定义类更新

xamarin.forms - Xamarin Forms 暂存、生产和开发环境

c# - 从索引数组动态填充 mvvm 模型

xamarin - 在同一 View 下绑定(bind)命令和CommandParameter

ios - 单点触控 : Get user current lcoation

visual-studio - 编写开口尖括号时Visual Studio 2013崩溃