【发布时间】:2017-11-20 14:50:43
【问题描述】:
这个问题是关于在面向 iOS(但也包括 Android)的 Xamarin.Forms 项目中使用 .Net Standard。
我读完了
https://forums.xamarin.com/discussion/86139/targeting-net-standard/p1
https://blog.xamarin.com/building-xamarin-forms-apps-net-standard/
https://channel9.msdn.com/Shows/XamarinShow/Snack-Pack-15-Upgrading-to-XamarinForms-to-NET-Standard
https://developer.xamarin.com/guides/cross-platform/application_fundamentals/net-standard/
https://stackoverflow.com/questions/tagged/.net-standard+xamarin.forms
@987654326 @
我做了什么
- 创建了一个新的空白 Xamarin.Forms 应用,面向 iOS 和 Android,并使用 PCL,使用 Visual Studio for Mac
- 按照@JamesMontemagno 的步骤,创建了一个新的 .Net 标准库项目并通过 Nuget 添加了 Xamarin.Forms 2.4.0
如果我理解得很好,此时,我的新核心应用程序项目是一个.Net Core项目。
- 添加了对 Microsoft.Rest.ClientRuntime 的 NuGet 依赖项,因为我的应用程序使用使用 autorest 创建的自动生成的 REST Api 定义
- 添加了我的 核心应用程序 项目作为对我的 iOS 项目的引用。这里的问题:这里的iOS项目是什么类型或项目? .Net 框架项目?或者是其他东西?如何/我可以创建一个 .Net Core/Standard iOS 项目?
然后我构建并运行我的应用程序。当我点击触发使用 Microsoft.Rest.ClientRuntime 的按钮时,我收到以下错误:
无法加载字段“MyApp.MyPage+d__3:5__2”(4) 的类型,原因是:无法加载文件或程序集“Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 ' 或其依赖项之一。程序集:Microsoft.Rest.ClientRuntime,版本=2.0.0.0,文化=中性,PublicKeyToken=31bf3856ad364e35 类型: 成员:
如果我将 Microsoft.Rest.ClientRuntime NuGet 包添加为 iOS 项目的依赖项,那么它确实可以工作,但是:
- 它拉动了大量的 NuGet 依赖项
- 这不是它过去与 PCL 一起工作的方式
所以我想知道是否有任何关于从旧的不那么标准的项目中引用 .Net 标准的文档并发现:
https://www.hanselman.com/blog/ReferencingNETStandardAssembliesFromBothNETCoreAndNETFramework.aspx
但它似乎不起作用,至少不适用于我使用 Visual Studio for Mac 的 iOS 项目。
关于如何解决这个问题的任何建议?或者我应该如何设置我的 iOS 应用项目以使用 .Net 标准项目?
这是在 iOS 应用程序项目中引用 .Net Standard 项目的方式:
<ItemGroup>
<ProjectReference Include="..\MyApp\MyApp.csproj">
<Project>{2AA92B90-07DC-420B-8A5B-C84F2C437FF2}</Project>
<Name>MyApp</Name>
</ProjectReference>
</ItemGroup>
这就是 MyApp.csproj 的样子:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>
<ItemGroup>
<Folder Include="MyBackend\" />
</ItemGroup>
<ItemGroup>
<None Remove="LiuStackLayout.xaml" />
<None Remove="MyPage.xaml" />
</ItemGroup>
<ItemGroup>
<Compile Update="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Update="MyPage.xaml.cs">
<DependentUpon>MyPage.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="2.4.0.91020" />
<PackageReference Include="Microsoft.Rest.ClientRuntime" Version="2.3.10" />
</ItemGroup>
</Project>
【问题讨论】:
标签: xamarin.forms .net-standard