MvvmCross - how do I access SQLite in a windows store background task?

帅比萌擦擦* 提交于 2019-12-06 14:39:32
Stuart

If you want to initialize the full MvvmCross framework including all of your app, then you'll need to run your Setup class.

In WinRT, this could be as simple as calling:

         var setup = new Setup(null /*rootFrame*/);
         setup.Initialize();

although it may require you to do a little work to:

  1. Make sure your presenter does not use the null rootFrame
  2. Provide some other means to create a UI thread dispatcher - currently MvxStoreViewDispatcher relies on .Dispatcher access - see https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.WindowsStore/Views/MvxStoreViewDispatcher.cs - to do this, you could override InitializeViewDispatcher with something like:

    protected override void InitializeViewDispatcher()
    {
        if (_rootFrame != null)
        {
             base.InitializeViewDispatcher(); return;
        }
    
        var dispatcher = new NonMainThreadDispatcher();
        Mvx.RegisterSingleton<IMvxMainThreadDispatcher>(dispatcher);
    }
    
    public class NonMainThreadDispatcher : MvxMainThreadDispatcher
    {
        public bool RequestMainThreadAction(Action action)
        {
            action();
        }
    }
    

If you want to initialize less functionality than the entire framework (e.g. for memory reasons) then you can also consider creating special Setup and App classes just for your background task.

Aside> This is similar to questions like these in Android - Using MvvmCross from content providers and activities and MvvmCross initialization

I was able to solve the problem in a straightforward way. Since the background task only needed the SQLite data service from the PCL core project, I did the following:

  1. Included a reference to the Core project.
  2. Added the nuget packages for MvvmCross and the SQLite community plugin.
  3. Deleted all of the files and folders added when doing the mvvmcross install: Bootstrap/, Todo-Mvvmcross/, Views/, DebugTrace.cs, and Setup.cs.
  4. There is a current limitation in the nuget installer that requires some additional edits to the project file to handle multiple store platforms (x86, ARM, and x64), see 'Cirrius.Mvvmcross.Community.Plugins.SQLite.WindowsStore needs platform-specific dlls for X86 and ARM' on Stack Overflow for details. Make sure you put the Choose statement after the default SQLite.WindowsStore reference and you need to leave the default reference in the project file. You will also need to adjust the HintPath based on the location/names of your references.
  5. Initialized the SQLite data service by explicitly calling the factory and creating a new instance of the data service:

        var factory = new MvxStoreSQLiteConnectionFactory();
        IMyDataService repository = new MyDataService(factory);
    

I then have access to the data service with no other overhead associated with mvvmcross.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!