プロジェクト1は画像を格納したリソースファイル(resx)を持ち、その画像を別のプロジェクト2のXAMLから参照しようとしています。プロジェクト2はユーザーコントロールライブラリ形式です。
開発環境はVisualStudio2013、.Net Frameworkは4.5です。
以下はプロジェクト2のxamlファイルで、プロジェクト1のリソースファイル内の画像imgを参照しようとしています。
lang
1<UserControl x:Class="WpfControlLibrary1.UserControl1" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 xmlns:local="clr-namespace:project1;assembly=project1" 7 xmlns:prop="clr-namespace:project1.Properties;assembly=project1" 8 mc:Ignorable="d" 9 d:DesignHeight="300" d:DesignWidth="300"> 10 <UserControl.Resources> 11 <ResourceDictionary> 12 <local:BitmapToImageSourceConverter x:Key="BitmapToImageSource"/> 13 </ResourceDictionary> 14 </UserControl.Resources> 15 <Grid> 16 <!--これはノーエラー--> 17 <Image Source="pack://application:,,,/project1;component/Resources/img.png" /> 18 <!--こちらはエラー「メンバー "img" が認識されないか、アクセスできません。」--> 19 <Image Source="{Binding Source={x:Static prop:Resources.img}, Converter={StaticResource BitmapToImageSource}}" /> 20 </Grid> 21</UserControl>
BitmapToImageSourceConverter
は、System.Drawing.Bitmap
をSystem.Windows.Media.ImageSource
へ変換するコンバータです。
lang
1 [ValueConversion(typeof(System.Drawing.Bitmap), typeof(ImageSource))] 2 public class BitmapToImageSourceConverter : IValueConverter 3 { 4 public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 5 { 6 var bmp = value as System.Drawing.Bitmap; 7 if (bmp == null) 8 return null; 9 return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( 10 bmp.GetHbitmap(), 11 IntPtr.Zero, 12 Int32Rect.Empty, 13 BitmapSizeOptions.FromEmptyOptions()); 14 } 15 16 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 17 { 18 throw new NotSupportedException(); 19 } 20 }
問題は、<Image Source="{Binding Source={x:Static prop:Resources.img}, Converter={StaticResource BitmapToImageSource}}" />
がエラーとなってしまうことです。
理由が分からなかったのですが、プロジェクト2をーザーコントロールライブラリ形式ではなくWPFアプリケーション形式にすると、エラーは発生しません。
このエラーを無くすためには、どうすれば良いのでしょうか?
以上、よろしくお願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2014/10/02 04:38