简体   繁体   中英

xamarin forms How to pass enum value to XAML and use it in switch statement?

I declared enum with some values and I want to pass to my Formated parameter. Here is my C# code:

        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;
            }
}

And here is my XAML code with Formated parameter where I have to pass my enum value:

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

Now some how I should pass FormationType anum to Formated="" parameter. And should I assign my Formated string to enum values?

Well, to solve this issue regarding this forum post helped for my to figure out everything. Here is my C# code:

public enum FomationType
        {
            NameValidation,
            CardNrValidation,
            ExpDate
        };

        public FomationType Formated { get; set; }

and here my switch statment:

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 property doesn't change:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM