IOException : 仅在设计时无法定位资源

Bel*_*ius 1 c# wpf designer mvvm ioexception

我的设计师在 MVVM 项目上遇到了问题。

我有TreeView一个自定义DataTemplate

                             <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <Image Name="img"  Width="20" Height="20" Stretch="Fill" 
                                       Source="{Binding 
                                       RelativeSource={RelativeSource 
                                       Mode=FindAncestor, 
                                       AncestorType={x:Type TreeViewItem}}, 
                                       Path=Header, 
                                       Converter={StaticResource HeaderToImageConverter}}"       
                                       />
                                    <TextBlock Text="{Binding}" Margin="5,0" />
                                </StackPanel>
                            </DataTemplate>
Run Code Online (Sandbox Code Playgroud)

资源声明:

<Window x:Class="BlobWorld.MainWindow"
        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:Core="clr-namespace:BlobWorld;assembly=" 
        xmlns:helper="clr-namespace:BlobWorld.Helper"
        mc:Ignorable="d"
        Title="MainWindow" Height="350.459" Width="746.561"
        DataContext="{DynamicResource MainWindowViewModel}">
    <Window.Resources>
        <helper:HeaderToImageConverter x:Key="HeaderToImageConverter"/>
    </Window.Resources>
Run Code Online (Sandbox Code Playgroud)

我的转换器是:

public class HeaderToImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((value as string).Contains(@"."))
            {
                Uri uri = new Uri("pack://application:,,,/images/File.png");
                BitmapImage source = new BitmapImage(uri);
                return source;
            }
            else
            {
                if (!(value as string).Contains(@":"))
                {
                    Uri uri = new Uri("pack://application:,,,/images/folder.png");
                    BitmapImage source = new BitmapImage(uri);
                    return source;
                }
                else
                {
                    Uri uri = new Uri("pack://application:,,,/images/diskdrive.png");
                    BitmapImage source = new BitmapImage(uri);
                    return source;
                }
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException("Cannot convert back");
        }
    }
Run Code Online (Sandbox Code Playgroud)

它在运行时完美运行,但是当我在 Visual Studio 中使用 xaml“设计”窗口而不是查看我的 Windows 的外观时,我只有一个 IOException : Cannot locate resource 'images/folder.png'

我的问题来自哪里?我该如何解决?

Mik*_*ike 5

我注意到这个问题从未得到解答,而且我遇到了同样需要解决的问题。此问题的解决方法如下:

改变:

pack://application:,,,/path/to/images/mypng.png
Run Code Online (Sandbox Code Playgroud)

到:

/Project Namespace;component/path/to/images/mypng.png
Run Code Online (Sandbox Code Playgroud)

就是这样!还要确保将图像上的构建操作设置为资源,并将复制到输出目录设置为不复制(因为这是一个资源,因此无需将图像复制到输出目录)。您的控件现在将以设计模式显示。