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

本文关键字:定位 资源 IOException | 更新日期: 2023-09-27 18:11:44

我在一个MVVM项目上遇到了一个问题。

我有一个自定义DataTemplateTreeView:

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

资源声明:

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

My Converter is:

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

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

我的问题是从哪里来的?

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

我注意到这个问题从来没有得到回答,我也有同样的问题需要解决。这个问题的解决办法如下:

改变:

pack://application:,,,/path/to/images/mypng.png

:

/Project Namespace;component/path/to/images/mypng.png

就是这样!还要确保在图像上将Build Action设置为Resource,并且将Copy to Output Directory设置为不要复制(因为这是一个资源,所以不需要将图像复制到输出目录)。您的控件现在将在设计模式下显示。

您可以检查它是否在DesignMode上运行,如下所示;

    public class HeaderToImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool designMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
            if (!designMode)
            {
                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");
        }
    }