Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share a numeric constant between xaml and c# in silverlight

I'm new to .NET programming, and trying to learn Silverlight 2 / C#.

I need to declare numeric constants (or better yet, readonly variables), and access them in both XAML and my C# code-behind file.

These values are more appropriately defined in XAML, but if the definition needs to be done in C#, that's better than hard-coding the value in several places.

I found a solution for WPF XAML, but Silverlight apparently doesn't support the syntax (and the solution I found required that the definitions be in the C# file).

TIA!

Reply to Michael

I'm adding this reply as a comment to my original question because the comment editor doesn't allow me to add this much text and I couldn't find another way to respond to your answer.

Thanks for the answer, Michael, but I don’t’ seem to be able to get it to work.

I’m starting with an example from “Pro Silverlight 2 in c# 2008”. The example works fine, but requires me to hard-code the same two values in two places in my XAML, and one place in my C#.

I implemented your solution, and everything built fine, but when I tried to execute the code I got a page error in the browser. I removed all the changes, and verified that things once again ran fine.

I then added the xmlns:sys declaration and sys:Int32 declaration to my App.xaml file. Everything again ran fine, but when I tried to view the Page.xaml file in VS2008, I got a strange error:

undeclared prefix [Line: 6 Position: 30] – Page.xmal, Line 1, Column 1

The preview page was blank.

There was nothing interesting on Line 6, Col 30 of Page.xaml, but in the App.xaml file, that location contains the closing “>” immediately before the integer value 8:

  <sys:Int32 x:Key="QAPS">8</sys:Int32>

If I hit F5, however, the app runs fine in the browser.

I’m running VS2008 SP1 and the Mar 09 Silverlight toolkit.

I can’t understand why the QAPS declaration in the App.xaml application resources section could cause, especially given that I make no reference to it anywhere in the app. Do you have any idea what might be going on?

Another Reply to Michael

You’re correct, I hadn’t added the xmlns:sys declaration to my Page.xaml, but adding it made no difference – I’m still getting the ‘undeclared prefix [Line: 6 Position: 30]’ error in Page.xaml (and the app still runs). Since everything runs OK, I guess this must be an issue in VS2008.

The larger issue was that I was getting a run-time exception when I tried to access QAPS from C#, and I’ve figured out what I was doing wrong to cause it. The problem was that I was apparently accessing the resource before it existed.

I had declared/initialized a private variable to hold the resource value at the class level; when I moved the initialization to the event handler, the problem went away. So I’m guessing that C# class level variables are evaluated before the XAML resources are created.

(As an aside, the 600 character limit on comments is a real pain. As it is, I have to edit my original question to post a reply).

like image 414
casterle Avatar asked May 10 '09 16:05

casterle


1 Answers

Put them in your application or page resources:

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <Application.Resources>
        <sys:Int32 x:Key="QPS">8</sys:Int32>
    </Application.Resources>
</Application>

Then you can access them via code like this:

if (Application.Current.Resources.Contains("QPS"))
{
    int callsPerSecond = (int) Application.Current.Resources["QPS"];
}

and via Xaml like this

<TextBlock Text="{StaticResource QPS}"/>
like image 138
Michael S. Scherotter Avatar answered Oct 07 '22 01:10

Michael S. Scherotter