[英]How to use Xamarin Forms PCL code from a Background Task in WP8?
I'm trying to use the PCL code to access a SQLite database from a periodic background task in windows-phone-8. 我试图使用PCL代码从周期性后台任务访问SQLite数据库在Windows手机-8。
However, I get this error: InvalidOperationException: You MUST call Xamarin.Forms.Init(); prior to using it
不过,我得到这个错误: InvalidOperationException: You MUST call Xamarin.Forms.Init(); prior to using it
InvalidOperationException: You MUST call Xamarin.Forms.Init(); prior to using it
Trying to do a Forms.Init()
when the agent is invoked like so 像这样调用代理时尝试执行Forms.Init()
protected override void OnInvoke(ScheduledTask task)
{
Forms.Init();
throws UnauthorizedAccessException: Invalid cross-thread access.
抛出UnauthorizedAccessException: Invalid cross-thread access.
Also tried Device.BeginInvokeOnMainThread(() => { Forms.Init(); });
也尝试Device.BeginInvokeOnMainThread(() => { Forms.Init(); });
but that also didn't work. 但这也不起作用。
What is the proper way of using the common code from the PCL project in a background task? 什么是使用从PCL项目通用代码在后台任务的正确方法?
EDIT: 编辑:
I'm calling Forms.Init()
in the startup page 我在启动页面中调用Forms.Init()
Not doing any UI updates from the background task. 不从后台任务进行任何UI更新。 The exception is thrown when trying to use the DependencyService
to get the platform-specific connection details: DependencyService.Get<IDatabaseConection>().GetConnection();
尝试使用DependencyService
获取特定于平台的连接详细信息时,将引发异常: DependencyService.Get<IDatabaseConection>().GetConnection();
Worked around the problem by using a different IoC container, as @Jason also suggested. 通过使用不同的IoC容器来解决此问题,如@Jason所建议的那样。 AutoFac did the trick for me. AutoFac帮了我大忙 。
Tried first with TinyIoC
but got the same errors referenced here : Error CS0246: The type or namespace name 'BindingFlags' could not be found. Are you missing an assembly reference? (CS0246)
首先使用TinyIoC
了尝试,但这里引用了相同的错误: Error CS0246: The type or namespace name 'BindingFlags' could not be found. Are you missing an assembly reference? (CS0246)
Error CS0246: The type or namespace name 'BindingFlags' could not be found. Are you missing an assembly reference? (CS0246)
Error CS0246: The type or namespace name 'BindingFlags' could not be found. Are you missing an assembly reference? (CS0246)
. Error CS0246: The type or namespace name 'BindingFlags' could not be found. Are you missing an assembly reference? (CS0246)
。
Other IoC containers that should work with Xamarin . 其他应与Xamarin配合使用的IoC容器 。
It's still unclear why Xamarin's IoC requires the Forms.Init()
call 尚不清楚为什么Xamarin的IoC需要Forms.Init()
调用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.