在 iOS 应用程序中使用 .Net Standard Xamarin.Forms 项目时出现依赖项加载错误
Dependency loading error when using a .Net Standard Xamarin.Forms project in an iOS app
这个问题是关于在 Xamarin.Forms 项目中使用 .Net Standard,目标是 iOS(还有 Android)。
我读完了
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://whosebug.com/questions/tagged/.net-standard+xamarin.forms
http://lastexitcode.com/blog/2017/06/04/NuGetSupportInVisualStudioMac7-0/
我做了什么
- 创建了一个新的空白 Xamarin.Forms 应用程序,针对 iOS 和 Android 并使用 PCL,使用 Visual Studio 用于 Mac
- 按照@JamesMontemagno 的步骤,创建了一个新的 .Net Standard 库项目并通过 Nuget 添加了 Xamarin.Forms 2.4.0
如果我理解得很好,在这一点上,我的新核心应用程序项目是一个.Net Core项目。
- 向 Microsoft.Rest.ClientRuntime 添加了 NuGet 依赖项,因为我的应用程序使用自动生成的 REST Api 定义,该定义是使用 autorest 创建的
- 添加了我的 核心应用程序 项目作为对我的 iOS 项目的引用。这里的问题:这里的iOS项目是什么类型或项目? .Net 框架项目?或者是其他东西? do/can 我如何创建 .Net Core/Standard iOS 项目?
然后我构建并 运行 我的应用程序。当我按下触发使用 Microsoft.Rest.ClientRuntime 的按钮时,出现以下错误:
Could not load type of field 'MyApp.MyPage+d__3:5__2' (4) due to: Could not load file or assembly 'Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. assembly:Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 type:<unknown type> member:<none>
如果我将 Microsoft.Rest.ClientRuntime NuGet 包添加为 iOS 项目的依赖项,那么它确实有效,但是:
- 它拉动了大量的 NuGet 依赖项
- 这不是以前使用 PCL
的方式
所以我想知道是否有任何关于从不那么标准的旧项目中引用 .Net Standard 的文档,并发现了这个:
https://www.hanselman.com/blog/ReferencingNETStandardAssembliesFromBothNETCoreAndNETFramework.aspx
但这似乎不起作用,至少对于我的 iOS 使用 Visual Studio 用于 Mac 的项目来说是这样。
有什么解决方法的建议吗?或者我应该如何设置我的 iOS 应用程序项目以使用 .Net Standard 项目?
这是 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>
您可以将 NuGet 包添加到您的 iOS 项目,或将包添加为解决方案级包。 (在VS中右击解决方案,管理解决方案的NuGet包)
A solution-level package (NuGet 3.x and later) is installed only once in a solution and is then available for all projects in the solution. A project-level package is installed in each project that uses it. A solution-level package might also install new commands that can be called from within the Package Manager Console.
通过https://docs.microsoft.com/en-us/nuget/policies/nuget-faq#nuget-in-visual-studio
现在,您的包安装为项目级包,这意味着只有您的标准库可以访问它(因此您在 iOS 中的错误)
Matt Ward 带来的解决方案:
If you want to avoid a lot of NuGet dependencies you could switch to
using PackageReferences in the iOS project instead of using a
packages.config file. If you are using VS for Mac then currently you
will need to add one PackageReference into the .csproj manually by
using a text editor. Once the project has a single PackageReference
then VS Mac will use PackageReferences in the project file from then
on.
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="2.4.0.280" />
</ItemGroup>
然后执行大量的 NuGet 恢复、清理和重建。
还要确保使用相当新的 Mono 版本(Mono 5.4.1.6 可以,Mono 4.8.0 不能)。 Matt 的解决方案对我不起作用,因为我使用的是旧的 Mono 版本 (4.8.0)
这个问题是关于在 Xamarin.Forms 项目中使用 .Net Standard,目标是 iOS(还有 Android)。
我读完了
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://whosebug.com/questions/tagged/.net-standard+xamarin.forms
http://lastexitcode.com/blog/2017/06/04/NuGetSupportInVisualStudioMac7-0/
我做了什么
- 创建了一个新的空白 Xamarin.Forms 应用程序,针对 iOS 和 Android 并使用 PCL,使用 Visual Studio 用于 Mac
- 按照@JamesMontemagno 的步骤,创建了一个新的 .Net Standard 库项目并通过 Nuget 添加了 Xamarin.Forms 2.4.0
如果我理解得很好,在这一点上,我的新核心应用程序项目是一个.Net Core项目。
- 向 Microsoft.Rest.ClientRuntime 添加了 NuGet 依赖项,因为我的应用程序使用自动生成的 REST Api 定义,该定义是使用 autorest 创建的
- 添加了我的 核心应用程序 项目作为对我的 iOS 项目的引用。这里的问题:这里的iOS项目是什么类型或项目? .Net 框架项目?或者是其他东西? do/can 我如何创建 .Net Core/Standard iOS 项目?
然后我构建并 运行 我的应用程序。当我按下触发使用 Microsoft.Rest.ClientRuntime 的按钮时,出现以下错误:
Could not load type of field 'MyApp.MyPage+d__3:5__2' (4) due to: Could not load file or assembly 'Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. assembly:Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 type:<unknown type> member:<none>
如果我将 Microsoft.Rest.ClientRuntime NuGet 包添加为 iOS 项目的依赖项,那么它确实有效,但是:
- 它拉动了大量的 NuGet 依赖项
- 这不是以前使用 PCL 的方式
所以我想知道是否有任何关于从不那么标准的旧项目中引用 .Net Standard 的文档,并发现了这个:
https://www.hanselman.com/blog/ReferencingNETStandardAssembliesFromBothNETCoreAndNETFramework.aspx
但这似乎不起作用,至少对于我的 iOS 使用 Visual Studio 用于 Mac 的项目来说是这样。
有什么解决方法的建议吗?或者我应该如何设置我的 iOS 应用程序项目以使用 .Net Standard 项目?
这是 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>
您可以将 NuGet 包添加到您的 iOS 项目,或将包添加为解决方案级包。 (在VS中右击解决方案,管理解决方案的NuGet包)
A solution-level package (NuGet 3.x and later) is installed only once in a solution and is then available for all projects in the solution. A project-level package is installed in each project that uses it. A solution-level package might also install new commands that can be called from within the Package Manager Console.
通过https://docs.microsoft.com/en-us/nuget/policies/nuget-faq#nuget-in-visual-studio
现在,您的包安装为项目级包,这意味着只有您的标准库可以访问它(因此您在 iOS 中的错误)
Matt Ward 带来的解决方案:
If you want to avoid a lot of NuGet dependencies you could switch to using PackageReferences in the iOS project instead of using a packages.config file. If you are using VS for Mac then currently you will need to add one PackageReference into the .csproj manually by using a text editor. Once the project has a single PackageReference then VS Mac will use PackageReferences in the project file from then on.
<ItemGroup> <PackageReference Include="Xamarin.Forms" Version="2.4.0.280" /> </ItemGroup>
然后执行大量的 NuGet 恢复、清理和重建。
还要确保使用相当新的 Mono 版本(Mono 5.4.1.6 可以,Mono 4.8.0 不能)。 Matt 的解决方案对我不起作用,因为我使用的是旧的 Mono 版本 (4.8.0)