How to multitarget in Xamarin.Forms with Visual Studio for Mac?

陌路散爱 提交于 2021-02-10 14:38:39

问题


I started out creating a .NET Standard Library that I intended to make a NuGet of - but came to find that I also needed platform-specific functionality.

So, I found this stack overflow question - which indicated I could use a Multiplatform Librarytemplate instead. But here I encountered a problem with DependencyService - and a problem with Custom Renderer - effectively meaning that there was no way for the non-platform-specific to communicate with platform-specific code in the library.

I also tried to edit the csproj file, examples: A, B, C. And by using the following csproj:

<Project Sdk="MSBuild.Sdk.Extras/2.0.29">
  <PropertyGroup>
    <TargetFrameworks>netstandard2.0;Xamarin.ios10</TargetFrameworks>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Xamarin.Forms" Version="4.1.0.581479" />
  </ItemGroup>
</Project>

... the following solution is generated:

But in Class1 the Xamarin.iOS nor Xamarin.Forms.Platform.iOS can be accessed in Visual Studio for Mac. And I recently checked and could see that it worked in Visual Studio on windows - using the same csproj.

Is there any way to multitarget in Visual Studio for Mac?


Edit 1: I found an issue on MSBuild.Sdk.Extras on GitHub leading to this post. It seems a certain extension can be installed called DotNetCoreSupportAnyTargetFramework.DotNetCoreSupportAnyTargetFramework_1.0. But it won't be installed correctly in VSfM 8.1.5:

Trying it out in older VSfM I can't open project or create new ones - so I can't test to see if the extension works. It could be related to the following error message:

that shows when opening the old VSfM.


Edit 2: After changing the order the target frameworks are given in - placing Xamarin.ios10 first - the Xamarin.iOS and Xamarin.Forms.Platform.iOS became accessible - as mentioned by Matt Ward.

However I can't add reference to the NetStandard Library to a Xamarin.Forms project to actually test the library:

And switching back to having netstandard placed first:

Projects/MyStandardLibrary/MyStandardLibrary/MyButtonRender.cs(12,12): Error CS0246: The type or namespace name'ExportRendererAttribute' could not be found (are you missing a using directive or an assembly reference?) (CS0246) (MyStandardLibrary) etc.


回答1:


The current release of Visual Studio for Mac, v8.2, does not support Multi-Targeting.

According to the Visual Studio for Mac Roadmap, Multi-Targeting support will be available in Visual Studio for Mac v8.3:

https://docs.microsoft.com/visualstudio/productinfo/mac-roadmap#2019-q3-july-september

Edit: You can follow the team's progress on adding Multi Targeting support here: https://github.com/mono/monodevelop/pull/7848



来源:https://stackoverflow.com/questions/57173464/how-to-multitarget-in-xamarin-forms-with-visual-studio-for-mac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!