在我的 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/