Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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# wpf转换器“;令牌无效";在编辑器中,来自扩展WPF工具包的颜色选择器_C#_Wpf_Xaml_Ivalueconverter_Wpf Extended Toolkit - Fatal编程技术网

C# wpf转换器“;令牌无效";在编辑器中,来自扩展WPF工具包的颜色选择器

C# wpf转换器“;令牌无效";在编辑器中,来自扩展WPF工具包的颜色选择器,c#,wpf,xaml,ivalueconverter,wpf-extended-toolkit,C#,Wpf,Xaml,Ivalueconverter,Wpf Extended Toolkit,我做了一个从字符串到颜色再到字符串的转换器,运行时效果很好,但在编辑器上它只是抛出一个“Token is not valid.”错误并阻止编辑器显示,这真的很烦人,因为它阻止我使用可视化编辑器 我从扩展WPF工具包中为ColorPicker制作了转换器 以下是转换器代码: public class MaddoColorConverter : IValueConverter { public object Convert(object value, Type targetType, obj

我做了一个从字符串到颜色再到字符串的转换器,运行时效果很好,但在编辑器上它只是抛出一个“Token is not valid.”错误并阻止编辑器显示,这真的很烦人,因为它阻止我使用可视化编辑器

我从扩展WPF工具包中为ColorPicker制作了转换器

以下是转换器代码:

public class MaddoColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Color color = Colors.Black;

        if (value != null && !string.IsNullOrWhiteSpace(value.ToString()))
        {
            string c = value.ToString();
            var convertedColor = ColorConverter.ConvertFromString(c);
            if (convertedColor != null)
            {
                color = (Color) convertedColor;
            }
        }

        return color;

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null)
        {
            Color color = (Color)value;
            Debug.WriteLine(color.ToString());
            return color.ToString();
        }
        return string.Empty;
    }
}
下面是来自表单xaml的一些相关片段:

<Window.Resources>        
    <wpfCatalog:MaddoColorConverter x:Key="ColorConverter" />
</Window.Resources>

<xctk:ColorPicker Grid.Row="3" Grid.Column="2" SelectedColor="{Binding ColoreTestoRGB, Converter={StaticResource ColorConverter}}"/>

您需要向
MaddoColorConverter
添加更多检查。例如,如果绑定失败,WPF将向转换器传递
dependencProperty.UnsetValue
。转换器不检查这种情况,而只是将传递给字符串的内容进行转换。这样更改转换器(注意,我只更新了
Convert
方法,没有触摸
ConvertBack
,这可能需要修复,但与此问题无关):

如果出于任何原因,预期在设计时颜色值无效-在设计时不要抛出异常,如下所示:

private static readonly DependencyObject _dummy = new DependencyObject();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
    System.Windows.Media.Color color = Colors.Black;

    if (value != null && value != DependencyProperty.UnsetValue && value is string && !String.IsNullOrWhiteSpace((string) value)) {
        string c = (string) value;
        object convertedColor = null;
        try {
            convertedColor = ColorConverter.ConvertFromString(c);
        }
        catch (Exception ex) {
            if (!DesignerProperties.GetIsInDesignMode(_dummy)) {
                throw new FormatException($"String {c} does not represent a valid color", ex);
            }
        }
        if (convertedColor != null) {
            color = (Color) convertedColor;
        }
    }

    return color;
}

ColoreTestoRGB源属性的类型是什么?它返回什么值?这需要是ColorConverter实际理解的有效值,例如“#000”。这是一个字符串属性,它在字典中查找正确的值或返回默认的一个公共字符串ColoreTestoRGB{get{return PicSettings.GetString(“ColoreTestoRGB”);}集{PicSettings.Set(“coloreTestoRGB”,value);RaisePropertyChanged(“coloreTestoRGB”);}}显然,您不能传递字符串“coloreTestoRGB”转换为转换器中的ColorConverter.ConvertFromString方法,但未获得异常。这修复了无效令牌错误,但现在我得到一个关于字符串不表示有效颜色的错误,这很有意义,因为它是一个查找字典的属性,因此它仅在运行时是有效颜色。我如何修复它?这是什么
\u dummy
应该是?dummy object检查我们是否处于设计模式(查看它是如何使用的)它就在那里,并给我一个未定义的对象错误,我必须在某个地方声明它吗?啊,对不起,我复制粘贴的代码错误,确实错过了声明。修复。
private static readonly DependencyObject _dummy = new DependencyObject();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
    System.Windows.Media.Color color = Colors.Black;

    if (value != null && value != DependencyProperty.UnsetValue && value is string && !String.IsNullOrWhiteSpace((string) value)) {
        string c = (string) value;
        object convertedColor = null;
        try {
            convertedColor = ColorConverter.ConvertFromString(c);
        }
        catch (Exception ex) {
            if (!DesignerProperties.GetIsInDesignMode(_dummy)) {
                throw new FormatException($"String {c} does not represent a valid color", ex);
            }
        }
        if (convertedColor != null) {
            color = (Color) convertedColor;
        }
    }

    return color;
}