Mega Code Archive

 
Categories / VB.Net / WPF
 

Load Xaml Resource

<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">     <Button Name="MyButton" HorizontalAlignment="Center" Margin="24">Button</Button>     <Ellipse Width="200" Height="100" Margin="24" Stroke="Red" StrokeThickness="10" />     <ListBox Width="100" Height="100" Margin="24">         <ListBoxItem>Sunday</ListBoxItem>         <ListBoxItem>Monday</ListBoxItem>         <ListBoxItem>Tuesday</ListBoxItem>     </ListBox> </StackPanel> //File:Window.xaml.vb Imports System Imports System.IO Imports System.Windows Imports System.Windows.Controls Imports System.Windows.Markup Namespace WpfApplication1.LoadXamlResource   Public Class LoadXamlResource     Inherits Window     Public Sub New()       Title = "Load Xaml Resource"       Dim uri As New Uri("pack://application:,,,/LoadXamlResource.xml")       Dim stream As Stream = Application.GetResourceStream(uri).Stream       Dim el As FrameworkElement = TryCast(XamlReader.Load(stream), FrameworkElement)       Content = el       Dim btn As Button = TryCast(el.FindName("MyButton"), Button)       If btn IsNot Nothing Then         AddHandler btn.Click, AddressOf ButtonOnClick       End If     End Sub     Private Sub ButtonOnClick(sender As Object, args As RoutedEventArgs)       Console.WriteLine(args.Source.ToString())     End Sub   End Class End Namespace