Cross-platform builds with MSBuild.Sdk.Extras and Xamarin¶
Updated in 2026. Since .NET 6 the standard .NET SDK understands platform target frameworks such as net10.0-android and net10.0-ios, so most projects no longer need MSBuild.Sdk.Extras. Xamarin has been replaced by .NET for Android and .NET for iOS, and Visual Studio for Mac has been retired. The platform folder technique below still works unchanged with modern target frameworks.
In August 2018 I took over the day-to-day management of the ReactiveUI project. ReactiveUI is a cross-platform, functional-style MVVM framework built on the Reactive Extensions for .NET. We support a lot of platforms, which makes building and testing the product harder.
Since then we have worked hard to make our CI and DevOps experience easier, so that contributors find it simple to get involved.
This article shares how we support six very active repositories that all target .NET Standard, .NET Core and Xamarin.
Handling the target frameworks in your project files¶
We support many Xamarin and .NET targets, including .NET Core, iOS, Android, Tizen, tvOS, WPF and WinForms. We wanted to keep the SDK-style project format because of its many advantages, but the standard MSBuild SDK does not support many of these target frameworks out of the box.
Oren Novotny's MSBuild.Sdk.Extras comes to the rescue. It builds on the standard MSBuild SDK and adds support for extra target frameworks.
To get started, add a file called global.json next to your solution file. It sets the version of MSBuild.Sdk.Extras that every project in the folders below it uses:
{
"msbuild-sdks": {
"MSBuild.Sdk.Extras": "1.6.65"
}
}
Next, at the top of each SDK-style project file, replace:
<Project Sdk="Microsoft.NET.Sdk">
with:
<Project Sdk="MSBuild.Sdk.Extras">
If you are not targeting the .NET Core 3.0 preview, WPF and WinForms projects need ExtrasEnableWpfProjectSetup or ExtrasEnableWinFormsProjectSetup set to true in the main PropertyGroup. These flags also add common references for you, so you do not need to reference PresentationFramework yourself for WPF.
<PropertyGroup>
<!-- Other entries -->
<TargetFrameworks>net461</TargetFrameworks>
<!-- Needed for WPF -->
<ExtrasEnableWpfProjectSetup>true</ExtrasEnableWpfProjectSetup>
<!-- Needed for WinForms -->
<ExtrasEnableWinFormsProjectSetup>true</ExtrasEnableWinFormsProjectSetup>
</PropertyGroup>
Most Xamarin projects need to be built with msbuild rather than dotnet build.
Building .NET Framework targets only on Windows¶
Our projects target .NET Framework and UWP. We run CI on Windows and macOS, the two platforms that support the Xamarin targets, so we needed to leave those targets out when we are not building on Windows.
The best approach we found is a condition on TargetFrameworks that only adds them when the build runs on Windows:
<Project Sdk="MSBuild.Sdk.Extras">
<PropertyGroup>
<TargetFrameworks>MonoAndroid81;Xamarin.iOS10;Xamarin.Mac20;Xamarin.TVOS10;Xamarin.WatchOS10;tizen40;netstandard2.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">$(TargetFrameworks);net461;uap10.0.16299</TargetFrameworks>
</PropertyGroup>
</Project>
With this approach Visual Studio for Mac only used the first target framework it found and ignored the rest. Rider and plain MSBuild handled it better.
If you need to detect macOS or Linux in MSBuild, this MSBuild issue explains how.
Platform code without #if blocks¶
In the past, code that had to compile on several platforms was often full of #if blocks. That does not scale.
Instead we use a Platforms folder in the project, with one folder per platform:
src/
├── Platforms/
│ ├── ios/
│ ├── net4/
│ ├── wpf/
│ ├── winforms/
│ └── uwp/
├── cross-platform-code/
└── more-platform-common-code/
MSBuild reads a project file from top to bottom. First we add an ItemGroup that removes everything under Platforms from the build, using a glob. We add each platform back later.
<ItemGroup>
<Compile Remove="Platforms\**\*.*" />
<EmbeddedResource Remove="Platforms\**\*.*" />
<!-- Workaround so the files still appear in Visual Studio -->
<None Include="Platforms\**\*.*" />
</ItemGroup>
MSBuild sets the TargetFramework property for each framework it builds, so we add an ItemGroup per platform with a condition on the framework's prefix. Using StartsWith makes it easy to move to newer platform versions later.
<ItemGroup Condition=" $(TargetFramework.StartsWith('netstandard')) ">
<Compile Include="Platforms\netstandard\**\*.cs" />
</ItemGroup>
<ItemGroup Condition=" $(TargetFramework.StartsWith('net4')) ">
<Compile Include="Platforms\net4\**\*.cs" />
</ItemGroup>
Some platforms are trickier. UWP also has XAML pages, so we tell the compiler to include those too:
<ItemGroup Condition=" $(TargetFramework.StartsWith('uap')) ">
<Compile Include="Platforms\uwp\**\*.cs" />
<Page Include="Platforms\uwp\**\*.xaml" SubType="Designer" Generator="MSBuild:Compile" />
<None Update="**\*.xaml.cs" DependentUpon="%(Filename)" />
<Compile Update="**\*.xaml.cs" DependentUpon="%(Filename)" />
</ItemGroup>
%(Filename) is the file name without its extension, so DependentUpon nests each .xaml.cs file under its .xaml page.
Putting it all together¶
The finished project file looks like this:
<Project Sdk="MSBuild.Sdk.Extras">
<PropertyGroup>
<TargetFrameworks>Xamarin.iOS10;Xamarin.Mac20;tizen40;netstandard2.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">$(TargetFrameworks);net461;uap10.0.16299</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Platforms\**\*.*" />
<EmbeddedResource Remove="Platforms\**\*.*" />
<!-- Workaround so the files still appear in Visual Studio -->
<None Include="Platforms\**\*.*" />
</ItemGroup>
<ItemGroup Condition=" $(TargetFramework.StartsWith('Xamarin.iOS')) ">
<Compile Include="Platforms\apple-common\**\*.cs" />
<Compile Include="Platforms\ios\**\*.cs" />
</ItemGroup>
<ItemGroup Condition=" $(TargetFramework.StartsWith('Xamarin.Mac')) ">
<Compile Include="Platforms\apple-common\**\*.cs" />
<Compile Include="Platforms\mac\**\*.cs" />
</ItemGroup>
<ItemGroup Condition=" $(TargetFramework.StartsWith('tizen')) ">
<Compile Include="Platforms\tizen\**\*.cs" />
</ItemGroup>
<ItemGroup Condition=" $(TargetFramework.StartsWith('uap')) ">
<Compile Include="Platforms\uwp\**\*.cs" />
<Page Include="Platforms\uwp\**\*.xaml" SubType="Designer" Generator="MSBuild:Compile" />
<None Update="**\*.xaml.cs" DependentUpon="%(Filename)" />
<Compile Update="**\*.xaml.cs" DependentUpon="%(Filename)" />
</ItemGroup>
<ItemGroup Condition=" $(TargetFramework.StartsWith('netstandard')) ">
<Compile Include="Platforms\netstandard\**\*.cs" />
</ItemGroup>
<ItemGroup Condition=" $(TargetFramework.StartsWith('net4')) ">
<Compile Include="Platforms\net4\**\*.cs" />
</ItemGroup>
</Project>
Summary¶
I hope this helps you get started with libraries that target many platforms. These tools and techniques dramatically cut the effort of maintaining our platform-specific code.