Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 27, 2023 08:15 pm GMT

.NET MAUI Blazor - Best practices for mobile UI

Both Blazor and .NET MAUI are great and Powerful frameworks. Using .NET MAUI, you can build fully native cross-platform apps and build web apps completely in C# with Blazor.
But when you combine them with the MAUI Blazor template, you need to know that it should feel like a mobile app, not a web page! So, in this article, I will show you some tips and tricks.

Disable selecting content and texts

There is no need to select all texts and contents in your app! So, you can disable it in your CSS with these properties:

user-select: none;-webkit-user-select: none;

Remove the default tapping effect

Mostly in mobile devices, when you tap on clickable elements like a tag, it shows some effects. In my experience, Android was showing an ugly blue color and in iOS was a gray one. So, give it your own beautiful effect using active selector in CSS and remove the default one with this CSS property:

-webkit-tap-highlight-color: transparent;

Annoying scroll animation

Sometimes you dont want your app (mostly iOS) to have animation when the scroll hits the end of it, so you can disable it with this CSS property:

overscroll-behavior: none;

Handling Safe areas

Dont worry its not Blazor or MAUIs fault that your content is in the Status bar or Home bar. You can fix it in CSS like this:

@supports (-webkit-touch-callout: none) {    body {        padding-top: env(safe-area-inset-top);        padding-right: env(safe-area-inset-right);        padding-bottom: env(safe-area-inset-bottom);        padding-left: env(safe-area-inset-left);    }}

Changing Android default colors

For those who are not familiar with native stuff, you can change the accent color and status bar color of your Android app from YOURPROJECTNAME\Platforms\Android\Resources\values\colors.xml.

Annoying startup background

When you run your app, you see a Loading text with a white background so lets fix that!
For Loading text you need to remove it from a div tag with app id in your index.html, then access the BlazorWebViews handler in your MainPage.xaml.cs and change the default background color of the platforms that you need like this:

BlazorWebViewHandler.BlazorWebViewMapper.AppendToMapping("CustomBlazorWebViewMapper", (handler, view) =>        {#if WINDOWS            //Workaround for WinUI splash screen            if (AppInfo.Current.RequestedTheme == AppTheme.Dark)            {                handler.PlatformView.DefaultBackgroundColor = Microsoft.UI.Colors.Black;            }#endif#if IOS || MACCATALYST            handler.PlatformView.BackgroundColor = UIKit.UIColor.Clear;            handler.PlatformView.Opaque = false;#endif#if ANDROID            handler.PlatformView.SetBackgroundColor(Android.Graphics.Color.Transparent);#endif        });

By the way, I already made a sample repo containing all of the code we discussed. You can have it from this link:
https://github.com/mhrastegari/MauiBlazorUIBestPractices

Happy coding ;D


Original Link: https://dev.to/mhrastegari/net-maui-blazor-best-practices-for-mobile-ui-4def

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To