xamarin表单如何将枚举值传递给XAML并在switch语句中使用它?

时间:2016-10-12 10:26:16

标签: c# xaml xamarin enums xamarin.forms

我用一些值声明了枚举,我想传递给我的Formated参数。这是我的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;
            }
}

这是我的带有Formated参数的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 =&#34;&#34;参数。我应该将我的Formated字符串分配给枚举值吗?

1 个答案:

答案 0 :(得分:2)

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

public enum FomationType
        {
            NameValidation,
            CardNrValidation,
            ExpDate
        };

        public FomationType Formated { get; set; }

这里是我的开关声明:

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" >