Control Enhancements in .NET MAUI 9
Please, support my blog by clicking on our sponsors ad!
.NET MAUI 9 introduces several exciting control enhancements that boost the performance, functionality, and flexibility of cross-platform apps. This blog explores these updates in detail, providing code examples to help you integrate these features into your .NET MAUI applications.
1. BackButtonBehavior OneWay Binding Mode
In previous versions of .NET MAUI, the BackButtonBehavior
in a Shell
app was set to BindingMode.OneTime
. In .NET MAUI 9, this has been updated to BindingMode.OneWay
, allowing easier control over the back button behavior at runtime. This means the button’s visibility and other properties can be dynamically updated with data bindings.
Example Code:
The following code demonstrates how to use data binding to control the visibility and behavior of the back button in a Shell
app.
<ContentPage ...>
<Shell.BackButtonBehavior >
<BackButtonBehavior Command="{Binding BackCommand}"
IsVisible="{Binding IsBackButtonVisible}"
IconOverride="back.png" / >
</Shell.BackButtonBehavior >
...
</ContentPage >;
In this example, the IsBackButtonVisible
and BackCommand
properties are bound to the view model. The visibility of the back button can now be dynamically controlled through data binding.
2. BlazorWebView Hosting Update (iOS and Mac Catalyst)
On iOS and Mac Catalyst 18, the default behavior for hosting content in a BlazorWebView
has changed to localhost. Previously, a 0.0.0.0
address was used. The new update ensures better compatibility and security when rendering Blazor content inside a MAUI app.
Enabling Legacy Behavior
If you prefer using the older 0.0.0.0
address, you can opt into it by setting the appropriate switch in your MauiProgram
class:
// Set this switch to use the LEGACY behavior of always using 0.0.0.0 to host BlazorWebView
AppContext.SetSwitch("BlazorWebView.AppHostAddressAlways0000", true);
This snippet reverts the behavior to the legacy host address for backward compatibility.
3. CollectionView and CarouselView Performance Improvements
.NET MAUI 9 introduces new optional handlers for CollectionView
and CarouselView
on iOS and Mac Catalyst, leveraging UICollectionView
APIs. These handlers improve performance and stability in scenarios involving complex lists or scrolling content.
Enabling the New Handlers
To opt into using these new handlers, you need to modify the MauiProgram
class in your app:
#if IOS || MACCATALYST
builder.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler<Microsoft.Maui.Controls.CollectionView,
Microsoft.Maui.Controls.Handlers.Items2.CollectionViewHandler2>();
handlers.AddHandler<Microsoft.Maui.Controls.CarouselView,
Microsoft.Maui.Controls.Handlers.Items2.CarouselViewHandler2&glt;();
});
#endif
This configuration adds the new CollectionViewHandler2
and CarouselViewHandler2
, which are optimized for performance, particularly for large collections of items.
4. Label Text Alignment (Justify)
One of the highly requested features in previous versions was the ability to justify text in a Label
. .NET MAUI 9 introduces a new HorizontalTextAlignment
option for justified text alignment.
Example Code:
The following XAML example shows how to use HorizontalTextAlignment
with the value Justify
:
<Label Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit.
In facilisis nulla eu felis fringilla vulputate."
HorizontalTextAlignment="Justify"/ >
This ensures that the text will be aligned evenly on both the left and right sides of the label, providing a cleaner look for blocks of text.
5. Soft Keyboard Input Support for Date, Time, and Password
In .NET MAUI 9, soft keyboard input for Password
, Date
, and Time
fields has been enhanced. You can now define the expected keyboard type for Entry
and Editor
controls, making the user input experience smoother.
Example Code:
The following example demonstrates how to specify different keyboard types in XAML:
<Entry Keyboard="Date" / >
<Entry Keyboard="Time" / >
<Entry Keyboard="Password" IsPassword="True" / >
This update improves user input by displaying context-appropriate keyboards for date, time, and password fields.
6. TimePicker Enhancements
The TimePicker
control now has a new event, TimeSelected
, which fires when the user selects a new time. This event provides the new and old times through TimeChangedEventArgs
, making it easier to track changes.
Example Code:
The following example demonstrates how to handle the TimeSelected
event in a .NET MAUI app:
<TimePicker x:Name="timePicker"
TimeSelected="OnTimeSelected" / >
// C# Code-Behind
private void OnTimeSelected(object sender, TimeChangedEventArgs e)
{
var newTime = e.NewTime;
var oldTime = e.OldTime;
Console.WriteLine($"Time changed from {oldTime} to {newTime}");
}
This event makes it easier to respond to time selection changes in your application, such as updating related UI elements or triggering additional logic.
7. WebView ProcessTerminated Event
The WebView
control in .NET MAUI 9 has been enhanced with a ProcessTerminated
event, which is raised when the web view process ends unexpectedly. This allows for more robust error handling when embedding web content in your app.
Example Code:
The following example demonstrates how to subscribe to the ProcessTerminated
event:
<WebView x:Name="webView" ProcessTerminated="OnProcessTerminated" / >
// C# Code-Behind
private void OnProcessTerminated(object sender, WebViewProcessTerminatedEventArgs e)
{
Console.WriteLine("WebView process terminated unexpectedly.");
// Handle process termination here
}
By handling this event, you can take corrective action or display a message to the user when a WebView
process is unexpectedly terminated.
Conclusion
.NET MAUI 9 introduces several powerful control enhancements that improve the overall developer experience and app performance. Whether you’re dealing with back button behaviors, BlazorWebView, or list performance improvements, these updates provide more control and flexibility when building cross-platform applications.
Try out these features in your next .NET MAUI project and see how they can simplify your code and enhance your app’s performance!
Comments
Post a Comment