If you are new to WPF, you must know that XAML can define or declare any object under your local / global namespace just like your code does. It has full capability to load up your types based on the namespace you provide in your XAML. Lets look how you can load your own namespace in XAML.<Window x:Class="TestImagesApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="418" Width="530">
</Window>This is the basic XAML that appear from template when you just create a new project. Now if you see minutely, you can see xmlns is there which points to some url of presentation layer. This will ensure that all the references of WPF objects is itself available to your XAML and you do not need to refer its namespace. Now what if you need to use something other than this ? As I told you that XAML can load any external type. You need to add a namespace to the XAML, which will instruct the Loader to load the CLR assembly and expose the Type that you use in the XAML.
For simplicity lets add a string as resource in the Window.<Window x:Class="TestImagesApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="418" Width="530">
<Grid>
<Grid.Resources>
<System:String x:Key="strString">This is a string</System:String>
</Grid.Resources>
<TextBox Text="{StaticResource strString}" />
</Grid>
</Window>So here the xmlns:System will load the assembly mscorlib and expose the namespace System to your XAML. Clr-namespace :System specifies the namespace and assembly=mscorlib refer the assembly. You should remember that the assembly should exist on GAC or local bin folder to make sure the XAML can load it properly. Read more: Daily .Net Tips
QR:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="418" Width="530">
</Window>This is the basic XAML that appear from template when you just create a new project. Now if you see minutely, you can see xmlns is there which points to some url of presentation layer. This will ensure that all the references of WPF objects is itself available to your XAML and you do not need to refer its namespace. Now what if you need to use something other than this ? As I told you that XAML can load any external type. You need to add a namespace to the XAML, which will instruct the Loader to load the CLR assembly and expose the Type that you use in the XAML.
For simplicity lets add a string as resource in the Window.<Window x:Class="TestImagesApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="418" Width="530">
<Grid>
<Grid.Resources>
<System:String x:Key="strString">This is a string</System:String>
</Grid.Resources>
<TextBox Text="{StaticResource strString}" />
</Grid>
</Window>So here the xmlns:System will load the assembly mscorlib and expose the namespace System to your XAML. Clr-namespace :System specifies the namespace and assembly=mscorlib refer the assembly. You should remember that the assembly should exist on GAC or local bin folder to make sure the XAML can load it properly. Read more: Daily .Net Tips
QR:
0 comments:
Post a Comment