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>
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}}"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With