x:Static in UWP XAML

匿名 (未验证) 提交于 2019-12-03 01:56:01

问题:

An app I'm working on requires a ConverterParameter to be an enum. For this, the regular way to do would be:

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

However, the UWP platform x: namespace does not seem to have the Static extension.

Does anyone know if there's a solution that does not rely on x:Static for comparing an enum in binding?

回答1:

This works for me in a UWP:

 


回答2:

There is no Static Markup Extension on UWP (and WinRT platform too).

One of the possible solutions is to create class with enum values as properties and store instance of this class in the ResourceDictionary.

Example:

public enum Weather {     Cold,     Hot }

Here is our class with enum values:

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

In your ResourceDictionary:

And here we are:

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


回答3:

The most concise way I know of...

public enum WeatherEnum {     Cold,     Hot }

Define the enum value in XAML:

Cold

And simply use it:

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


回答4:

This is an answer utilizing resources and without Converters:

View:

EnumNamedConstant        

ViewModel

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


转载请标明出处:x:Static in UWP XAML
文章来源: x:Static in UWP XAML
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!