5

我有一个用 C++ 编写的静态库,我想将它提供给用 .NET 编写的 Windows 8 和 Windows Phone 8 应用程序。

我想通过设置我的解决方案来尽量减少要维护的单独项目的数量,如下所示:

尝试 #1:只有 UI 项目是特定于平台的

MySolution.sln
|
+- Library                    (virtual folder to group projects below)
|  |
|  +- LegacyLib.vcxproj       Static Library project - Platform-independant, native C++ code using STL & CRT. Just enough C++/CX to make some types available to .NET and expose a few static methods.
|  |
|  +- LegacyPcl.csproj        Portable Class Library project - References LegacyLib, adds some types, wrappers, interfaces, etc. written in C#.
|
+- SampleApp                  (virtual folder to group projects below)
   |
   +- CommonAppSrc            Platform-independant C# source code (e.g. MVVM classes for Model and ViewModel).
   |
   +- SampleApp_Win8.csproj   Platform-specific App project - References LegacyPcl, includes files from CommonAppSrc using "Add as Link".
   |
   +- SampleApp_Wp8.csproj    Platform-specific App project - References LegacyPcl, includes files from CommonAppSrc using "Add as Link".

问题:可移植类库似乎不支持以任何形式包含本机 C++ 代码。

在 Windows 8 和 Windows Phone 8 应用程序中包含本机 C++ 的一种建议方法是创建Windows 运行时组件。不幸的是,针对 Win8 和 Wp8 的 WRC 项目是不同的项目类型;没有创建面向多个平台的 Windows 运行时组件的选项

因此,为每个目标平台创建一个 Windows 运行时组件项目会产生以下结果:

尝试 #2 - UI 项目是特定于平台的,LegacyLib 包装器也是如此

MySolution.sln
|
+- Library                    (virtual folder to group projects below)
|  |
|  +- LegacyLib.vcxproj       Static Library project - Platform-independant, native C++ code using STL & CRT. Just enough C++/CX to make some types available to .NET and expose a few static methods.
|  |
|  +- LegacyWrc_Win8.csproj   Platform-specific Windows Runtime Component project - References LegacyLib, adds some types, wrappers, interfaces, etc. written in C#.
|  |
|  +- LegacyWrc_Wp8.csproj    Platform-specific Windows Runtime Component project - References LegacyLib, adds some types, wrappers, interfaces, etc. written in C#.
|
+- SampleApp                  (virtual folder to group projects below)
   |
   +- CommonAppSrc            C# source code (e.g. Model and ViewModel classes).
   |
   +- SampleApp_Win8.csproj   Platform-specific App project - References LegacyWrc_Win8, includes files from CommonAppSrc using "Add as Link".
   |
   +- SampleApp_Wp8.csproj    Platform-specific App project - References LegacyWrc_Wp8, includes files from CommonAppSrc using "Add as Link".

问题:当我尝试从 LegacyWrc_Win8 项目中引用 LegacyLib 时;我看到以下警告:

不推荐添加对“LegacyLib”的引用,因为它与 Windows 应用商店应用程序不兼容。

构建环境说要使 LegacyLib 静态库与我的 Win8 Windows 运行时组件兼容,我必须在 LegacyLib 项目配置中启用Windows Store App Support(忽略警告只会导致稍后出现错误):

  • 项目属性>配置属性>常规>项目默认值
  • Windows 应用商店应用支持设置为

接下来,当我尝试对 LegacyWrc_Wp8 项目做同样的事情时,我收到以下错误:

无法添加对“LegacyLib”的引用,因为这两个项目针对不同的平台。

因此,使静态库与 Windows 8兼容使其与 Windows 8 Phone 不兼容。

看起来根本不可能构建可由这两个不同平台使用的单个静态库,即使库本身不包含特定于平台的代码。

我不一定要维护源代码的并行版本,但我必须创建/维护两个单独的项目,每个项目都构建一个特定于平台的静态库版本。

我的解决方案现在每个项目都有一个特定于平台的版本:

尝试 #3 - 每个项目都是特定于平台的

MySolution.sln
|
+- Library                        (virtual folder to group projects below)
|  |
|  +- NativeCode                  (virtual folder to group projects below)
|  |  |
|  |  +- CommonLibSrc             Native C++ source code using STL & CRT. Just enough C++/CX to make some types available to .NET and expose a few static methods.
|  |  |
|  |  +- LegacyLib_Win8.vcxproj   Platform-specific Static Library project - Includes files from CommonLibSrc using "Add as Link".
|  |  |
|  |  +- LegacyLib_Wp8.vcxproj    Platform-specific Static Library project - Includes files from CommonLibSrc using "Add as Link".
|  |
|  +- DotNetWrapper               (virtual folder to group projects below)
|     |
|     +- CommonWrcSrc             Platform-independant C# source code (types, wrappers, interfaces, etc. for LegacyLib support) for the Windows Runtime Component projects.
|     |
|     +- LegacyWrc_Win8.csproj    Platform-specific Windows Runtime Component project - References LegacyLib_Win8, includes files from CommonWrcSrc using "Add as Link".
|     |
|     +- LegacyWrc_Wp8.csproj     Platform-specific Windows Runtime Component project - References LegacyLib_Wp8, includes files from CommonWrcSrc using "Add as Link".
|
+- SampleApp                      (virtual folder to group projects below)
   |
   +- CommonAppSrc                Platform-independant C# source code (e.g. Model, ViewModel).
   |
   +- SampleApp_Win8.csproj       Platform-specific App project - References LegacyWrc_Win8, includes files from CommonAppSrc using "Add as Link".
   |
   +- SampleApp_Wp8.csproj        Platform-specific App project - References LegacyWrc_Wp8, includes files from CommonAppSrc using "Add as Link".

问题:到目前为止我看不到任何东西(除了这很丑且维护成本更高的事实)。

顺便说一句,我确实研究了使用构建配置的可能性,但它们已经被用于调试/发布和 x86/ARM 的组合。将 Win8/Wp8 添加到组合中只会使配置再次翻倍,并且您已经转移,而不是减少了维护负担。

我错过了什么吗?这似乎是一件很常见的事情。有没有其他人经历过这个并想出替代/更好的方法来做到这一点?

编辑完整性

这就是我最终做的......

MySolution.sln
|
+- LegacyApiLib               (virtual folder to group projects below)
|  |
|  +- CommonCppSource         Common C++ source code used by LegacyWrc_*.vcxproj projects
|  |                          below
|  |
|  +- LegacyPcl.csproj        Portable class library with C# API interfaces only; no
|  |                          implementation classes.
|  |
|  +- LegacyWrc_Win8.vcxproj  Platform-specific Windows Runtime Component project - includes
|  |                          legacy C++ files, and uses C++/CX to expose static methods and
|  |                          classes to .NET in Win8.
|  |
|  +- LegacyWrc_Wp8.vcxproj   Platform-specific Windows Runtime Component project - includes
|                             legacy C++ files, and uses C++/CX to expose static methods and
|                             classes to .NET in WP8.
|
+- SampleApp                  (virtual folder to group projects below)
   |
   +- SampleAppBL.csproj      Portable class library containing the app's business logic.
   |                          References LegacyPcl.csproj for legacy API interface definitions.
   |                          Business logic is written against those interfaces; never against
   |                          specific classes. The Win8 or WP8 apps will reference the
   |                          appropriate Windows Runtime Component project, create or register
   |                          the concrete classes that implement the legacy API interface, and
   |                          either pass it into the BL or register it with IOC mechanism. In
   |                          this way, SampleAppBL.csproj never has to know about (or
   |                          reference) any of the WRC projects. 
   |
   +- SampleAppUI_Win8.csproj Platform-specific App project - References LegacyWrc_Win8.vcxproj
   |                          and SampleAppBL. Contains minimal platform-specific Win8 UI code.
   |
   +- SampleAppUI_Wp8.csproj  Platform-specific App project - References LegacyWrc_Wp8.vcxproj
                              and SampleAppBL. Contains minimal platform-specific WP8 UI code.

我意识到 LegacyApiLib 导出的少数辅助类可以用托管 C++ 而不是 C# 编写。这意味着我可以将它们与 WRC 项目捆绑在一起,这稍微简化了一些事情。

4

1 回答 1

2

如您所见,本机库需要为每个平台单独编译,因此不能直接从可移植类库中引用。

我建议使用抽象模式来允许您通过可移植类库共享大部分 .NET 代码(即模型和视图模型)。在 PCL 中,为您在本机库中公开的功能创建接口或抽象类。然后在将调用转发到本机库的特定平台项目中实现这些。最后使用依赖注入来连接这些接口的实现,以便您可以从共享 PCL 中调用它们。

以下是一些涵盖此内容的博客文章:

于 2013-07-01T17:00:28.987 回答