Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to share a numeric constant between xaml and c# in silverlight
    text
    copied!<p>I'm new to .NET programming, and trying to learn Silverlight 2 / C#.</p> <p>I need to declare numeric constants (or better yet, readonly variables), and access them in both XAML and my C# code-behind file. </p> <p>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.</p> <p>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).</p> <p>TIA!</p> <p><strong>Reply to Michael</strong></p> <p>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.</p> <p>Thanks for the answer, Michael, but I don’t’ seem to be able to get it to work.</p> <p>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#.</p> <p>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. </p> <p>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:</p> <p>undeclared prefix [Line: 6 Position: 30] – Page.xmal, Line 1, Column 1</p> <p>The preview page was blank.</p> <p>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:</p> <pre><code> &lt;sys:Int32 x:Key="QAPS"&gt;8&lt;/sys:Int32&gt; </code></pre> <p>If I hit F5, however, the app runs fine in the browser.</p> <p>I’m running VS2008 SP1 and the Mar 09 Silverlight toolkit. </p> <p>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? </p> <p><strong>Another Reply to Michael</strong></p> <p>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.</p> <p>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. </p> <p>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.</p> <p>(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).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload