András Tóth‘s professional blog
banditoth.net

Hey there 👋, I’m banditoth a .NET MAUI developer from Hungary.
I write about software development with .NET technologies.

You can find me on:
LinkedIn | Github | StackOverflow | X / Twitter | Threads

  • .NET MAUI Android Error: Type androidx.collection.ArrayMapKit is defined multiple times

    This content has 2 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

    One common challenge is AndroidX dependency conflicts. In this blog post, we’ll guide you through resolving compilation errors related to AndroidX in .NET MAUI. Before we proceed, let’s take a look at the error message that may have troubled you:

    /Users/Username/Documents/src/ProjectFolder/Project: Error JAVA0000: Error in /Users/Username/.nuget/packages/xamarin.androidx.collection.jvm/1.3.0.1/buildTransitive/net6.0-android31.0/../../jar/androidx.collection.collection-jvm.jar:androidx/collection/ArrayMapKt.class:
    Type androidx.collection.ArrayMapKt is defined multiple times: /Users/Username/.nuget/packages/xamarin.androidx.collection.jvm/1.3.0.1/buildTransitive/net6.0-android31.0/../../jar/androidx.collection.collection-jvm.jar:androidx/collection/ArrayMapKt.class, /Users/Username/.nuget/packages/xamarin.androidx.collection.ktx/1.2.0.5/buildTransitive/net6.0-android31.0/../../jar/androidx.collection.collection-ktx.jar:androidx/collection/ArrayMapKt.class
    Compilation failed
    java.lang.RuntimeException: com.android.tools.r8.CompilationFailedException: Compilation failed to complete, origin: /Users/Username/.nuget/packages/xamarin.androidx.collection.jvm/1.3.0.1/buildTransitive/net6.0-android31.0/../../jar/androidx.collection.collection-jvm.jar
    androidx/collection/ArrayMapKt.class
    
    

    In my case the meaning of this error message is: Type androidx.collection.ArrayMapKt is defined multiple times

    Examine Dependencies and Manually Delete bin and obj Folders:

    Start by inspecting your project’s dependencies. Ensure that you have the same versions of .NET MAUI packages and other libraries. Dependency mismatches can often lead to compilation errors.

    Sometimes, cleaning your project isn’t enough.
    To ensure a fresh build, you might need to manually delete the bin and obj folders. You can find these folders in your project directory. They contain build artifacts and removing them helps clear cached data.

    Verify NuGet Packages:

    Review your NuGet packages. Look out for multiple versions of the same library, as this can lead to conflicts. If you find any conflicting packages, remove the outdated or conflicting versions. You may need to edit your project’s .csproj file to resolve these package issues.

    Additionally, if you’ve recently installed a new NuGet package with has an Android dependency, make sure you have the correct version. A different version might introduce incompatibilities with the already installed pacakages.

  • Troubleshooting Xamarin and .NET MAUI: iOS Deployment Issues after XCode Upgrade

    This content has 2 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

    Are you facing deployment issues with your Xamarin or .NET MAUI iOS app after upgrading XCode? You’re not alone. Many developers encounter the frustrating “/usr/bin/xcrun exited with code 1” error message, coupled with the “actool exited with code 1” and an error about failing to locate a simulator runtime. In this blog post, we’ll delve into this problem and provide you with a solution to get your iOS app deployment back on track.

    Understanding the Problem

    After upgrading XCode to a newer version, you may notice that you can’t deploy your Xamarin or .NET MAUI iOS app to physical iOS devices, and the simulator targets are mysteriously missing from the drop-down menu where you select deployment targets. This issue can be perplexing and hinder your development workflow.

    The error message you encounter typically looks something like this:

    Resolution

    To tackle this deployment challenge, you should delve into your XCode configuration and confirm that the iOS platform is both accessible and correctly installed on your development machine. Follow these steps:

    1. Launch XCode on your Mac.
    2. Click on “XCode” in the top menu bar and choose “Preferences.” This will open the XCode preferences window.
    3. Within the preferences window, select the “Platforms” section. Here, you’ll find the key settings related to platform configuration.
    4. After you’ve confirmed that all the required iOS components are installed, close and restart Visual Studio (or your preferred development environment).
  • .NET MAUI: Enhancing apps with Microsoft App Center: Analytics and Diagnostics

    This content has 2 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

    As the world of software development continues to evolve, building robust and responsive applications has become crucial. Microsoft’s App Center offers a comprehensive suite of tools designed to streamline the development process and elevate user experiences. In this extended blog post, we’ll dive into integrating Microsoft App Center into .NET MAUI projects, specifically focusing on how Analytics and Diagnostics can provide invaluable insights for developers and project owners.

    What is Microsoft App Center?

    Microsoft App Center is a unified platform that empowers developers to build, test, distribute, and monitor mobile and desktop applications. By offering a wide range of tools and services, App Center enhances collaboration, accelerates development, and ensures the quality and performance of applications.

    Analytics and Diagnostics: Why Are They Important?

    1. Analytics: App Center Analytics provides comprehensive insights into user behavior, enabling developers and project owners to make data-driven decisions. By tracking user interactions, app usage patterns, and feature engagement, you can understand how users are interacting with your application. This data is invaluable for optimizing user experiences, improving user flows, and tailoring your app’s functionalities to better meet user needs.
    2. Diagnostics: Diagnosing and resolving issues quickly is essential for maintaining a positive user experience. App Center Diagnostics helps you monitor crashes and exceptions in real-time, allowing you to identify and address problems as they arise. This feature significantly reduces downtime and enhances app stability, thereby improving user satisfaction and retention.

    Integrating Microsoft App Center into .NET MAUI Projects:

    Integrating Microsoft App Center into your .NET MAUI projects is a straightforward process. The steps closely resemble the integration with Xamarin.Forms.

    Set Up App Center

    1. Create an account on App Center if you don’t have one already.
    2. Follow steps on this link to create new projects within the AppCenter portal: https://learn.microsoft.com/en-us/appcenter/sdk/getting-started/xamarin.

    Add SDK to Your Project

    Install the Microsoft.AppCenter and Microsoft.AppCenter.Analytics packages via NuGet.

    Create a Crash Analytics Service

    Create an interface and an implementation for the crash analytics features (Analytics and Diagnostics):

    public interface ICrashAnalyticsService
    {
        void TrackEvent(string eventName, Dictionary<string, string>? properties = null);
        void TrackError(Exception exception, Dictionary<string, string>? properties = null);
    }
    
    public class CrashAnalyticsService : ICrashAnalyticsService
    {
        public void TrackEvent(string eventName, Dictionary<string, string>? properties = null)
        {
            Analytics.TrackEvent(eventName, properties);
        }
    
        public void TrackError(Exception exception, Dictionary<string, string>? properties = null)
        {
            Crashes.TrackError(exception, properties);
        }
    }
    

    Initialization and dependency registration

    In your MauiProgram.cs file, register the ICrashAnalyticsService in the ServiceProvider:

    using Microsoft.Maui.Controls.Hosting;
    using Microsoft.Extensions.DependencyInjection;
    using YourNamespace.Services; // Import the namespace for your service implementation
    
    public static MauiApp CreateMauiAppBuilder()
    {
        var builder = MauiApp.CreateBuilder();
        // Other configurations
        .....
        builder.Services.AddTransient<ICrashAnalyticsService, CrashAnalyticsService>();
    
        AppCenter.Start("YOUR_APP_CENTER_SECRET",
            typeof(Analytics), typeof(Crashes));
    
        return builder.Build();
    }
    

    Inject and Use Crash Analytics Service:

    In your ViewModel or code-behind (or wherever you want to use analytics or crash reporting), inject the ICrashAnalyticsService and use it to track events and errors:

    using Microsoft.Maui.Controls;
    using YourNamespace.Services; // Import the namespace for the service
    
    public class MainViewModel : BindableObject
    {
        private readonly ICrashAnalyticsService _crashAnalyticsService;
    
        public MainViewModel(ICrashAnalyticsService crashAnalyticsService)
        {
            _crashAnalyticsService = crashAnalyticsService;
        }
    
        public void ImportantButtonClick()
        {
            // Track an analytics event
            var properties = new Dictionary<string, string>
            {
                { "UserID", "123" },
                { "Action", "ButtonClick" }
            };
            _crashAnalyticsService.TrackEvent("ImportantButtonClick", properties);
    
            // Simulate an exception and track the error
            try
            {
                // Simulate an error
                throw new Exception("Simulated error");
            }
            catch (Exception ex)
            {
                _crashAnalyticsService.TrackError(ex, properties);
            }
        }
    }
    

    Conclusion:

    Microsoft App Center is a powerful ally for .NET MAUI developers, offering robust tools for application development, testing, distribution, and monitoring. Analytics and Diagnostics components provide insights that empower developers and project owners to make informed decisions, optimize user experiences, and ensure application stability. By integrating App Center into your .NET MAUI projects, you’re embracing a proactive approach to delivering high-quality, user-centric applications.

    Note: Although .NET MAUI support isn’t available on App Center at the time of writing, it’s important to stay informed about updates. The .NET MAUI community is actively evolving, and it’s possible that support for .NET MAUI on App Center will be available in the near future. This would streamline the integration process and provide a dedicated platform for managing your .NET MAUI applications.

  • .NET MAUI : Creating Customized Popups with Mopups

    This content has 2 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.


    Popups are a powerful UI element to engage users with important information, actions, or requests. While the .NET MAUI CommunityToolkit offers simple popup functionality, sometimes you may need more customization and control over your popups. In this tutorial, we will explore how to implement custom popups using the Mopups library in .NET MAUI.

    Prerequisites:

    • A basic understanding of XAML and C#.

    Install NuGet Package

    Open your .NET MAUI project and install the “Mopups” NuGet package by LuckyDucko.

    Initialize Mopups


    In your MauiProgram.cs file, within the CreateMauiAppBuilder method, call .ConfigureMopups() on the host builder to initialize the Mopups library:

    using Mopups;
    // ...
    
    public static MauiApp CreateMauiAppBuilder()
    {
        var builder = MauiApp.CreateBuilder();
        // Other configurations
        builder.ConfigureMopups();
        return builder.Build();
    }
    

    Create a Custom PopupPage


    Add a new ContentPage to your project and modify the base class of the XAML file to PopupPage. Add the necessary namespace declaration for the Mopups.Pages namespace:

    <?xml version="1.0" encoding="utf-8" ?>
    <pages:PopupPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
                      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                      xmlns:pages="clr-namespace:Mopups.Pages;assembly=Mopups"
                      x:Class="YourNamespace.CustomPopupPage">
        <!-- Your custom popup content here -->
    </pages:PopupPage>
    

    Create and Display the Popup


    In your code-behind or ViewModel, you can use the MopupService to create and display your custom popup:

    using Mopups.Services;
    // ...
    
    private async Task ShowCustomPopupAsync()
    {
        await MopupService.Instance.PushAsync(new CustomPopupPage());
    }
    

    Tip: Adding Background Overlay


    To create a partially opaque background that covers the content underneath the popup, you can set the BackgroundColor property of your PopupPage to a HEX color with transparency, like “#99272727”:

    <pages:PopupPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
                      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                      xmlns:pages="clr-namespace:Mopups.Pages;assembly=Mopups"
                      x:Class="YourNamespace.CustomPopupPage"
                      BackgroundColor="#99272727">
        <!-- Your custom popup content here -->
    </pages:PopupPage>
    

    Conclusion


    The Mopups library offers a robust solution for creating customized popups in .NET MAUI applications. With the ability to fully customize the layout and presentation of your popups, Mopups provides the flexibility needed to create engaging user experiences. By following this tutorial, you can easily integrate Mopups into your .NET MAUI projects and leverage its features to create unique and interactive popups for your users.

  • .NET MAUI: iOS ListView disappearing cells

    This content has 2 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

    In this article, we will delve into a persistent .NET MAUI issue affecting ListViews on the iOS platform in .NET 7 builds. While the .NET 8 preview has addressed this issue, developers seeking a solution can employ the BindableLayout as a temporary workaround. We’ll also explore the concept of BindableLayout and touch on the CachingStrategy within ListViews.

    The Problem: Disappearing Cells in ListViews on iOS

    The issue at hand revolves around the behavior of ListViews on the iOS platform in .NET 7 builds. As users scroll through the list elements, the ListView cells mysteriously disappear, causing a jarring experience. While the .NET 8 preview has resolved this vexing problem, the official release is still pending, leaving developers seeking immediate solutions.

    The Workaround: BindableLayout within ScrollView

    A viable workaround to mitigate the disappearing cell issue involves utilizing the BindableLayout within a ScrollView. The BindableLayout.ItemSource property can be harnessed to mimic the ListView’s behavior. However, it’s crucial to acknowledge that this solution might not deliver the same performance as a native ListView.

    Example Code

    Here’s how you can implement the BindableLayout workaround:

    <ScrollView>
        <StackLayout>
            <BindableLayout.ItemsSource>
                <x:Array Type="{x:Type local:ItemModel}">
                    <local:ItemModel Name="Item 1" />
                    <local:ItemModel Name="Item 2" />
                    <local:ItemModel Name="Item 3" />
                    <!-- Add more items here -->
                </x:Array>
            </BindableLayout.ItemsSource>
            <BindableLayout.ItemTemplate>
                <DataTemplate>
                    <Label Text="{Binding Name}" />
                </DataTemplate>
            </BindableLayout.ItemTemplate>
        </StackLayout>
    </ScrollView>
    

    BindableLayout: A Glimpse

    BindableLayout is a versatile feature within the .NET MAUI framework that allows developers to easily bind collections to layout controls. It’s an excellent alternative when dealing with scenarios where a native ListView isn’t performing optimally or in cases like the aforementioned issue. BindableLayout empowers developers to achieve the desired UI layout while maintaining data synchronization.

    Use BindableLayout when you want more control over the layout of your items, need to create a dynamic UI with varying layouts, and don’t necessarily require built-in performance optimizations.

    Understanding CachingStrategy within ListViews

    ListViews in .NET MAUI come with a property known as CachingStrategy. This property determines how the ListView should cache its visual elements, significantly influencing performance. There are three options:

    1. RecycleElement: This strategy reuses existing cells, enhancing memory usage and rendering speed. However, it might pose issues when complex cell layouts are used.
    2. RetainElement: This strategy preserves cells for the duration of the ListView’s existence. While memory consumption can be higher, it can be useful for more intricate cell layouts.
    3. None: In this strategy, no caching is employed, causing cells to be created and destroyed frequently. While it minimizes memory usage, it can have an adverse impact on performance.

    Conclusion

    While the .NET MAUI framework continues to evolve, issues like the disappearing ListView cells on iOS in .NET 7 builds are inevitable. Developers eagerly anticipate the benefits that .NET 8 will bring, including the resolution of this particular problem. In the interim, the BindableLayout within a ScrollView offers a workaround that replicates the ListView’s behavior, albeit with potential performance differences. By understanding concepts like BindableLayout and the CachingStrategy within ListViews, developers can navigate these challenges while continuing to create engaging and efficient cross-platform applications. Stay tuned for the official .NET 8 release and more innovations that will undoubtedly enhance the .NET MAUI experience.

    https://github.com/dotnet/maui/issues/11640