Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the {x:Static ...} extension for Phone7 Silverlight apps?

I'm writing a Phone 7 app and I would like to reference constant values in markup. I believe the way one is supposed to do this is via x:Static.

However, Visual Studio keeps claiming it has no knowledge of x:static. What is the secret sauce here? I have the following:

<phone:PhoneApplicationPage 
  ...
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  ...

  <Image Height="{x:Static App.ImageHeight}" ... />
  ...

And of course:

public partial class App : Application
{
  public const double ImageHeight = 100;
  ...

The error message is "The type 'x:Static' was not found. Verify that...".

like image 378
i_am_jorf Avatar asked Jan 16 '11 23:01

i_am_jorf


People also ask

How do I get Microsoft Silverlight to work on Chrome?

You must use a browser that supports Silverlight content to access a Silverlight page. To work around this issue on versions 42 to 44 of Chrome, follow these steps: On the address bar in Chrome, type chrome://flags/#enable-npapi . In the Enable NPAPI Mac, Windows box, click Enable.

How do I open Silverlight web app?

Using Silverlight outside the browser is as easy as bookmarking Web pages. Silverlight applications with out-of-browser support are simple to install, run, and uninstall. To install, just right-click the Web-based application and select install. The installer adds a shortcut to your desktop or Start menu.


1 Answers

x:Static is only available in WPF - neither Silverlight for the browser nor Silverlight for Windows Phone 7 support this markup extension.

The usual workaround is to create a (non-static) class that has properties which simply wrap the static properties you want, and create an instance of that as a Resource and bind against it that way.

Example*:

public class StaticSideEnums
{
    public static Side Bid { get { return Side.Bid; } }
    public static Side Ask { get { return Side.Ask; } }
}

In the resources (app.xaml):

<ResourceDictionary>
    <local:StaticSideEnums x:Key="StaticSideEnums"/>
</ResourceDictionary>

In the xaml where it's used:

<toolkit:ListPicker Name="picker" SelectionChanged="OnSelectionChanged">
    <toolkit:ListPickerItem Content="Buy"  Tag="{Binding Bid, Source={StaticResource StaticSideEnums}}" />
    <toolkit:ListPickerItem Content="Sell" Tag="{Binding Ask, Source={StaticResource StaticSideEnums}}" />
</toolkit:ListPicker>

*This example is taken from an answer in Using localized strings in a ListPicker populated from Enum

like image 189
Austin Lamb Avatar answered Oct 27 '22 00:10

Austin Lamb