13

I couldn't get this to work in Silverlight, so I created two test projects. One simple WPF project and one simple Silverlight project that both do only one thing: set a public static readonly variable in code, and use it in a completely bare bones XAML. In WPF, works without a hitch. In Silverlight, I get the following compiler warning and runtime error:

Warning 2 The tag 'Static' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml'...

and

Invalid attribute value {x:Static SilverlightApplication3:Page.Test} for property Text. [Line: 7 Position: 25]

I'm assuming this is not supported in Silverlight 2, or am I just missing something really simple? Here's the full code for both just in case it's the latter:

public partial class Window1 : Window
{
    public static readonly string Test = "test";
    public Window1()
    {
        InitializeComponent();
    }
}

<Window x:Class="WpfApplication4.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300"
        xmlns:WpfApplication4="clr-namespace:WpfApplication4">    
    <Grid>
        <TextBlock Text="{x:Static WpfApplication4:Window1.Test}" />
    </Grid>
</Window>

and here's the SL version:

public partial class Page : UserControl
    {
        public static readonly string Test = "test";
        public Page()
        {
            InitializeComponent();
        }
    }

<UserControl x:Class="SilverlightApplication3.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:SilverlightApplication3="clr-namespace:SilverlightApplication3"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <TextBlock Text="{x:Static SilverlightApplication3:Page.Test}" />
    </Grid>
</UserControl>

5 Answers 5

6

Unfortunately Silverlight has many limits with respect to functionality and you just found one of them. StaticMarkupExpression is not supported by SL2. You also can't define it by yourself.

e.g. guy from ms: http://blogs.msdn.com/edmaia/archive/2008/11/23/animating-objects-visibility-in-silverlight.aspx

The trick may be to use an object like

class Helper{
    public string Value {get{return Page.Test;}} 

// implement INotifyPropertyChange if you want updates
}

And then

<Grid.Resources>
     <somexmlns:Helper x:Key="Helper"/>
</Grid.Resources>

<TextBlock Text="{Binding Value, Source={StaticResource Helper}}"/>
Sign up to request clarification or add additional context in comments.

3 Comments

That worked great as in your example with a simple string, but when I try to use a Color object or a string representation of a color where I am able to hard code one in the XAML (GradientStop Color="#FAF6...), I get the following error: AG_E_PARSER_BAD_PROPERTY_VALUE. What are the limitations?
Use the full syntax. A string is not a Color object, so you can't assign it to one. But if you use the full angle bracket syntax you can specify a color object and it will work fine. Or, use a generator like Blend and have a look at the way it marks up properties that contain Color objects in the abbreviated syntax.
public static string Value {get{return Page.Test;}} also works
5

Unfortunately, it looks like Silverlight doesn't support binding to static properties: What is the {x:Static sdfsdf} equivalent?

2 Comments

That sucks. Thanks for the link though. I didn't find anything definitive when I searched, but the guy who answered in the post you linked is an authority on SL
WhyTF doesn't it support them?!
3

You can actually bind to static properties as long as the class is not a static class. So using the previous example of the Helper class:

public class Helper
{
    public static string Value{ get {return Page.Test;} }
}

The XAML will stay the same.

2 Comments

Hi Sam, will it notify the binding if the value of Page.Test has been updated?
Hmmm, I cannot get it working. What the xaml syntax, can you add it here
1

I just noticed that you had a secondary question about binding to a color. I don't think it can be done in Silverlight. I'm pretty sure the minimum requirement for a binding target in Silverlight is FrameworkElement.

1 Comment

Yeah...and looking at it now months later, if I wanted to bind to what I think is a Color because I can declare it as a hex string, it's really a brush object when all is said and done. I don't even remember which project I was asking this question for, but if I need to do that now, I would imagine that this would work if I exposed a static Brush object instead of a color or string representation of a color.
-2

A static object will be instantiated only once and will persist until the end of the program.A static object can retain its state even when it is not in scope, but only visible within their local scope.

1 Comment

Just so you understand why your factually correct answer has been voted negative, we are not talking about the static keyword in C derivative languages, we are talking about the use in XAML of the StaticExtension class which supports markup references to class properties that are static in the sense your answer describes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.