0

我的 wpf 应用程序中有一个值转换器,它导致构建随机崩溃。有时应用程序正在工作,而完全改变其他东西并重建项目可能会使整个应用程序停止工作,抛出

方法或操作未实现

如果我删除值转换器,应用程序每次都能完美运行。为了让它完全工作,我有时必须多次清理和重建解决方案(或者从 XAML 中删除转换器并构建,然后将其重新添加并再次构建有时也可以)。

最困扰我的是它有时会工作,而且在重建后它会无缘无故停止工作。

这是我的 XAML

<Window x:Class="Common.Cleanup.Views.CleanupView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:converters="clr-namespace:Common.Cleanup.Converters"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:cal="http://www.caliburnproject.org"
        mc:Ignorable="d">
    <Window.Resources>
        <converters:IconTypeConverter x:Key="IconConverter" />
    </Window.Resources>
        ...
        <TreeView x:Name="CleanItems" Grid.Row="1" Grid.Column="1" ItemsSource="{Binding CleanupItemList}" >

            <TreeView.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}">
                    <Setter Property="Focusable" Value="false" />
                </Style>
            </TreeView.ItemContainerStyle>

            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Items}">
                    <StackPanel Orientation="Horizontal">
                        <Image Margin="0,0,3,0" MinWidth="16" MinHeight="16" Source="{Binding Path=IconType, Converter={StaticResource IconConverter}}" />
                        <CheckBox IsChecked="{Binding IsChecked}" />
                        <TextBlock Text="{Binding Name}" Margin="3,0,0,0" />
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
        ...
</Window>

和我的转换器:

public class IconTypeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var iconType = (IconType)value;

        switch (iconType)
        {
            case IconType.View:
                return FileUtility.GetBitmapImage("Shared/Icons/views.ico");
            case IconType.Schedule:
                return FileUtility.GetBitmapImage("Shared/Icons/schedules.ico");
            case IconType.Sheet:
                return FileUtility.GetBitmapImage("Shared/Icons/sheets.ico");
            case IconType.Link:
                return FileUtility.GetBitmapImage("Shared/Icons/links.ico");
        }

        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // This is not the issue. I've tried to return null without any differene
        throw new NotImplementedException();
    }
}

我正在使用 x64 平台的调试配置构建项目。

4

1 回答 1

0

添加Mode=OneWay您的图像绑定,事情应该会变得更好。

像这样:

<Image Margin="0,0,3,0" MinWidth="16" MinHeight="16" 
       Source="{Binding Path=IconType, Converter={StaticResource IconConverter}, Mode=OneWay}" />

这是因为在ConvertBack您的转换器的功能中,您已经要求它抛出异常。因此,每当它尝试使用 UI 元素的更改值更新源时,ConvertBack都会调用该函数并抛出您看到的异常。

编辑

我刚刚在ConvertBack函数中看到了您的评论。我还是建议你试试我的回答。还尝试返回NotSupportedException而不是返回NotImplementedException或 null 并查看错误消息是否更改。

于 2019-11-07T15:15:43.060 回答