0

Let me start out by saying I have tried this and looked at this (which just leads to the first one). Those are great solutions for WPF. As we all are getting to know, the Windows Phone 7 is missing some of the nice edge cases.

My project seems to be destined to be devoid of dynamic images. I have an tag that I am trying to bind to a resource file and it is not working. I have tried both of the following, and neither seem to work.

First, the ever popular converter. Takes the URI string and returns a bitmap image. Apparently it works in WPF, but I can't make it work.

   public class BitmapImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
                              object parameter, CultureInfo culture)
        {
            try
            {
                return new BitmapImage(new Uri((string)value));
            }
            catch
            {
                return new BitmapImage();
            }
        }

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

Then there is the BitmapImage property. Didn't really expect this one to work and it didn't disappoint.

    public BitmapImage ImgSource 
    { 
        get
        {
            // ImgURI is a URI to the image.
            BitmapImage newImg = new BitmapImage(new Uri(ImgURI));
            // Tried ALL the creation options. :(
            //newImg.CreateOptions = BitmapCreateOptions.None;

            return newImg;
        }
    }

This is the XAML I am using for the converter (which is defined elsewhere) is....

        <Image Name="imgMap"
                VerticalAlignment="Center"
                HorizontalAlignment="Center"
                Source="{Binding ImgURI, Mode=OneWay, Converter={StaticResource bitmapConverter}}" 
                    Stretch="None"/>

I have debugged it to the point that I know that each binding is returning what it is supposed to. The converter is called and does what I think it should. ImgURI always returns a string of the format "/MyNamespace;component/Images/MyImg.jpg" and ImgSource was always a bitmapImage. All images have a BuildAction of "Resource".

I hope I am missing something obvious. Thanks for looking at my issue and if further information is needed, please leave a comment.

Community
  • 1
  • 1
Craig
  • 11,614
  • 13
  • 44
  • 62
  • The resources are in the same assembly as the consuming `Image`? This fails on both emulator and device? Does the converter throw an exception, or something else? – Kent Boogaart Jun 13 '11 at 09:36
  • Yes. Yes. The converter works fine, no errors, exceptions, or evasions. Weird, huh. – Craig Jun 13 '11 at 18:14

2 Answers2

3

You should be using the Build Action of Content not Resource.

Matt Lacey
  • 65,560
  • 11
  • 91
  • 143
  • I could have sworn up, down left and right that putting images in as Content was bad, m'kay. I'll look into it. I'd gladly take on some (not much) performance hit to at least work. – Craig Jun 13 '11 at 18:16
  • I have used the `/project;component/path.jpg` syntax with `Resource` many times. In fact, XAML editor warning even tells you that not having it set to `Resource` might be the problem. – Richard Szalay Jun 13 '11 at 20:09
  • Once I set the path to Relative, it worked either way. Kind of makes you want to know why... :) – Craig Jun 14 '11 at 04:10
1

I doubt this is your problem, but make sure you're passing UriKind.Relative to the Uri constructor.

Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
  • The problem turned out to be that I had a complete mental collapse. The statement "(new Uri((string)value))" is great, but to actually work, it needs to be explicitly set to relative. Sigh. Thanks for making me check! – Craig Jun 14 '11 at 04:07