Xamarin.forms 消息中心未从xamarin ios任务呼叫
在我的xamarin.forms应用程序中,我尝试在ios和android中创建后台任务和服务。我为android创建了服务,为ios创建了任务。根据服务和任务,我试图在共享代码中调用web API。我正在使用消息中心与共享代码通信。在安卓系统中,通信工作正常。但在ios中,消息中心不订阅共享代码 我的ios后台任务Xamarin.forms 消息中心未从xamarin ios任务呼叫,xamarin.forms,Xamarin.forms,在我的xamarin.forms应用程序中,我尝试在ios和android中创建后台任务和服务。我为android创建了服务,为ios创建了任务。根据服务和任务,我试图在共享代码中调用web API。我正在使用消息中心与共享代码通信。在安卓系统中,通信工作正常。但在ios中,消息中心不订阅共享代码 我的ios后台任务 public class iOSLongRunningTaskExample { nint _taskId; CancellationTo
public class iOSLongRunningTaskExample
{
nint _taskId;
CancellationTokenSource _cts;
public async Task Start()
{
_cts = new CancellationTokenSource();
_taskId = UIApplication.SharedApplication.BeginBackgroundTask("LongRunningTask", OnExpiration);
try
{
// Here I am calling the shared code
Device.BeginInvokeOnMainThread(() =>
MessagingCenter.Send<Object>(new Object(),
"CheckNotificationAPI")
);
}
catch (OperationCanceledException)
{
}
finally
{
//if (_cts.IsCancellationRequested)
//{
// var message = new CancelledMessage();
// Device.BeginInvokeOnMainThread(
// () => MessagingCenter.Send(message, "CancelledMessage")
// );
//}
}
UIApplication.SharedApplication.EndBackgroundTask(_taskId);
}
public void Stop()
{
_cts.Cancel();
}
void OnExpiration()
{
_cts.Cancel();
}
}
public类iOSLongRunningTaskExample
{
任天堂任务ID;
取消令牌源;
公共异步任务启动()
{
_cts=新的CancellationTokenSource();
_taskId=UIApplication.SharedApplication.BeginBackgroundTask(“LongRunningTask”,OneExption);
尝试
{
//我在这里调用共享代码
Device.beginInvokeMainThread(()=>
MessagingCenter.Send(新对象(),
“检查通知API”)
);
}
捕获(操作取消异常)
{
}
最后
{
//如果(_cts.iscancellationrequest)
//{
//var message=新的CanceledMessage();
//Device.beginInvokeMainThread(
//()=>MessagingCenter.Send(消息,“取消消息”)
// );
//}
}
UIApplication.SharedApplication.EndBackgroundTask(_taskId);
}
公共停车场()
{
_cts.Cancel();
}
void onexption()
{
_cts.Cancel();
}
}
我的共享代码
MessagingCenter.Subscribe<Object>(this, "CheckNotificationAPI", async (sender) =>
{
Device.BeginInvokeOnMainThread(async () =>
{
await DisplayAlert("Message", "Recieved", "OK");
});
});
[Service]
public class LongRunningTaskService : Service
{
CancellationTokenSource _cts;
public override IBinder OnBind(Intent intent)
{
return null;
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
_cts = new CancellationTokenSource();
Task.Run(() => {
try
{
//INVOKE THE SHARED CODE
Device.BeginInvokeOnMainThread(()=>
MessagingCenter.Send<Object>(new Object(), "CheckNotificationAPI")
);
}
catch (System.OperationCanceledException)
{
}
finally
{
//if (_cts.IsCancellationRequested)
//{
// var message = new CancelledMessage();
// Device.BeginInvokeOnMainThread(
// () => MessagingCenter.Send(message, "CancelledMessage")
// );
//}
}
}, _cts.Token);
return StartCommandResult.Sticky;
}
public override void OnDestroy()
{
if (_cts != null)
{
_cts.Token.ThrowIfCancellationRequested();
_cts.Cancel();
}
base.OnDestroy();
}
}
MessagingCenter.Subscribe(此“CheckNotificationAPI”,异步(发送方)=>
{
Device.beginInvokeMainThread(异步()=>
{
等待显示警报(“消息”、“收到”、“确定”);
});
});
我的android服务运行良好
MessagingCenter.Subscribe<Object>(this, "CheckNotificationAPI", async (sender) =>
{
Device.BeginInvokeOnMainThread(async () =>
{
await DisplayAlert("Message", "Recieved", "OK");
});
});
[Service]
public class LongRunningTaskService : Service
{
CancellationTokenSource _cts;
public override IBinder OnBind(Intent intent)
{
return null;
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
_cts = new CancellationTokenSource();
Task.Run(() => {
try
{
//INVOKE THE SHARED CODE
Device.BeginInvokeOnMainThread(()=>
MessagingCenter.Send<Object>(new Object(), "CheckNotificationAPI")
);
}
catch (System.OperationCanceledException)
{
}
finally
{
//if (_cts.IsCancellationRequested)
//{
// var message = new CancelledMessage();
// Device.BeginInvokeOnMainThread(
// () => MessagingCenter.Send(message, "CancelledMessage")
// );
//}
}
}, _cts.Token);
return StartCommandResult.Sticky;
}
public override void OnDestroy()
{
if (_cts != null)
{
_cts.Token.ThrowIfCancellationRequested();
_cts.Cancel();
}
base.OnDestroy();
}
}
[服务]
公共类LongRunningTaskService:服务
{
取消令牌源;
公共覆盖iBind OnBind(意图)
{
返回null;
}
公共覆盖StartCommandResult OnStartCommand(意图、StartCommandFlags标志、int-startId)
{
_cts=新的CancellationTokenSource();
Task.Run(()=>{
尝试
{
//调用共享代码
Device.beginInvokeMainThread(()=>
MessagingCenter.Send(新对象(),“CheckNotificationAPI”)
);
}
捕获(系统操作取消异常)
{
}
最后
{
//如果(_cts.iscancellationrequest)
//{
//var message=新的CanceledMessage();
//Device.beginInvokeMainThread(
//()=>MessagingCenter.Send(消息,“取消消息”)
// );
//}
}
},_cts.Token);
返回StartCommandResult.Sticky;
}
公共覆盖无效OnDestroy()
{
如果(_cts!=null)
{
_cts.Token.ThrowIfCancellationRequested();
_cts.Cancel();
}
base.ondestory();
}
}
非常感谢您的帮助。我看到您在本机代码中使用
BeginInvokeOnMainThread
,使用本机方法不是更有意义吗?另外,为什么要使用本机类来调用API?在共享代码中这样做不是更有意义吗?另外,您为什么要为此使用MessagingCenter
?你不应该有一个直接的参考,你可以使用吗?@FreakyAli Bro。我试图实现的是在调用任务时在共享代码中调用API。那怎么做呢?我将api调用放在消息中心的订阅部分中。但它没有订阅我猜@AndroDevil Hi,在iOSLongRunningTaskExample
中调用的代码是不是?首先,您应该检查BeginInvokeOnMainThread
是否在iOSLongRunningTaskExample
中工作。如果只是想从api中获取数据,使用FreakyAli说的Restful是一种很好的实现方法。@JuniorJiang MSFT我只想从iOSLongRunningTaskExample中初始化信息中心,并在共享代码上订阅它