Application.GetResourceStream Method (Uri)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Returns a resource file from a location in the application package.
Assembly: System.Windows (in System.Windows.dll)
'Declaration Public Shared Function GetResourceStream ( _ uriResource As Uri _ ) As StreamResourceInfo
- Type: System.Uri
A relative URI that identifies the resource file to be loaded. If the file has a Build Action of Content, the URI is relative to the application package and you should not use a leading forward slash. If the file has a Build Action of Resource, you can use a leading forward slash, but it is not required.
The GetResourceStream method enables you to load resource files with Content or Resource set as the Build Action property value. The following table lists the file types you can load with GetResourceStream:
Notes and URI Example
Embedded in the application assembly in the application package
You can use a leading forward slash, but it is not required.
Included in the application package
You should not use a leading forward slash.
For performance reasons, if possible, you should set the Build Action for resources to Content, and load them accordingly.
The following code shows how to use GetResourceStream to load an image resource file from these two locations.
Imports Microsoft.Phone.Controls Imports System.IO Imports System.Windows Imports System.Windows.Controls Imports System.Windows.Media.Imaging Imports System.Windows.Resources Partial Public Class MainPage Inherits PhoneApplicationPage Public Sub New() InitializeComponent() ' Load image resource files included in the application package ' and resources that are embedded in assemblies included in the ' application package. ' Load an image resource file embedded in the application assembly. Dim img1 As Image = LoadImage("/PhoneApp1VB;component/EmbeddedInApplicationAssembly.png") Me.stackPanel.Children.Add(img1) ' Load an image resource file included the application package. Dim img2 As Image = LoadImage("IncludedInApplicationPackage.png") Me.stackPanel.Children.Add(img2) End Sub Public Function LoadImage(relativeUriString As String) As Image ' Get the image stream at the specified URI that ' is relative to the application package root. Dim uri As New Uri(relativeUriString, UriKind.Relative) Dim sri As StreamResourceInfo = Application.GetResourceStream(uri) ' Convert the stream to an Image object. Dim bi As New BitmapImage() bi.SetSource(sri.Stream) Dim img As New Image() img.Source = bi Return img End Function End Class