0

I have a set of Entry Views which is created Dynamically. I need to identify that on which entry view the text has been changed. I have the same scenario in buttons where I use CommandParameter to Identify each button.

Sample Code

        public class Questionaire : ContentPage
        {
        StackLayout contentview = new StackLayout {
        VerticalOptions = LayoutOptions.CenterAndExpand,
        HorizontalOptions = LayoutOptions.CenterAndExpand,
        };

        int Count  = <The Count varies>; //Number Of Views

        for(int i=0; i<Count; i++)
        {
            Entry  entryview = new Entry {
            WidthRequest = Metrics.Textentrywidth,
            Keyboard = Keyboard.Numeric,
            VerticalOptions=LayoutOptions.CenterAndExpand,
            HorizontalOptions = LayoutOptions.Center,
        };

        entryview.TextChanged += OnTextChanged;
        contentview.Children.Add (entryview); 
       }

       void OnTextChanged(object sender, TextChangedEventArgs e)
       {
            //Need to get which Entry view is changed
            Entry entry = sender as Entry;
            String val = entry.Text;
       }
       }
4

1 回答 1

0

使用Dictionary< int, Entry > GroupEntry在 StackLayout 中根据它的索引存储条目视图,我可以跟踪条目视图。

    public class Questionaire : ContentPage
    {
    public Dictionary<int,Entry> GroupEntry { get; set; }// To store Entry against Index
    StackLayout contentview = new StackLayout {
    VerticalOptions = LayoutOptions.CenterAndExpand,
    HorizontalOptions = LayoutOptions.CenterAndExpand,
    };

    int Count  = <The Count varies>; //Number Of Views

    for(int i=0; i<Count; i++)
    {
        Entry  entryview = new Entry {
        WidthRequest = Metrics.Textentrywidth,
        Keyboard = Keyboard.Numeric,
        VerticalOptions=LayoutOptions.CenterAndExpand,
        HorizontalOptions = LayoutOptions.Center,
    };

    entryview.TextChanged += OnTextChanged;
    contentview.Children.Add (entryview);
    GroupEntry.Add (i, entryview); //Add in Dictionary
   }

   void OnTextChanged(object sender, TextChangedEventArgs e)
   {
        Entry entry = sender as Entry;
        String val = entry.Text;

        int index = GroupEntry.FirstOrDefault(x => x.Value == entry)
            .Key; //Index of Entry
   }
   }
于 2014-08-12T07:57:00.983 回答