The given System.Uri cannot be converted into a Windows.Foundation.Uri

后端 未结 7 1909
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 10:47

I\'m trying to programmatically load a BitmapImage in a XAML Metro app. Here\'s my code:

var uri = new Uri(\"/Images/800x600/BackgroundTile.bmp\", UriKind.Re         


        
相关标签:
7条回答
  • 2020-12-01 11:03

    This would work in XAML but would not work in code... so each control or page has a BaseUri property which you can use to build the proper uri for assets... here is an example:

        imageIcon.Source = new BitmapImage(new Uri(this.BaseUri, "Assets/file.gif"));
    

    // Or use the base uri from the imageIcon, same thing

        imageIcon.Source = new BitmapImage(new Uri(imageIcon.BaseUri, "Assets/file.gif"));
    

    also you would need to set the build action to "Content" rather than Embedded Resource... otherwise you need to use the ms-resource:// protocol.

    0 讨论(0)
  • 2020-12-01 11:04

    In case you're still having issues or are being asked to find an app to open the link, are you trying to use a WebView? If so, try using ms-appx-web instead of ms-appx.

    An example:

    this.webBrowser.Navigate(new Uri("ms-appx-web:///level/level/level/index.html"));
    

    Also note the lack of the URIKind parameter--evidently not needed at all in these instances.

    (I believe you may need to vary the leading forward slashes depending on your reference)

    0 讨论(0)
  • 2020-12-01 11:06

    In the Consumer Preview, the correct URL format has apparently changed to ms-appx:/Images/800x600/BackgroundTile.bmp

    0 讨论(0)
  • 2020-12-01 11:06

    Judging from the documentation for Windows.Foundation.Uri, it looks like WinRT doesn't support relative URIs. I tried a pack:// URI, but that gave me a UriFormatException, so apparently that's not the way to do it in WinRT either.

    I found the answer on this thread: MS invented yet another URI format for WinRT resources. This works:

    new Uri("ms-resource://MyAssembly/Images/800x600/BackgroundTile.bmp")
    

    Note that you don't add your actual assembly name -- the MyAssembly part is literal text.

    0 讨论(0)
  • 2020-12-01 11:19

    You will need to use the page's BaseUri property or the image control's BaseUri property like this:

    //using the page's BaseUri property
    BitmapImage bitmapImage = new BitmapImage(this.BaseUri,"/Images/800x600/BackgroundTile.bmp");
    image.Source = bitmapImage;
    

    or

    //using the image control's BaseUri property
    image.Source = new BitmapImage(image.BaseUri,"/Images/800x600/BackgroundTile.bmp");
    

    you can see the reason and solution here

    0 讨论(0)
  • 2020-12-01 11:19

    MVVM ;)

        public static ImageSource SetImageSource(string uriPath)//.com/image or some path
        {
            // this method very good for mvvm ;)
            try
            {   
                //In Model - public ImageSource AccountPhoto { get; set; } 
                //AccountPhoto = SetImageSource("some path");
                return return new BitmapImage()
                {
                    CreateOptions = BitmapCreateOptions.IgnoreImageCache,
                    UriSource = new Uri(uriPath)
                };
            }
            catch
            {
                Debug.WriteLine("Bad uri, set default image.");
                return return new BitmapImage()
                {
                    CreateOptions = BitmapCreateOptions.IgnoreImageCache,
                    UriSource = new Uri("ms-appx:///Avatar/Account Icon.png")
                };
            }
        }
    
    0 讨论(0)
提交回复
热议问题