加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows-8 – 在Win8和WP8应用程序中共享静态库

发布时间:2020-12-14 02:50:22 所属栏目:Windows 来源:网络整理
导读:我有一个用C编写的静态库,我希望它可用于用.NET编写的 Windows 8和 Windows Phone 8应用程序. 我想通过设置我的解决方案来最小化要维护的单独项目的数量,如下所示: 尝试#1:只有UI项目是特定于平台的 MySolution.sln|+- Library (virtual folder to group p
我有一个用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".

问题:似乎Portable Class Libraries不支持以任何形式包含本机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,etc. written in C#.
|  |
|  +- LegacyWrc_Wp8.csproj    Platform-specific Windows Runtime Component project - References LegacyLib,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时;我看到以下警告:

Adding reference to ‘LegacyLib’ is not recommended because it is not compatible with Windows Store Apps.

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

>项目属性>配置属性>一般>项目默认值
>将Windows应用商店应用支持设置为是

接下来,当我尝试使用LegacyWrc_Wp8项目执行相同操作时,出现以下错误:

A reference to ‘LegacyLib’ cannot be added because the two projects target different platforms.

因此,使静态库与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,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".

问题:到目前为止我无法看到(除了这是丑陋和更高维护的事实).

顺便说一下,我确实研究过使用Build Configurations的可能性,但它们已经被用于Debug / Release和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项目捆绑在一起,这简化了一些事情.

解决方法

如您所见,需要为每个平台单独编译本机库,因此无法从可移植类库直接引用它.

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

以下是一些博客文章,内容包括:

> Using Target-Specific Code in a Portable Library
> How to Make Portable Class Libraries Work for You

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读