How to use SQLite in Windows (Phone) 8.1's Bac

2019-02-20 00:43发布

问题:

Setup

I followed Universal App with SQLite example to add SQLite to my project; then to add the BackgroundTask I followed Quickstart: Create and register a background task example.

Problem

  1. The SQLite targets either Windows Phone 8.1 or Windows 8.1

  2. The BackgroundTask targets only Windows (being a Windows Runtime Component)

This is where it becomes tricky.

You cannot reference a project with the BakgroundTask that isn't portable, thus I cannot add SQLite for Windows Runtime (Windows 8.1) or SQLite for Windows Phone 8.1 to the BackgroundTask project.

Question

Is there anyway to use SQLite in the BackgroundTask of Windows (Phone) 8.1 project?

回答1:

Sometimes the answer is easier than expected.

Steps

  1. Set the Background Task's Library Target to only target Windows (Phone) 8.1
  2. Create a Class Library that targets only Windows (Phone) 8.1
  3. Add SQLite for Windows (Phone) 8.1 to the Class Library
  4. Reference the Class Library with the Background Task

That's it!

Do most of the work in the Class Library and only make calls from the Background task, this makes the BGT look cleaner. The nice thing about using a Class Library is that you can use generics.

You will have 2 Background Tasks for your universal app

  • Windows 8.1 Background Task
  • Windows Phone 8.1 Background Task