WPF XAML 中的不受控制的异常:指定的转换无效 [英] Not controlled exception in WPF XAML: The specified conversion is not valid

查看:43
本文介绍了WPF XAML 中的不受控制的异常:指定的转换无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MVVM WPF 应用程序.我有以下转换器:

I have a MVVM WPF application. I have below converter:

public class PrintIconVisibilityValueConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values[0] == null || values[1] == null) return Visibility.Collapsed;

        int item1 = (int)values[0];
        string item2 = (string)values[1];

        if (item1 > 0 || !string.IsNullOrEmpty(item2))
        {
            return Visibility.Visible;
        }
        else
        {
            return Visibility.Collapsed;
        }
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

我认为:

<Window.Resources>
   <classes:PrintIconVisibilityValueConverter x:Key="PrintIconVisibilityValueConverter"/>
</Window.Resources>

然后我在这个视图中有一个图像:

then I have an image in this view:

<Image Source="/MyImages;component/Images/PrintIco.png" 
       Height="15" Margin="20 0 5 0">
    <Image.Visibility>
            <MultiBinding Converter="{StaticResource PrintIconVisibilityValueConverter}">
                <Binding Path="Item1" />
                <Binding Path="Item2" />
            </MultiBinding>
    </Image.Visibility>
</Image>

Item1 和 Item2 是视图模型中的公共属性:

Item1 and Item2 are public properties in view model:

private string _item2 = string.Empty;
public string Item2
{
    get
    {
        return _item2;
    }

    set
    {
        if (_item2 == value) return;

        _item2 = value;
        OnPropertyChanged("Item2");
    }
}

private int _item1;
public int Item1
{
    get
    {
        return _item1;
    }

    set
    {
        if (_item1 == value) return;

        _item1 = value;
        OnPropertyChanged("Item1");
    }
}

它编译正确,我可以毫无问题地执行应用程序,但在设计时,视图未显示,错误显示不受控制的异常并指向该行:

It compiles correctly and I can execute the application without problems but in design time, the view is not show, an error says Not controlled exception and points to the line:

int item1 = (int)values[0];

在 PrintIconVisibilityValueConverter 类中.

within PrintIconVisibilityValueConverter class.

在视图中显示的异常屏幕截图下方:

Below the screenshots of the exception shown on view:

推荐答案

一些建议;

调用转换器中的 GetIsInDesignMode 方法,如果返回 true 则立即返回:

Call the GetIsInDesignMode method in your converter and return immediately if it returns true:

public class PrintIconVisibilityValueConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            return Visibility.Visible;
        ...
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

在 XAML 中设置 DataContext:

Set the DataContext in XAML:

<Window.DataContext>
    <local:ViewModel />
</Window.DataContext>

设置设计时数据上下文:

Set the design time data context:

<Window ... d:DataContext ="{d:DesignInstance {x:Type local:ViewModel}, IsDesignTimeCreatable=True}">

禁用 XAML UI 设计器

这篇关于WPF XAML 中的不受控制的异常:指定的转换无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆