开发者

StaticResource Markup Extension vs. System.Windows.Application.FindResource

开发者 https://www.devze.com 2023-01-09 07:07 出处:网络
I\'ve defined a BitmapImage in my ResourceDictionary like that: <BitmapImage x:Key=\"Skin_Image_Back\" UriSource=\"./images/back.png\" />

I've defined a BitmapImage in my ResourceDictionary like that:

<BitmapImage x:Key="Skin_Image_Back" UriSource="./images/back.png" />

and loaded the ResourceDictionary like that

var dict = Application.LoadComponent(
   new Uri("TestProject.DefaultStyle;component/Style.xaml",
   UriKind.Relative)) as ResourceDictionary;
Application.Current.Resources.MergedDictionaries.Add(dict);

when i assign the image via xaml and the StaticResource Markup Extension like

<Image Source="{StaticResource Skin_Image_Back}" />

everything works fine.

but when i want to set the image from source via:

MyObject.ImageUrl = FindResource("Skin_Image_Back").ToString();

FindResource returns an URI, which ToString results in

"pack://application:,,,/TestProject.DefaultStyle;component/images/back.png"

which gets bound through a Converter like

<Image Source="{Binding ImageURL, Converter={StaticResource StringToImageThumbSourceConverter}}" />

that is implemented as

<Converter:StringToImageThumbSource x:Key="StringToImageThumbSourceConverter" />

and

  public class StringToImageThumbSource : IValueConverter {
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        try {
           if(value != null) {
              string s = value.ToString();
              if(!string.IsNullOrEmpty(s) && File.Exists(s)) {
                 BitmapImage bi = new BitmapImage();

                 bi.CreateOptions = BitmapCreateOptions.DelayCreation;
                 bi.BeginInit();
                 if(parameter is int) {
                    bi.DecodePixelWidth = (int)parameter;
                    bi.DecodePixelHeight = (int)parameter;
                 }
                 bi.UriSou开发者_开发百科rce = new Uri(value.ToString());
                 bi.EndInit();
                 return bi;
              }
           }
        } catch {
        }
        return null;
     }

     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        throw new NotImplementedException();
     }
  }

then it doesn't work... but WHY?


damn am i stupid...

if(!string.IsNullOrEmpty(s) && File.Exists(s))

File.Exists cannot check pack://,,,-URL's... so it never gets evaluated...

think i have to close the question... or delete it... don't know what to do...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号