【问题标题】:Dependency loading error when using a .Net Standard Xamarin.Forms project in an iOS app在 iOS 应用程序中使用 .Net Standard Xamarin.Forms 项目时的依赖项加载错误
【发布时间】: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 @

我做了什么

  1. 创建了一个新的空白 Xamarin.Forms 应用,面向 iOS 和 Android,并使用 PCL,使用 Visual Studio for Mac
  2. 按照@JamesMontemagno 的步骤,创建了一个新的 .Net 标准库项目并通过 Nuget 添加了 Xamarin.Forms 2.4.0

如果我理解得很好,此时,我的新核心应用程序项目是一个.Net Core项目。

  1. 添加了对 Microsoft.Rest.ClientRuntime 的 NuGet 依赖项,因为我的应用程序使用使用 autorest 创建的自动生成的 REST Api 定义
  2. 添加了我的 核心应用程序 项目作为对我的 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 项目的依赖项,那么它确实可以工作,但是:

  1. 它拉动了大量的 NuGet 依赖项
  2. 这不是它过去与 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


    【解决方案1】:

    您可以将 NuGet 包添加到您的 iOS 项目中,也可以将包添加为解决方案级别的包。 (在VS中右键解决方案,管理解决方案的NuGet包)

    解决方案级包(NuGet 3.x 及更高版本)仅在解决方案中安装一次,然后可用于解决方案中的所有项目。每个使用它的项目中都会安装一个项目级包。解决方案级别的包还可以安装可以从包管理器控制台中调用的新命令。

    通过https://docs.microsoft.com/en-us/nuget/policies/nuget-faq#nuget-in-visual-studio

    现在,您的包作为项目级包安装,这意味着只有您的标准库可以访问它(因此您在 iOS 中的错误)

    【讨论】:

    • 谢谢,但是这个步骤已经在问题中被访问过,并不是一个好的解决方案。
    【解决方案2】:

    Matt Ward 带来的解决方案:

    如果你想避免大量的 NuGet 依赖,你可以切换到 在 iOS 项目中使用 PackageReferences 而不是使用 包.config 文件。如果您使用的是 VS for Mac,那么目前您 需要手动将一个 PackageReference 添加到 .csproj 中 使用文本编辑器。一旦项目有一个 PackageReference 然后VS Mac将从那时起在项目文件中使用PackageReferences 开。

    <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)

    来源:forums.xamarin.com/discussion/...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-25
      • 1970-01-01
      • 2017-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多