如何使用 Phone7 Silverlight 应用程序的 {x:Static ...} 扩展?

发布于 2024-10-12 07:21:18 字数 580 浏览 2 评论 0原文

我正在编写一个 Phone 7 应用程序,我想在标记中引用常量值。我相信应该这样做的方式是通过 x:Static

然而,Visual Studio 一直声称它不了解 x:static。这里的秘诀是什么?我有以下内容:

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

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

当然:

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

错误消息是“未找到类型‘x:Static’。请验证...”。

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...".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倥絔 2024-10-19 07:21:18

x:Static 仅在 WPF 中可用 - 用于浏览器的 Silverlight 和用于 Windows Phone 7 的 Silverlight 均不支持此标记扩展。

通常的解决方法是创建一个(非静态)类,该类的属性只需包装所需的静态属性,然后创建该实例作为资源并以这种方式绑定它。

示例*:

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

在资源 (app.xaml) 中:

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

在使用它的 xaml 中:

<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>

*此示例取自 在从 Enum 填充的 ListPicker 中使用本地化字符串

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文