相关疑难解决方法(0)

从Description属性获取枚举

可能重复:
通过其描述属性查找枚举值

我有一个通用的扩展方法,它Description从以下方式获取属性Enum:

enum Animal
{
    [Description("")]
    NotSet = 0,

    [Description("Giant Panda")]
    GiantPanda = 1,

    [Description("Lesser Spotted Anteater")]
    LesserSpottedAnteater = 2
}

public static string GetDescription(this Enum value)
{            
    FieldInfo field = value.GetType().GetField(value.ToString());

    DescriptionAttribute attribute
            = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))
                as DescriptionAttribute;

    return attribute == null ? value.ToString() : attribute.Description;
}
Run Code Online (Sandbox Code Playgroud)

所以我可以......

string myAnimal = Animal.GiantPanda.GetDescription(); // = "Giant Panda"
Run Code Online (Sandbox Code Playgroud)

现在,我正试图在另一个方向上找出等效函数,比如......

Animal a = (Animal)Enum.GetValueFromDescription("Giant Panda", typeof(Animal));
Run Code Online (Sandbox Code Playgroud)

.net c# enums attributes

206
推荐指数
4
解决办法
18万
查看次数

如何在Xamarin.Forms页面中从XAML传递Button作为CommandParameter?

我想将Xamarin.Forms.Button它自己Command作为CommandParameter我的ViewModel 传递给它.我知道如何从背后的代码实现这一点,例如......

XAML (由于简洁而遗漏了大多数属性)

<Button x:Name="myButton"
    Text="My Button"
    Command="{Binding ButtonClickCommand}"/>
Run Code Online (Sandbox Code Playgroud)

XAML.cs

public partial class MyTestPage
{
    public MyTestPage()
    {
        InitializeComponent();

        myButton.CommandParameter = myButton;
    }
}
Run Code Online (Sandbox Code Playgroud)

视图模型

public class MyViewModel : ViewModelBase
{
    public MyViewModel()
    {
        ButtonClickCommand = new Command(
            (parameter) =>
            {
                var view = parameter as Xamarin.Forms.Button;
                if (view != null)
                {
                    // Do Stuff
                }
            });
    }

    public ICommand ButtonClickCommand { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)

......但是可以CommandParameter在XAML中声明它吗?或者换句话说,将参数设置为按钮本身的绑定语法是什么?

<Button x:Name="myButton"
        Text="My …
Run Code Online (Sandbox Code Playgroud)

c# xaml commandparameter xamarin.forms

13
推荐指数
3
解决办法
2万
查看次数

标签 统计

c# ×2

.net ×1

attributes ×1

commandparameter ×1

enums ×1

xamarin.forms ×1

xaml ×1