How can I choose an appiconfg.svg file based on the platform?

dg2k 1,416 Reputation points
2023-05-05T05:48:05.18+00:00

A single icon file, as defined in MAUI project, doesn't work perfectly both for mobile and desktop. If the image is adjusted for mobile, it will be too small for desktop and vice versa.

Yes, I can abandon this icon definition approach and use native icon setting (say, using android resources and manifest file) but thought worth a try with this minimalist MAUI approach if at all possible.

Now, I defined two icon files: appiconfg.svg for mobile and appiconfg-win.svg (see below) for desktop (WinUI). Both SVG images are identical except for the image scale with respect to the canvas area.

How can I choose these icon files based on platform?

A SIDELINE WISHLIST:
As SVG is inherently scalable, I wish MAUI scales the icon image based on platform: something like:
scale={OnPlatform Default='0.5', WinUI = '1.0'}

<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#aaccff" />

<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg-win.svg" Color="Transparent" />

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,436 Reputation points Microsoft External Staff
    2023-05-05T05:59:52.8933333+00:00

    Hello,

    Please open the .csproj file of your MAUI project, then declare a specific icon for Windows and for other platforms.

    <ItemGroup>
        <!-- App icon for Windows -->
        <MauiIcon Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'"
                  Include=... />
    
        <!-- App icon for all other platforms -->
        <MauiIcon Include=... />
    </ItemGroup>
    

    You can also change the condition for different platforms: 'ios' 'maccatalyst' 'android' 'windows'.

    Note: Please clean the project and uninstall the app from the target platform to see your new icon.

    For more details, you could refer to Change the app icon - .NET MAUI | Microsoft Learn

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.