Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create platform specific views?

Tags:

maui

.net-maui

In UWP, device specific views can be created by placing those views into their respective folders or appending the relevant device name to the file name. I don't see any information on doing the equivalent with MAUI as regards platform specific views only platform specific code. Is this a thing ?

The closest I can get is using preprocessor directives and giving the files unique names. This means there are still multiple code behind files unfortunately.

So -

#if ANDROID

      await Navigation.PushAsync(new Page1Windows());
 

#elif WINDOWS

      await Navigation.PushAsync(new Page1Android());
    

#endif

like image 319
Peter DC Avatar asked Sep 11 '25 23:09

Peter DC


1 Answers

It's a thing. The default MAUI projects provide a platform specific folder you can put platform specific code in.

If you want something more complicated, like your platform-named folders or files, you can edit your csproj to include or exclude files by name & platform.

There are examples of both file and folder-based targeting in the Configure multi-targeting docs:

Here's the combined view from Combine filename and folder multi-targeting that excludes files and folders that include platform names that don't match the current target.

<ItemGroup Condition="$(TargetFramework.StartsWith('net6.0-android')) != true">
   <Compile Remove="**\**\*.Android.cs" />   <None Include="**\**\*.Android.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
   <Compile Remove="**\Android\**\*.cs" />   <None Include="**\Android\**\*.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework.StartsWith('net6.0-ios')) != true AND $(TargetFramework.StartsWith('net6.0-maccatalyst')) != true"> 
   <Compile Remove="**\**\*.iOS.cs" />   <None Include="**\**\*.iOS.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />  
   <Compile Remove="**\iOS\**\*.cs" />   <None Include="**\iOS\**\*.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />  
</ItemGroup>

<ItemGroup Condition="$(TargetFramework.Contains('-windows')) != true ">
    <Compile Remove="**\*.Windows.cs" />   <None Include="**\*.Windows.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
    <Compile Remove="**\Windows\**\*.cs" />   <None Include="**\Windows\**\*.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" /> 
</ItemGroup>
like image 129
Rob Caplan - MSFT Avatar answered Sep 13 '25 20:09

Rob Caplan - MSFT