IOException:只能在设计时找不到资源 [英] IOException : cannot locate resource at Design time only

查看:437
本文介绍了IOException:只能在设计时找不到资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having a problem with my designer on a MVVM project.

我有一个 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>

资源声明:

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

我的转换器是:

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:无法找到资源 images / folder.png

It works perfectly at run-time, but when I'm using the xaml "design" windows in Visual Studio instead of seeing the appearance of my Windows, I only have a IOException : Cannot locate resource 'images/folder.png'

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

Where is my problem coming from ? How can I fix it ?

推荐答案

我注意到这个问题从未得到解决,我遇到了与需要的完全相同的问题决议。解决此问题的方法如下:

I noticed this was never answered and I had the same exact issue that needed a resolution. The resolution to this problem is as follows:

更改:

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

收件人:

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

就是这样!另外,还要确保将图像上的 Build Action 设置为 Resource ,并且将 Copy to Output Directory 设置为请勿复制(由于这是资源,因此无需将映像复制到输出目录)。现在,您的控件将以设计模式显示。

That's it! Also make sure your that the Build Action is set to Resource on your images and Copy to Output Directory is set to Do not copy (since this is a resource there is no need to copy the image to the output directory). Your control will now show in design mode.

这篇关于IOException:只能在设计时找不到资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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