编辑器中的 wpf 转换器 "token is not valid",来自扩展 WPF 工具包的 ColorPicker

wpf converter "token is not valid" in editor, ColorPicker from extended WPF toolkit

我做了一个从字符串到颜色的转换器,然后再返回,当 运行 时它工作正常,但在编辑器上它只是抛出一个 "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 会将 DependencyProperty.UnsetValue 传递给您的转换器。您的转换器不会检查这种情况,而只是将传递给字符串的任何内容进行转换。像这样改变你的转换器(注意我只更新了 Convert 方法,没有触及 ConvertBack 可能需要修复,但这与这个问题无关):

public class MaddoColorConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        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) {
                throw new FormatException($"String {c} does not represent a valid color", ex);
            }
            if (convertedColor != null) {
                color = (Color) convertedColor;
            }
        }

        return color;
    }
}

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

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