Advertisemen
Introduction:
Fortunately in xaml, we can create elements that come from the standard Silverlight and XAML namespaces. And also we can create objects that are instances of our own custom classes, or standard .NET classes in other namespaces. However in this article i am going to explain about 'How to create objects for our custom classes from xaml?".Requirements:
- This sample is targeted on windowsphone 7.1 OS
Description:
Step 1:
- Open Visual Studio and create new project name (Ex: "CsharpObjectvsXaml")
Step 2:
Open MainPage.xaml.cs file and create your own class as mentioned below.
- namespace CsharpObjectvsXaml
- {
- public partial class MainPage : PhoneApplicationPage
- {
- // Constructor
- public MainPage()
- {
- InitializeComponent();
- }
- }
- //Custom Class
- public class Blog
- {
- private string str_name;
- public string Name
- {
- get
- {
- return str_name;
- }
- set
- {
- str_name = value;
- }
- }
- }
- }
Step 3:
Now start by including a new namespace attribute, to indicate the namespace and assembly where your class can be found. In the following example, we set up the :MyCustomObject prefix as pointing to the namespace 'CsharpObjectvsXaml'. It can be xmlns:local or xmln:src or any other word followed by xmlns:. But we have to use the same identifier(MyCustomObject or local or src) when we refer (or when want to use) the namespace.
- xmlns:MyCustomObject="clr-namespace:CsharpObjectvsXaml"
To use the classes in the above namespace, we need to create an object of the class we are interested in as shown below. Ex, Blog class
- <Grid.Resources>
- <MyCustomObject:Blog x:Key="BlogClassObject" Name="SubramanyamRaju_WindowsPhone_Blog"/>
- </Grid.Resources>
Once the object of the 'Blog' class is instantiated, you can use it as a source during binding operation to bind to some UI element as shown below.
- <TextBlock Text="{Binding Source={StaticResource BlogClassObject}, Path=Name}" />
The complete code in MainPage.xaml page is like below:
- <phone:PhoneApplicationPage
- x:Class="CsharpObjectvsXaml.MainPage"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
- xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
- FontFamily="{StaticResource PhoneFontFamilyNormal}"
- FontSize="{StaticResource PhoneFontSizeNormal}"
- Foreground="{StaticResource PhoneForegroundBrush}"
- SupportedOrientations="Portrait" Orientation="Portrait"
- shell:SystemTray.IsVisible="True"
- xmlns:MyCustomObject="clr-namespace:CsharpObjectvsXaml">
- <!--LayoutRoot is the root grid where all page content is placed-->
- <Grid x:Name="LayoutRoot" Background="Transparent">
- <Grid.Resources>
- <MyCustomObject:Blog x:Key="BlogClassObject" Name="SubramanyamRaju_WindowsPhone_Blog"/>
- </Grid.Resources>
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="*"/>
- </Grid.RowDefinitions>
- <!--TitlePanel contains the name of the application and page title-->
- <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="0,17,0,28">
- <TextBlock x:Name="ApplicationTitle" FontSize="28" Text="Creating Custom Object in xaml :" Style="{StaticResource PhoneTextNormalStyle}"/>
- </StackPanel>
- <!--ContentPanel - place additional content here-->
- <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
- <StackPanel>
- <TextBlock Text="Blog Name:"/>
- <TextBlock Text="{Binding Source={StaticResource BlogClassObject}, Path=Name}" />
- </StackPanel>
- </Grid>
- </Grid>
- </phone:PhoneApplicationPage>
Step 4:
Press 'F5' to run the sample and screen will be like this.
I thought you don't need sample source code, because i already provided necessary code in this post.
FeedBack Note:
Please share your thoughts,what you think about this post,Is this post really helpful for you?I always welcome if you drop comments on this post and it would be impressive.
Follow me always at @Subramanyam_B
Have a nice day by Subramanyam Raju :)
Advertisemen
Tidak ada komentar:
Posting Komentar