.Net Maui Stacklayout und der Scrollviewer

In der aktuellen Version von .Net Maui gibt es einen lustigen Fehler. Dieser tritt bei Android und wenn ein Stacklayout scrollen lassen will. Das funktioniert beim ersten Mal super, jedoch passiert es das beim zweiten Mal die Seite einfach leer bleibt. Am besten ein Grid verwenden.

using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace MyApp
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();

            // Create the ScrollView
            var scrollView = new ScrollView();

            // Create the StackLayout
            var stackLayout = new StackLayout();

            // Add some content to the StackLayout
            for (int i = 1; i <= 20; i++)
            {
                var label = new Label
                {
                    Text = $"Item {i}",
                    FontSize = 20,
                    HorizontalOptions = LayoutOptions.Center
                };

                stackLayout.Children.Add(label);
            }

            // Set the StackLayout as the content of the ScrollView
            scrollView.Content = stackLayout;

            // Set the ScrollView as the content of the page
            Content = scrollView;
        }
    }
}

Code Schnipsel: Service Provider

Dependency Injection macht das Leben einfacher auch in .NET MAUI. Hin und wieder ist es notwendig ein Service zu referenzieren das nicht über den Konstruktor kommt. Hier eine simple Lösung dafür:

public static class ServiceProvider
{
    public static TService GetService<TService>()
        => Current.GetService<TService>();

    public static IServiceProvider Current
        =>
#if WINDOWS10_0_17763_0_OR_GREATER
			MauiWinUIApplication.Current.Services;
#elif ANDROID
            MauiApplication.Current.Services;
#elif IOS || MACCATALYST
			MauiUIApplicationDelegate.Current.Services;
#else
			null;
#endif
}