Question Loading XAML at runtime

Jayme65

Active member
Joined
Apr 5, 2011
Messages
35
Programming Experience
Beginner
Hi,

I'm working on loading xaml code at runtime.
Until now, I can do it with XAML files which are set as "Resource"...but I would like to load it from a user's folder (let's say the "Desktop Folder" for example)
How should I please proceed?

The XAML example file, loaded as resource file in my project (but I would like to use it "independently")
VB.NET:
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Border Width="385" Height="385" Margin="0,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" CornerRadius="8" Background="#FF0085C8">
</Grid>

The Application XAML code
VB.NET:
<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
</Window>

The Application VB.Net code
VB.NET:
Class MainWindow
    Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Dim GridUri As New Uri("Resources\theme.xaml", UriKind.Relative)
        Dim sri As Windows.Resources.StreamResourceInfo = Application.GetResourceStream(GridUri)
        Dim xrdr As New System.Windows.Markup.XamlReader()
        Dim grd As Grid = CType(xrdr.LoadAsync(sri.Stream), Grid)
        Me.Content = grd
    End Sub
End Class

Thank you!
 
Back
Top