c# - xamarin forms 如何将枚举值传递给 XAML 并在 switch 语句中使用它?

标签 c# xaml xamarin enums xamarin.forms

我用一些值声明了枚举,我想传递给我的格式化参数。这是我的 C# 代码:

        public MyControllerView()
        {
         ContentEntry.TextChanged += Entry_TextChanged;
        }         
                public string Formated
                {
                    get
                    {
                        return formatedText;
                    }
                    set
                    {
                        formatedText = value;
                    }
                }

        public enum FomationType
                {
                    NameValidation,
                    CardNrValidation,
                    ExpDate
                };
    // here I want to use my enum in switch statement, but I can't, because I can't modify my method parameters

private void Entry_TextChanged(object sender, TextChangedEventArgs e)
        {
            FomationType FomationType;
            switch (FomationType)
            {
                case FomationType.NameValidation:
                    ToUpper(ent);
                    break;
                case FomationType.CardNrValidation:
                    CardNumberValidation(ent);
                    break;
                case FomationType.ExpDate:
                    ExpDate(ent, e);
                    break;
            }
}

这是我的带有格式化参数的 XAML 代码,我必须在其中传递我的枚举值:

<ContentPage.Content>

    <StackLayout Padding="7,7,7,7" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Spacing="0">

      <cv:MyControllerView LabelText="some text 4" Placeholder="some text 4" Formated="" Keyboard="Text" >
      </cv:MyControllerView>

      <cv:MyControllerView LabelText="some text 3" Placeholder="some text 3" Formated="" Keyboard="Numeric"  >
      </cv:MyControllerView>

      <cv:MyControllerView LabelText="some text 2" Placeholder="some text 2" Formated="" Keyboard="Numeric" >
      </cv:MyControllerView>

    </StackLayout>
  </ContentPage.Content>

现在我应该如何将 FormationType anum 传递给 Formated=""参数。我应该将我的格式化字符串分配给枚举值吗?

最佳答案

好吧,解决这个关于this的问题论坛帖子帮助我弄清楚了一切。这是我的 C# 代码:

public enum FomationType
        {
            NameValidation,
            CardNrValidation,
            ExpDate
        };

        public FomationType Formated { get; set; }

这里是我的 switch 声明:

var ent = sender as Entry;
            switch (Formated)
            {
                case FomationType.NameValidation:
                    ToUpper(ent);
                    break;
                case FomationType.CardNrValidation:
                    CardNumberValidation(ent);
                    break;
                case FomationType.ExpDate:
                    ExpDate(ent, e);
                    break;
            }

XAML 属性不变:

<cv:MyControllerView LabelText="some text 4" Placeholder="some text 4" Formated="NameValidation" Keyboard="Text" >

关于c# - xamarin forms 如何将枚举值传递给 XAML 并在 switch 语句中使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39996280/

相关文章:

xamarin - 无法在 Xamarin.Mac 中的 NSButton 上触发鼠标输入

java - 使用跨平台语言开发android有哪些缺点?

c# - 为什么可选参数在 Visual Studio 2015 中传递错误值?

wpf - TextBox 上的 MinLines 和 MaxLines 不起作用

c# - Xamarin Forms iOS - 当前上下文中不存在运行时

xaml - 添加 Pivot 和其他 Microsoft.Phone 控件时,Visual Studio 2012 XAML UI 设计器引发异常

c# - 当 TextBox 失去焦点时在 ViewModel 中运行函数?

c# - C#中是否有一个内置的数据结构,只允许添加项,但不允许删除项?

c# - 字典 <string,string> 使用 Automapper 映射到一个对象

c# - 如何为 .NET Core View 类替换 Request.Url.GetLeftPart(UriPartial.Authority)?