79597844

Date: 2025-04-29 07:46:06
Score: 1
Natty:
Report link

I have found a solution. I register the Converter within my Module.cs file, which inherits from IModule (sth. from the Prism framework). I guess, you can also register it within the App.xaml.cs file, but this file has no access to my converter file.

public class InfoModule : IModule
{
    public void OnInitialized(IContainerProvider containerProvider)
    {
        ConnectionStatusToColorConverter converter = containerProvider.Resolve<ConnectionStatusToColorConverter>();
        Application.Current.Resources.Add(nameof(ConnectionStatusToColorConverter), converter);
    }

    public void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterSingleton<ConnectionStatusToColorConverter>();
    }
}

In the end, I had to remove the creation of the converter within my xaml, since it seems like the creation within the xaml creates a new converter using the empty-parameter ctor. And if I want to use the converter, I have to use {StaticResource ConnectionStatusToColorConverter - so like before.

<UserControl.Resources>
<!-- this has to be removed -->
<localConverters:ConnectionStatusToColorConverter x:Key="ConnectionStatusToColorConverter"/> 
</UserControl.Resources>

<!-- Example Usage -->
<StackPanel Grid.Column="2" Orientation="Horizontal" VerticalAlignment="Center">
                            <md:PackIcon Kind="Web" VerticalAlignment="Center" 
                                         Foreground="{Binding ConnectionStatus, Converter={StaticResource ConnectionStatusToColorConverter}}"/>
</StackPanel>

I am not quite sure if this is sth. like the anti-patterns mentioned above, but it works exactly as I wanted now.

Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: 06luki06