Reputation: 10005
This question is about the use of .Net Standard in a Xamarin.Forms project targeting iOS (but also Android).
I've read through
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/
What I've done
If I understand well, at this point, my new core application project is a .Net Core project.
Then I build and run my application. When I hit a button that trigger the use of Microsoft.Rest.ClientRuntime, I get the following error:
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>
If I add Microsoft.Rest.ClientRuntime NuGet package as a dependency to the iOS project then it does work but:
So I wondered if there's anything documented about referencing .Net Standard from old-not-so-standard projects and found this:
https://www.hanselman.com/blog/ReferencingNETStandardAssembliesFromBothNETCoreAndNETFramework.aspx
But it does not seem to work, at least not for my iOS project using Visual Studio for Mac.
Any suggestion on how to solve this? Or how am I supposed to set up my iOS app project to use the .Net Standard project?
This is how the .Net Standard project is referenced in the iOS application project:
<ItemGroup>
<ProjectReference Include="..\MyApp\MyApp.csproj">
<Project>{2AA92B90-07DC-420B-8A5B-C84F2C437FF2}</Project>
<Name>MyApp</Name>
</ProjectReference>
</ItemGroup>
And this is how MyApp.csproj looks like:
<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>
Upvotes: 0
Views: 1644
Reputation: 10005
Solution brought by 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>
Then perform a fair amount of NuGet restore, clean and rebuild.
Also make sure to use a rather new Mono version (Mono 5.4.1.6 works, Mono 4.8.0 does not). The Matt's solution was not working for me because I was using an old Mono build (4.8.0)
Source: forums.xamarin.com/discussion/...
Upvotes: 0
Reputation: 31
You could either add the NuGet package to your iOS project, or add the package as a solution-level package. (Right click on the solution in VS and manage NuGet packages for solution)
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.
Via https://learn.microsoft.com/en-us/nuget/policies/nuget-faq#nuget-in-visual-studio
Right now, your package is installed as a project-level package, which means only your Standard library receives access to it (thus your error in iOS)
Upvotes: 0