Home > OS >  How to load a view with a converter in another assembly [MAUI.NET]
How to load a view with a converter in another assembly [MAUI.NET]

Time:01-27

I have a converter

using System.Globalization;
namespace WidgetsForRuntimeInjection.Converters
{
    public class SampleBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        { ... }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        { ... }
    }
}

and a view

<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:viewModels="clr-namespace:WidgetsForRuntimeInjection.ViewModels"
             xmlns:converters="clr-namespace:WidgetsForRuntimeInjection.Converters;assembly=WidgetsForRuntimeInjection"
             x:Class="WidgetsForRuntimeInjection.Views.SampleWidgetsForInjection">
    <converters:SampleBooleanConverter x:Key="SampleBooleanConverter"/>
    <DataTemplate x:DataType="viewModels:SwitchViewModel" x:Key="SwitchDataTemplate">
        <Switch IsToggled="{Binding Value, Converter={StaticResource SampleBooleanConverter}}" IsEnabled="{Binding ReadWrite}"/>
    </DataTemplate>
</ResourceDictionary>

both of them are in a single assembly that is build as dll.

Then in another assembly I am loading it and adding to an application resources.

var assembly = Assembly.LoadFile( ... path to dll... );
var resources = assembly.CreateInstance("WidgetsForRuntimeInjection.Views.SampleWidgetsForInjection");
App.Current.Resources.Add("SwitchDataTemplate", (resources as ResourceDictionary)["SwitchDataTemplate"]);

And I got error like that:

Microsoft.Maui.Controls.Xaml.XamlParseException: 'Position 7:6. Type converters:SampleBooleanConverter not found in xmlns clr-namespace:WidgetsForRuntimeInjection.Converters;assembly=WidgetsForRuntimeInjection'

But I can create an instance of this converter a line before (in this second assembly).

var converter = assembly.CreateInstance("WidgetsForRuntimeInjection.Converters.SampleBooleanConverter");

And of course with no converter scenario it all works like a charm.

How to make it work? I tried do Converter as DynamicResource and declaring namespace for converters differently.

UPDATE:

That is kind of weird, because it seems to work after I have added same in alternative way - in xaml.cs file like below:

public partial class SampleWidgetsForInjection : ResourceDictionary
{
    public SampleWidgetsForInjection()
    {
        this.Add("SampleBooleanToColorConverter", new SampleBooleanToColorConverter());
        InitializeComponent();
    }
}

CodePudding user response:

So it seems this is a workaround / solution:

public partial class SampleWidgetsForInjection : ResourceDictionary
{
    public SampleWidgetsForInjection()
    {
        this.Add("SampleBooleanToColorConverter", new SampleBooleanToColorConverter());
        InitializeComponent();
    }
}
  • Related