Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for .NET MAUI Blazor - Best practices for mobile UI
Mohammad Hossein Rastegarinia
Mohammad Hossein Rastegarinia

Posted on • Edited on

     

.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:

*:not(input){user-select:none;-webkit-user-select:none;}
Enter fullscreen modeExit fullscreen mode

Remove the default tapping effect

Mostly in mobile devices, when you tap on clickable elements likea 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 usingactive selector in CSS and remove the default one with this CSS property:

*{-webkit-tap-highlight-color:transparent;}
Enter fullscreen modeExit fullscreen mode

Handling Safe areas

Don’t worry it’s not Blazor or MAUI’s 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);}}
Enter fullscreen modeExit fullscreen mode

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 fromYOURPROJECTNAME\Platforms\Android\Resources\values\colors.xml.

<?xml version="1.0" encoding="utf-8"?><resources><colorname="colorPrimary">#512BD4</color><colorname="colorPrimaryDark">#2B0B98</color><colorname="colorAccent">#2B0B98</color></resources>
Enter fullscreen modeExit fullscreen mode

Annoying startup background

When you run your app, you see a “Loading…” text with a white background so let’s fix that!
For “Loading…” text you need to remove it from adiv tag withapp id in yourindex.html, then access the BlazorWebView’s handler in yourMainPage.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 screenif(AppInfo.Current.RequestedTheme==AppTheme.Dark){handler.PlatformView.DefaultBackgroundColor=Microsoft.UI.Colors.Black;}#endif#if IOS || MACCATALYSThandler.PlatformView.BackgroundColor=UIKit.UIColor.Clear;handler.PlatformView.Opaque=false;#endif#if ANDROIDhandler.PlatformView.SetBackgroundColor(Android.Graphics.Color.Transparent);#endif});
Enter fullscreen modeExit fullscreen mode

Annoying scroll animation

Sometimes you don’t want your app's body (mostly iOS) to have animation when the scroll hits the bottom or top of the page, so you can disable it in theBlazorWebView handler we talked about like this:

BlazorWebViewHandler.BlazorWebViewMapper.AppendToMapping("CustomBlazorWebViewMapper",(handler,view)=>{#if IOS || MACCATALYSThandler.PlatformView.ScrollView.Bounces=false;#endif#if ANDROIDhandler.PlatformView.OverScrollMode=Android.Views.OverScrollMode.Never;#endif});
Enter fullscreen modeExit fullscreen mode

By the way, I already made a sample repo containing all of the code we discussed. You can have itfrom here.
Also you can read about the second part of this article which is about Desktop UIfrom here.

Happy coding ;D

Top comments(9)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
amsmart profile image
Emmanuel Oluwagbemiga Adebiyi (Smart)
  • Location
    Nigeria
  • Work
    Software Engineer
  • Joined

Great post!

Don't you think for the last one, we could just build the Splash Screen with HTML and CSS in the index.html file?

CollapseExpand
 
mhrastegari profile image
Mohammad Hossein Rastegarinia
Buy me marshmallows and I'll be your friend. 👀 💻#WinUI,📱#MAUI and 🌐#Blazor Developer.

Yeah! I had this in my mind few days ago, but what about MAUI Splash Screen?
Can we remove it? or if we want it to be the same as MAUI how do we get the splash size? I mean you may know that MAUI Splash Screen has deferent sizes in each Android device and OS version.

CollapseExpand
 
msynk profile image
Saleh Yusefnejad
  • Work
    Co-founder & Tech Lead at bitplatform
  • Joined

great job Mohammad jaan 👍

can't you do something about colorizing the code samples?

CollapseExpand
 
mhrastegari profile image
Mohammad Hossein Rastegarinia
Buy me marshmallows and I'll be your friend. 👀 💻#WinUI,📱#MAUI and 🌐#Blazor Developer.
• Edited on• Edited

Done, Thanks for the suggestion dear Saleh! ❤️

CollapseExpand
 
ghobakhloum profile image
Mohammad Ghobakhlou
  • Joined

Thanks for the post specially for removing Annoying startup background

CollapseExpand
 
mhrastegari profile image
Mohammad Hossein Rastegarinia
Buy me marshmallows and I'll be your friend. 👀 💻#WinUI,📱#MAUI and 🌐#Blazor Developer.

Happy to hear it was useful for you

CollapseExpand
 
rezajananfar profile image
Amirreza Jananfar
  • Joined

Wow! Such a fascinating article...

CollapseExpand
 
mhrastegari profile image
Mohammad Hossein Rastegarinia
Buy me marshmallows and I'll be your friend. 👀 💻#WinUI,📱#MAUI and 🌐#Blazor Developer.

Thanks dear Amirreza ❤️

CollapseExpand
 
mrfarzam profile image
Info Comment hidden by post author - thread only accessible via permalink
mr-farzam
  • Joined

Lol

Comment hidden by post author

Some comments have been hidden by the post's author -find out more

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Buy me marshmallows and I'll be your friend. 👀 💻#WinUI,📱#MAUI and 🌐#Blazor Developer.
  • Joined

More fromMohammad Hossein Rastegarinia

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp