简体   繁体   English

VS2015 C# UWP Build 错误,共享项目和后台任务

[英]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').公共类型有一个命名空间 ('BackgroundTask'),它与其他命名空间 ('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. Windows 元数据文件中的所有类型都必须存在于文件名隐含的命名空间的子命名空间中。 BackgroundTask C:\\Users\\User\\documents\\visual studio 2015\\Projects\\Project\\BackgroundTask\\WINMDEXP 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;在我的后台任务项目中,在顶部,我使用了 SharedCode; And when the task runs, it just needs to make one call to the shared code:当任务运行时,它只需要对共享代码进行一次调用:

await SharedCode.Check();等待 SharedCode.Check();

The background task project, doesnt need to use SharedCode.Functions directly, that would be called from within the SharedCode project itself.后台任务项目,不需要直接使用 SharedCode.Functions,它会从 SharedCode 项目本身内部调用。

Any ideas?有任何想法吗?

And just for reference, the shared code will make a http request and update a local database using SQLite.net-PCL仅供参考,共享代码将使用 SQLite.net-PCL 发出 http 请求并更新本地数据库

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(或 MyApp.Client)
  • MyApp.Shared MyApp.Shared
  • MyApp.BackgroundTask 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.您似乎正在使用在 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.在这种项目中,代码实际上被复制到不同的父项目中,这意味着即使您在这里看到三个项目,也只会生成两个 exe/dll。 The code in the shared project will be copied into the windows and into the windows phone project.共享项目中的代码将被复制到 windows 和 windows phone 项目中。

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) .为了实现您想要做的事情,您可以简单地创建一个常规class library (Universal Windows)来托管您的共享代码、一个Windows Runtime Component (Universal Windows)库来托管后台任务入口点和一个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.您还可以将所有共享代码保留在后台任务库 (WinMD) 中,并从您的应用程序中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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