x:UWP XAML中的静态

fon*_*232 21 c# enums windows-10 uwp

我正在处理的应用程序需要ConverterParameter作为枚举.为此,常规方法是:

{Binding whatever, 
    Converter={StaticResource converterName}, 
    ConverterParameter={x:Static namespace:Enum.Value}}
Run Code Online (Sandbox Code Playgroud)

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

有没有人知道是否有一个不依赖于x的解决方案:静态用于比较绑定中的枚举?

小智 38

这在UWP中对我有用:

<Button Command="{Binding CheckWeatherCommand}">
  <Button.CommandParameter>
     <local:WeatherEnum>Cold</local:WeatherEnum>
  <Button.CommandParameter>
</Button>
Run Code Online (Sandbox Code Playgroud)

  • @Felix似乎可以很好地将类型声明为资源.<local:WeatherEnum x:Key ="Cold"> Cold </ local:WeatherEnum>然后在任何地方引用CommandParameter = {StaticResource Cold}.不像x那样干净:静态和内联,但每次都可以节省添加XAML包装. (4认同)
  • 哇,这需要更多的投资.这完全有效,没有类需要的解决方法,什么不是. (2认同)
  • 有没有办法通过`CommandParameter = ...`来指定这个,以避免按钮体中的xaml? (2认同)

Rav*_*Dev 7

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

其中一个可能的解决方案是创建具有枚举值作为属性的类,并在ResourceDictionary中存储此类的实例.

例:

public enum Weather
{
    Cold,
    Hot
}
Run Code Online (Sandbox Code Playgroud)

这是我们的枚举值类:

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

    public static Weather Hot
    {
        get
        {
            return Weather.Hot;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在您的ResourceDictionary中:

<local:WeatherEnumValues x:Key="WeatherEnumValues" />
Run Code Online (Sandbox Code Playgroud)

我们在这里:

"{Binding whatever, Converter={StaticResource converterName},
 ConverterParameter={Binding Hot, Source={StaticResource WeatherEnumValues}}}" />
Run Code Online (Sandbox Code Playgroud)


Ste*_*hlm 7

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

public enum WeatherEnum
{
    Cold,
    Hot
}
Run Code Online (Sandbox Code Playgroud)

在XAML中定义枚举值:

<local:WeatherEnum x:Key="WeatherEnumValueCold">Cold</local:WeatherEnum>
Run Code Online (Sandbox Code Playgroud)

并简单地使用它:

"{Binding whatever, Converter={StaticResource converterName},
 ConverterParameter={StaticResource WeatherEnumValueCold}}"
Run Code Online (Sandbox Code Playgroud)