简体   繁体   中英

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

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();
    }
}

So it seems this is a workaround / solution:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM