简体   繁体   中英

VS2015 C# UWP Build error, shared project and background task

I want to run a background task that will use a lot of the same code as the main project, so i have created a new project for my background task, and a new shared project for my shared code.

I have transferred all of my shared code to the shared code project, i have added the reference to my shared project into my main project and the project works.

I have then added a reference to my shared project into my background task project, built the solution and it works (works as in compiles).

As soon as i add the reference to my background task project into my main project, when i try to build the solution i get the following error:

A public type has a namespace ('BackgroundTask') that shares no common prefix with other namespaces ('SharedCode.Functions'). All types within a Windows Metadata file must exist in a sub namespace of the namespace that is implied by the file name. BackgroundTask C:\\Users\\User\\documents\\visual studio 2015\\Projects\\Project\\BackgroundTask\\WINMDEXP

If i dont add the reference to the background project within the main project and i try to run the background task, it fails.

In my background task project, at the top, i have included using SharedCode; And when the task runs, it just needs to make one call to the shared code:

await SharedCode.Check();

The background task project, doesnt need to use SharedCode.Functions directly, that would be called from within the SharedCode project itself.

Any ideas?

And just for reference, the shared code will make a http request and update a local database using SQLite.net-PCL

It seems there is a base namespace rule for shared projects and Runtime Components. (I've never heard of this before.)

Because of the way code generated form a runtime component is exported it must have a single root namespace. The easiest way to achieve this is to use project names with a common prefix before a dot.

So instead of having projects called

  • MyApp
  • SharedCode
  • BackgroundTask

You call them (and set the root namespaces)

  • MyApp (or MyApp.Client)
  • MyApp.Shared
  • MyApp.BackgroundTask

As an alternative you may make things much simpler for yourself if you might instead be able to use an in-process background task (depending on what your task does.)

It seems that you are using an old 'shared' project template that was used on Windows 8 / Windows Phone 8.

共享应用

In this kind of projects, the code is actually duplicated into the different parent projects meaning that even if you see three projects here, only two exe/dll will be generated. The code in the shared project will be copied into the windows and into the windows phone project.

To achieve what you are trying to do, you can simply create a regular class library (Universal Windows) to host your shared code, a Windows Runtime Component (Universal Windows) library to host the background task entry point and a Blank App (Universal Windows) .

Both your background task and your app will reference the shared class library.

You can also leave all your shared code in the background task library (WinMD) and consume it from your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM