在iOS应用程序

时间:2017-11-20 14:50:43

标签: xamarin.forms .net-standard

这个问题是关于在面向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
http://lastexitcode.com/blog/2017/06/04/NuGetSupportInVisualStudioMac7-0/

我做了什么

  1. 使用Visual Studio for Mac创建了一个新的空白Xamarin.Forms应用,面向iOS和Android并使用PCL
  2. 按照@JamesMontemagno的步骤,创建了一个新的.Net标准库项目,并通过Nuget添加了Xamarin.Forms 2.4.0
  3. 如果我理解得很好,那么我的新核心应用程序项目就是一个.Net Core项目。

    1. Microsoft.Rest.ClientRuntime 添加了NuGet依赖项,因为我的应用程序使用使用 autorest创建的自动生成的REST Api定义
    2. 添加了我的核心应用程序项目作为我的iOS项目的参考。问题: iOS项目是什么类型或项目? .Net Framework项目?或者是其他东西?我如何/可以创建.Net核心/标准iOS项目?
    3. 然后我构建并运行我的应用程序。当我点击一个触发使用 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,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35 type:< unknown type>构件:其中无>

      如果我将 Microsoft.Rest.ClientRuntime NuGet包添加为iOS项目的依赖项,那么它确实有效:

      1. 它正在吸引大量的NuGet依赖
      2. 这不是以前用于PCL的方式
      3. 所以我想知道是否有任何关于从旧的不太标准的项目中引用.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>
        

2 个答案:

答案 0 :(得分:0)

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

  

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

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

目前,您的软件包已安装为项目级软件包,这意味着只有您的标准库才能访问它(因此您在iOS中的错误)

答案 1 :(得分:0)

Matt Ward带来的解决方案:

  

如果你想避免很多NuGet依赖,你可以切换到   在iOS项目中使用PackageReferences而不是使用   packages.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/...