Since all code is common except the inheritance is different, it better to use a compile-time check (no worries of code being messy in this case).
Xamarin.Forms project does not support Multi-targeting frameworks, at the opposite it next evolution which is called MAUI will.
Meanwhile you can use MSBuild SDK Extras SDK instead of the default Microsoft.NET.Sdk
an (but keep in mind that it is not officially supported), but end result is neat (compile-time check).
- In
YourSharedProject.csproj
change <Project Sdk="Microsoft.NET.Sdk">
to <Project Sdk="MSBuild.Sdk.Extras/2.1.2">
.
- Setup the platforms that your projects is targeting and their version:
Example if you target netstandard2.1 and iOS 10 then change <TargetFramework>netstandard2.1</TargetFramework>
to <TargetFrameworks>netstandard2.1;iOS10</TargetFrameworks>
by starting with the netstandardxx
first.
- Taget Uwp:
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">$(TargetFrameworks);uap10.0.17763;netcoreapp3.1;net472</TargetFrameworks>
(if you have .net framework 4.7.1 installed instead of 4.7.2, replace net472
by net471
(same thing for .net.core and uwp versions).
At the end, your .csproj file starts will looks like this:
<Project Sdk="MSBuild.Sdk.Extras/2.1.2">
<PropertyGroup>
<TargetFrameworks>netstandard2.1;iOS10</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">
$(TargetFrameworks);uap10.0.17763;netcoreapp3.1;net472</TargetFrameworks>
All this will enable you to use the symbolic constants in a conditional compile time check:
public class CustomIOSEditor :
#if __iOS__
Editor //will inherit from this if we are building against iOS
#endif
#if WINDOWS_UWP //will inherit from this if we are building against uwp
SfRichTextEditor
#endif
{
public static readonly BindableProperty StringResultCommandProperty =
BindableProperty.Create(
nameof(StringResultCommand),
typeof(ICommand),
typeof(CustomIOSEditor),
default(ICommand));
public object StringResultCommandParameter
{
get => GetValue(StringResultCommandParameterProperty);
set => SetValue(StringResultCommandParameterProperty, value);
}
}
For targeting other platforms-version:
- Mac version 20:
Xamarin.Mac20
- Android version 10.0:
MonoAndroid10.0
- tizen version 40:
tizen40
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…