在TextChanged事件上识别动态创建的条目视图中的条目视图

时间:2014-08-12 06:39:23

标签: xamarin.forms xamarin.forms.entry

我有一组动态创建的条目视图。我需要确定文本已更改的条目视图。我在按钮中有相同的场景,我使用CommandParameter来识别每个按钮。

示例代码

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

1 个答案:

答案 0 :(得分:0)

使用字典&lt; int,Entry&gt; GroupEntry 在StackLayout中存储Entry View的索引,我可以跟踪Entry View。

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