2016-10-06 11 views
0

MVVMプロジェクトでデザイナーに問題があります。IOException:設計時にリソースが見つかりません

私はカスタム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> 

リソース宣言:それは実行時に完璧に動作

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

、しかし:

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

マイConverterがあります私がVisual Studioのxamlの "デザイン"ウィンドウを使用しているとき私のWindowsの外観を見て、ちょうど持っているIOException : Cannot locate resource 'images/folder.png'

私の問題はどこから来ていますか? どうすれば修正できますか?

+1

おそらく最も簡単な修正ですコンバータに(http://stackoverflow.com/a/834332/1997232)[デザインモードを検出]このような場合には画像を解決しようとしないでください(リターン 'null'なのでへ)。 – Sinatr

+0

http://stackoverflow.com/a/11948876/3955716 – Rom

+0

@Sinatrもしうまくいけば、私は今、私のWindowsを見るが、(私がnullを返すように)私は自分のイメージを見ないので、私のレンダリングのアイデア。 イメージをデザインモード固有のコードで返す方法があれば、それを答えとして受け入れます。 – Belterius

答えて

0

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"); 
     } 
    } 
関連する問題