jQuery and ASP.NET

January 29, 2010

How to Programmatically access Constants declared in your Xaml




If you have been looking out for a way to declare some constants in your xaml and then programmatically access them, then here’s a way I follow.

Open your App.xaml and add the following string constant to it

<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SampleSilverlight.App"
xmlns:clr="clr-namespace:System;assembly=mscorlib">
<
Application.Resources>
<
clr:String x:Key="DC">DevCurry</clr:String>
</
Application.Resources>
</
Application>



We will access this string constant programmatically using C# or VB.NET. 
Open your Page.xaml and add a TextBlock and a Button to it.
<StackPanel Height="200" Width="200">
<
TextBlock x:Name="tb"></TextBlock>
<
Button x:Name="btnFetch" Content="Fetch"
Click="btnFetch_Click"></Button>
</
StackPanel>
Now add the following code on button click:
C# 
private void btnFetch_Click(object sender, RoutedEventArgs e)
{
if (Application.Current.Resources.Contains("DC"))
{
tb.Text = (string)Application.Current.Resources["DC"];
}
}
VB.NET
Private Sub btnFetch_Click(ByVal sen As Object, ByVal e As RoutedEventArgs)
If Application.Current.Resources.Contains("DC") Then
tb.Text = CStr(Application.Current.Resources("DC"))
End If
End Sub

The Application.Resources gets a collection of Application-Scoped resources.

On clicking the button, the text ‘DevCurry’ gets displayed


image



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

0 Responses to "How to Programmatically access Constants declared in your Xaml"
 

Copyright © 2009-2011 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions