Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将颜色代码转换为media.brush?_C#_Wpf - Fatal编程技术网

C# 如何将颜色代码转换为media.brush?

C# 如何将颜色代码转换为media.brush?,c#,wpf,C#,Wpf,我有一个矩形,我想用颜色填充它。当我写入Fill=“#ffffffff 90”时,它会显示一个错误: 无法将类型“string”隐式转换为“System.Windows.Media.Brush” 请给我一些建议。在代码中,您需要显式创建画笔实例: Fill = new SolidColorBrush(Color.FromArgb(0xff, 0xff, 0x90)) 您可以使用与XAML读取系统相同的机制:类型转换器 var converter = new System.Windows.Med

我有一个矩形,我想用颜色填充它。当我写入
Fill=“#ffffffff 90”
时,它会显示一个错误:

无法将类型“string”隐式转换为“System.Windows.Media.Brush”


请给我一些建议。

在代码中,您需要显式创建
画笔
实例:

Fill = new SolidColorBrush(Color.FromArgb(0xff, 0xff, 0x90))
您可以使用与XAML读取系统相同的机制:类型转换器

var converter = new System.Windows.Media.BrushConverter();
var brush = (Brush)converter.ConvertFromString("#FFFFFF90");
Fill = brush;

您使用的是什么版本的WPF?我在3.5和4.0中都试过了,在XAML中,Fill=“#FF000000”应该可以在a中正常工作。但是,如果没有,还有另一种语法。这里有一个3.5 XAML,我用两种不同的方法进行了测试。更好的办法是使用资源

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Rectangle Height="100" HorizontalAlignment="Left" Margin="100,12,0,0" Name="rectangle1" Stroke="Black" VerticalAlignment="Top" Width="200" Fill="#FF00AE00" />
    <Rectangle Height="100" HorizontalAlignment="Left" Margin="100,132,0,0" Name="rectangle2" Stroke="Black" VerticalAlignment="Top" Width="200" >
        <Rectangle.Fill>
            <SolidColorBrush Color="#FF00AE00" />
        </Rectangle.Fill>
    </Rectangle>
</Grid>

用于WinRT(Windows应用商店应用程序)


抱歉来晚了!我在WinRT遇到了类似的问题。我不确定您使用的是WPF还是WinRT,但它们在某些方面确实有所不同(有些比其他更好)。希望这能帮助所有人,无论他们处于何种情况

老实说,您可以始终使用我创建的converter类中的代码,以便在C#code behind中重复使用和执行,或者在您使用它的任何地方:

我这样做的目的是,6位(RGB)或8位(ARGB)十六进制值可以任意使用

所以我创建了一个转换器类:

public class StringToSolidColorBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        var hexString = (value as string).Replace("#", "");

        if (string.IsNullOrWhiteSpace(hexString)) throw new FormatException();
        if (hexString.Length != 6 || hexString.Length != 8) throw new FormatException();

        try
        {
            var a = hexString.Length == 8 ? hexString.Substring(0, 2) : "255";
            var r = hexString.Length == 8 ? hexString.Substring(2, 2) : hexString.Substring(0, 2);
            var g = hexString.Length == 8 ? hexString.Substring(4, 2) : hexString.Substring(2, 2);
            var b = hexString.Length == 8 ? hexString.Substring(6, 2) : hexString.Substring(4, 2);

            return new SolidColorBrush(ColorHelper.FromArgb(
                byte.Parse(a, System.Globalization.NumberStyles.HexNumber),
                byte.Parse(r, System.Globalization.NumberStyles.HexNumber),
                byte.Parse(g, System.Globalization.NumberStyles.HexNumber),
                byte.Parse(b, System.Globalization.NumberStyles.HexNumber)));
        }
        catch
        {
            throw new FormatException();
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}
已将其添加到my App.xaml中:

<ResourceDictionary>
    ...
    <converters:StringToSolidColorBrushConverter x:Key="StringToSolidColorBrushConverter" />
    ...
</ResourceDictionary>

...
...
并在我的视图的Xaml中使用它:

<Grid>
    <Rectangle Fill="{Binding RectangleColour,
               Converter={StaticResource StringToSolidColorBrushConverter}}"
               Height="20" Width="20" />
</Grid>

真有魅力

旁注…
不幸的是,WinRT没有获得H.B.建议的
System.Windows.Media.BrushConverter
;因此,我需要另一种方法,否则我将创建一个VM属性,该属性从
矩形颜色
字符串属性返回一个
SolidColorBrush
(或类似的属性)。

为简单起见,您可以创建一个扩展:-

    public static SolidColorBrush ToSolidColorBrush(this string hex_code)
    {
        return (SolidColorBrush)new BrushConverter().ConvertFromString(hex_code);
    }
然后使用:-

 SolidColorBrush accentBlue = "#3CACDC".ToSolidColorBrush();

如何生成此代码。我还有另外一种颜色。这种颜色由三个两位数的十六进制数(
R
G
、和
B
)组成,再加上一个可选的
Alpha
(透明度)。您需要将这些数字从argb传递到
将颜色代码分成两组。然后你会看到他使用了最后三个参数。FromArgb(a,r,g,b)中会有4个参数他/她在谈论代码背后…你是对的,duh。通过dunce cap,谢谢。有可能使它成为一个单行程序吗?好的,最后我使用了
Fill=(SolidColorBrush)new BrushConverter().ConvertFromString(#76EB7E)),这是我在另一个SO答案中找到的,与这个答案几乎相同!美好的
 SolidColorBrush accentBlue = "#3CACDC".ToSolidColorBrush();