如何获取多请求表单下所有动态生成DatePicker的选中日期值
多请求表单多日期区间取值实现方案
1 调整数据存储结构
放弃原有的全局单字典存储逻辑,改为嵌套结构,每个请求表单独立维护自身的日期选择器集合,避免不同表单的序号、控件冲突:
// 单个表单的日期选择器存储类 public class FormDateInfo { // 当前表单已添加的日期组序号,每个表单独立计数 public int CurrentDatePairIndex { get; set; } = 1; // 键为日期组序号,值为对应开始日期选择器 public Dictionary<int, DatePicker> StartDatePickers { get; set; } = new(); // 键为日期组序号,值为对应结束日期选择器 public Dictionary<int, DatePicker> EndDatePickers { get; set; } = new(); } // 全局存储所有请求表单的日期数据,键可以是表单的唯一ID、也可以直接用表单的控件实例作为标识 public Dictionary<object, FormDateInfo> AllRequestFormDates { get; set; } = new();
2 修改日期组添加逻辑
点击添加按钮时,首先获取当前按钮所属的请求表单标识,再将生成的DatePicker存入对应表单的存储集合中:
private void AddDateBtn_Clicked(object sender, EventArgs e) { // 获取当前按钮所属的请求表单标识,这里以按钮的Parent(即表单实例)为例,可根据你的实际布局调整取值逻辑 var currentForm = (sender as Button).Parent; // 如果当前表单还没有注册到全局存储,先初始化 if (!AllRequestFormDates.ContainsKey(currentForm)) { AllRequestFormDates.Add(currentForm, new FormDateInfo()); } var currentFormDateInfo = AllRequestFormDates[currentForm]; var currentIndex = currentFormDateInfo.CurrentDatePairIndex; Grid grid1 = new Grid { ColumnDefinitions = { new ColumnDefinition{ Width = new GridLength(0.4, GridUnitType.Star)}, new ColumnDefinition{ Width = new GridLength(0.6, GridUnitType.Star)} } }; Label label1 = new Label { Text = "Start Date", HorizontalOptions = LayoutOptions.StartAndExpand, VerticalOptions = LayoutOptions.CenterAndExpand, FontSize = Device.GetNamedSize(NamedSize.Caption, typeof(Label)), TextColor = Color.Black }; grid1.Children.Add(label1, 0, 0); DatePicker startdatePicker1 = new DatePicker { HorizontalOptions = LayoutOptions.StartAndExpand, VerticalOptions = LayoutOptions.CenterAndExpand, FontSize = Device.GetNamedSize(NamedSize.Caption, typeof(Label)), TextColor = Color.Black }; // 存入当前表单的开始日期集合 currentFormDateInfo.StartDatePickers.Add(currentIndex, startdatePicker1); grid1.Children.Add(startdatePicker1, 1, 0); BoxView boxView = new BoxView { HorizontalOptions = LayoutOptions.FillAndExpand, HeightRequest = 1, BackgroundColor = Color.LightGray }; // 这里的NewDurationStack也要替换为当前表单对应的日期容器,可从currentForm的子控件中获取 var currentFormDurationStack = currentForm.FindByName<StackLayout>("NewDurationStack"); currentFormDurationStack.Children.Add(grid1); currentFormDurationStack.Children.Add(boxView); Grid grid2 = new Grid { ColumnDefinitions = { new ColumnDefinition{ Width = new GridLength(0.4, GridUnitType.Star)}, new ColumnDefinition{ Width = new GridLength(0.6, GridUnitType.Star)} } }; Label label2 = new Label { Text = "End Date", HorizontalOptions = LayoutOptions.StartAndExpand, VerticalOptions = LayoutOptions.CenterAndExpand, FontSize = Device.GetNamedSize(NamedSize.Caption, typeof(Label)), TextColor = Color.Black }; grid2.Children.Add(label2, 0, 0); DatePicker enddatePicker1 = new DatePicker { HorizontalOptions = LayoutOptions.StartAndExpand, VerticalOptions = LayoutOptions.CenterAndExpand, FontSize = Device.GetNamedSize(NamedSize.Caption, typeof(Label)), TextColor = Color.Black }; grid2.Children.Add(enddatePicker1, 1, 0); // 存入当前表单的结束日期集合 currentFormDateInfo.EndDatePickers.Add(currentIndex, enddatePicker1); BoxView boxView1 = new BoxView { HorizontalOptions = LayoutOptions.FillAndExpand, HeightRequest = 1, BackgroundColor = Color.LightGray }; currentFormDurationStack.Children.Add(grid2); currentFormDurationStack.Children.Add(boxView1); // 当前表单的日期序号自增 currentFormDateInfo.CurrentDatePairIndex++; }
3 批量获取所有日期值
遍历全局存储集合即可拿到所有表单的所有选中日期:
// 定义返回结果结构,可根据你的业务需求调整 public class FormDateResult { public object FormId { get; set; } public List<(DateTime StartDate, DateTime EndDate)> DateRanges { get; set; } = new(); } public List<FormDateResult> GetAllFormSelectedDates() { var result = new List<FormDateResult>(); foreach (var formItem in AllRequestFormDates) { var formResult = new FormDateResult { FormId = formItem.Key }; var startPickers = formItem.Value.StartDatePickers; var endPickers = formItem.Value.EndDatePickers; // 按序号配对日期 foreach (var index in startPickers.Keys) { if (endPickers.TryGetValue(index, out var endPicker)) { formResult.DateRanges.Add((startPickers[index].Date, endPicker.Date)); } } result.Add(formResult); } return result; }
可选优化方案:如果不需要保留控件引用,也可以直接在DatePicker的DateSelected事件中同步更新对应存储的日期值,后续无需遍历控件直接读取预存的日期值即可,性能更好。
内容的提问来源于stack exchange,提问作者Abubakar Hanif
相关产品推荐
相关产品推荐

