Search code examples
c#winui-3brushsolidcolorbrush

Converting Brush to SolidColorBrush WinUi 3


Im moving WPF project to WinUi 3. I would like to convert Brushes.Red/Green to SolidColorBrush. It's used by xaml elements' properties like Background.

public SolidColorBrush PowerBrush
{
    get
    {
        if (_powerMode == PowerMode.Off)
        {
            //return (SolidColorBrush)Application.Current.Resources["SystemFillColorCriticalBrush"]; 
            return new SolidColorBrush(Brushes.Red);
        }
        else
        {
            //return (SolidColorBrush)Application.Current.Resources["SystemFillColorSuccessBrush"];
            return Brushes.Green;
        }

    }
}

I got errors:

Cannot convert from 'System.Drawing.Brush' to 'Windows.UI.Color'

Cannot implicitly convert type 'System.Drawing.Brush' to 'Microsoft.UI.Xaml.Media.SolidColorBrush'

How to do it?


Solution

  • There is no way to convert from Brush used in WPF to SolidColorBrush used in WinUi 3.

    The easies way is to use Microsoft.UI.Colors instead of System.Drawing.Brushes for WinUi 3.

    public SolidColorBrush PowerBrush
    {
        get
        {
            if (_powerMode == PowerMode.Off)
            {
                return new(Colors.OrangeRed);
            }
            else
            {
                return new(Colors.Green);
            }
        }
    }