UWP - Image Uri in Application Folder

前端 未结 5 1313
自闭症患者
自闭症患者 2020-12-10 10:51

I\'m having a little issue here in showing images.

So when I\'m trying to load images from XAML, I can use a relative uri to the image source like this :

<         


        
相关标签:
5条回答
  • 2020-12-10 11:31

    You can also use it with BaseUri.

    BitmapImage bitmapImage = new BitmapImage(new Uri(this.BaseUri, "/Assets/image.jpg"));
    

    Assets is a folder name and you can change it with your any custom folder name :)

    0 讨论(0)
  • 2020-12-10 11:32

    To access files stored inside the application package, but from code where there is no inferred root authority, you need to specify the ms-appx: scheme :

    So in your case it will be something like :

    BitmapImage bitmapImage = 
                         new BitmapImage(new Uri("ms-appx:///[project-name]/Assets/image.jpg"));
    

    Read this documentation for more details : https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh965322.aspx

    Hope it helps.

    0 讨论(0)
  • 2020-12-10 11:37
    Image img = new Image();
    img.Source = new BitmapImage(new Uri("http://www.contoso.com/images/logo.png"));
    
    0 讨论(0)
  • 2020-12-10 11:39

    I believe WPF is more forgiving and paths like "Assets/image.png" are possible. If you are writing a UWA (win 10) app then you need to use the "ms-appx" URI's.

    My experience is "ms-appx:///Assets/image.png" works fine.

    Yes, three "/"s (!) and no need for the name of the app in the path.

    0 讨论(0)
  • 2020-12-10 11:39

    Just use it like this

    <Image Source="Assets/image.jpg" />
    
    0 讨论(0)
提交回复
热议问题