Using platform-specific page background

In the previous post I showed you how to use the Mica background for the Windows head application. Unfortunately Mica backdrop cannot be used with the other platforms.

In this one I want to show you how to use different backgrounds depending on the different targets.

Add a new class to the App project and named it PageExtensions.cs

internal static class PageExtensions
{
    public static void SetPageBackground(this Page page)
    {
        Brush brush;

#if WINDOWS && !HAS_UNO
        brush = new SolidColorBrush(Microsoft.UI.Colors.Transparent);
#elif __ANDROID__
        var stops = new GradientStopCollection();
        stops.Add(new GradientStop() { Color = ColorHelper.FromArgb(0xff, 0x3d, 0x3d, 0x8d), Offset = 0 });
        stops.Add(new GradientStop() { Color = ColorHelper.FromArgb(0xff, 0x77, 0x45, 0xff), Offset = 1 });
        brush = new LinearGradientBrush(stops, 0);
#else
        brush = (Brush)Application.Current.Resources["ApplicationPageBackgroundThemeBrush"];
#endif

        page.Background = brush;
    }
}

Now to use this extension method simply call this.SetPageBackground(); in the page constructor.

public sealed partial class MyPage : Page
{
    public MyPage()
    {
        this.InitializeComponent();
        this.SetPageBackground();
    }
}

Comments

Popular posts from this blog

Using Mica backdrop with UNO Platform applications

gRPC server in a WorkerService