开发者

Setting an Image to a local resource on WP7

开发者 https://www.devze.com 2023-03-12 18:03 出处:网络
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

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 someth开发者_运维问答ing obvious. Thanks for looking at my issue and if further information is needed, please leave a comment.


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


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

0

精彩评论

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