Home > Blockchain >  Reference ValueConverter from XAML page in MAUI app
Reference ValueConverter from XAML page in MAUI app

Time:11-09

I am trying to use the BoolToObject converter class referenced here https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/converters/bool-to-object-converter

In my XAML page I included the following line

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">

I am trying to use it like as indicated in the reference page like so:

<ContentPage.Resources>
    <ResourceDictionary>
        <toolkit:BoolToObjectConverter x:Key="BoolToObjectConverter" TrueObject="42" FalseObject="0" />
    </ResourceDictionary>
</ContentPage.Resources>

But it says the type "BoolToObjectConverter" can not be found. What am I missing?

Thanks for taking the time to help me.

CodePudding user response:

When using the .NET MAUI Community Toolkit package, you need to call the extension method in your MauiProgram.cs file as follows:


using CommunityToolkit.Maui;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        
        // Initialise the toolkit
        builder.UseMauiApp<App>().UseMauiCommunityToolkit();

        // the rest of your logic...
    }
}

CodePudding user response:

For those facing a similar issue, here are the two things I had to do to resolve the issue:

  • Install CommunityToolkit.Maui package. I had mistakenly installed CommunityToolkit.Maui.Core instead.

  • As mentioned by Alexandar, make sure to call the extension method in MauiProgram.cs:

    builder.UseMauiApp().UseMauiCommunityToolkit();

  • Related