【问题标题】:x:Static in UWP XAMLx:UWP XAML 中的静态
【发布时间】:2015-11-04 23:39:24
【问题描述】:

我正在开发的应用程序要求 ConverterParameter 是一个枚举。为此,通常的做法是:

{Binding whatever, 
    Converter={StaticResource converterName}, 
    ConverterParameter={x:Static namespace:Enum.Value}}

但是,UWP 平台 x: 命名空间似乎没有静态扩展。

有谁知道是否有不依赖 x:Static 来比较绑定中的枚举的解决方案?

【问题讨论】:

  • 我没有做过任何 UWP,所以我不知道,但是有 this article 解释如何为 Silverlight 制作标记扩展(也没有 x:Static)。它可能适用于这里
  • 这里是answer,关于在 xaml 中使用枚举必须有效

标签: c# enums windows-10 uwp


【解决方案1】:

这在 UWP 中适用于我:

<Button Command="{Binding CheckWeatherCommand}">
  <Button.CommandParameter>
     <local:WeatherEnum>Cold</local:WeatherEnum>
  <Button.CommandParameter>
</Button>

【讨论】:

  • 哇,这需要更多的支持。效果很好,类不需要任何变通方法。
  • 有没有办法通过CommandParameter=...在线指定这个,以避免按钮正文中的xaml?
  • @Felix 似乎可以很好地将类型声明为资源。 Cold 然后在任何地方引用 CommandParameter={StaticResource Cold}。不像 x:Static 和 inline 那样干净,但它可以节省每次添加 XAML 包装器。
  • 我无法将 .... 转换为类型的对象 .... XAML 中的错误工具提示。在运行时该值为 null。
【解决方案2】:

我所知道的最简洁的方式...

public enum WeatherEnum
{
    Cold,
    Hot
}

在 XAML 中定义枚举值:

<local:WeatherEnum x:Key="WeatherEnumValueCold">Cold</local:WeatherEnum>

然后简单地使用它:

"{Binding whatever, Converter={StaticResource converterName},
 ConverterParameter={StaticResource WeatherEnumValueCold}}"

【讨论】:

  • 这对我不起作用...我按照您的建议进行了尝试,但在运行时出现此错误:“Phase”的 TypeConverter 不支持从字符串转换。 (Phase 是我的枚举器)
  • 这对我也不起作用。它会引发转换错误
【解决方案3】:

UWP(以及 WinRT 平台)上没有静态标记扩展。

其中一种可能的解决方案是使用枚举值作为属性创建类,并将此类的实例存储在 ResourceDictionary 中。

例子:

public enum Weather
{
    Cold,
    Hot
}

这是我们的枚举值类:

public class WeatherEnumValues
{
    public static Weather Cold
    {
        get
        {
            return Weather.Cold;
        }
    }

    public static Weather Hot
    {
        get
        {
            return Weather.Hot;
        }
    }
}

在您的资源字典中:

<local:WeatherEnumValues x:Key="WeatherEnumValues" />

我们在这里:

"{Binding whatever, Converter={StaticResource converterName},
 ConverterParameter={Binding Hot, Source={StaticResource WeatherEnumValues}}}" />

【讨论】:

    【解决方案4】:

    这是一个利用资源且没有转换器的答案:

    查看

    <Page
    .....
    xmlns:local="using:EnumNamespace"
    .....
    >
      <Grid>
        <Grid.Resources>
            <local:EnumType x:Key="EnumNamedConstantKey">EnumNamedConstant</local:SettingsCats>
        </Grid.Resources>
    
        <Button Content="DoSomething" Command="{Binding DoSomethingCommand}" CommandParameter="{StaticResource EnumNamedConstantKey}" />
      </Grid>
    </Page>
    

    视图模型

        public RelayCommand<EnumType> DoSomethingCommand { get; }
    
        public SomeViewModel()
        {
            DoSomethingCommand = new RelayCommand<EnumType>(DoSomethingCommandAction);
        }
    
        private void DoSomethingCommandAction(EnumType _enumNameConstant)
        {
         // Logic
         .........................
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-07
      • 2013-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      相关资源
      最近更新 更多