【问题标题】:Identify a Entry View among Dynamically created Entry Views On TextChanged Event在 TextChanged 事件上动态创建的条目视图中识别一个条目视图
【发布时间】:2014-10-05 03:14:01
【问题描述】:

我有一组动态创建的条目视图。我需要确定文本已在哪个条目视图上更改。我在按钮中有相同的场景,我使用 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;
       }
       }

【问题讨论】:

    标签: xamarin.forms xamarin.forms.entry


    【解决方案1】:

    使用 Dictionary 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
       }
       }
    

    【讨论】:

      猜你喜欢
      • 2017-01-29
      • 2023-03-14
      • 2013-09-19
      • 2018-03-11
      • 2014-12-03
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 2019-05-05
      相关资源
      最近更新 更多