public int count = 1;
Dictionary<DatePicker, int> StartDates = new Dictionary<DatePicker, int>();
Dictionary<DatePicker, int> EndDates = new Dictionary<DatePicker, int>()。
private void AddDateBtn_Clicked(object sender, EventArgs e)。
{
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 = "開始日期"。
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
};
StartDates.Add(startdatePicker1, count);
grid1.Children.Add(startdatePicker1, 1, 0)。
BoxView boxView = new BoxView
{
HorizontalOptions = LayoutOptions.FillAndExpand,
HeightRequest = 1,
背景顏色 = Color.LightGray
};
NewDurationStack.Children.Add(grid1)。
NewDurationStack.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)。
EndDates.Add(enddatePicker1, count);
BoxView boxView1 = new BoxView
{
HorizontalOptions = LayoutOptions.FillAndExpand,
HeightRequest = 1,
背景顏色 = Color.LightGray
};
NewDurationStack.Children.Add(grid2)。
NewDurationStack.Children.Add(boxView1);
}
我有這樣一個Button點擊事件,每次用戶按下該按鈕都會創建兩個DatePickers。用戶可以創建他們想要的數量。另外,這個頁面包括一個請求表,用戶可以有多個請求。所以在同一個頁面上,用戶可以創建多個請求表單,每個請求都可以有多個日期范圍,那么我怎樣才能在最后得到所有日期的值? 我試著把控制元件添加到Dictionry中,但這只適用于一個有多個日期范圍的請求表,而不適用于有多個日期范圍的多個請求。
uj5u.com熱心網友回復:
你使用的方法有點原始。你可以用listview輕松解決這個問題。檢查這個
首先你在ObservableCollection<DatTime>中添加新元素,當按鈕事件被觸發時。你在listview中只指定一次的DatePicker將自動復制自己。
uj5u.com熱心網友回復:
當你用DateSelected事件選擇DatePicker時,你可以得到日期,然后你可以使用KeyValue來存盤它們。
例如在你上面的代碼中:
Dictionary<int, DateTime> startDateTimes = new Dictionary<int, DateTime>()。
Dictionary<int, DateTime> endDateTimes = new Dictionary<int, DateTime> ()。
DatePicker startdatePicker1 = new DatePicker
{
HorizontalOptions = LayoutOptions.StartAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand,
FontSize = Device.GetNamedSize(NamedSize.Caption, typeof(Label))。
TextColor = Color.Black
};
startdatePicker1.DateSelected = StartdatePicker1_DateSelected;
StartDates.Add(startdatePicker1, count);
...
DatePicker enddatePicker1 = new DatePicker
{
HorizontalOptions = LayoutOptions.StartAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand,
FontSize = Device.GetNamedSize(NamedSize.Caption, typeof(Label))。
TextColor = Color.Black
};
enddatePicker1.DateSelected = EnddatePicker1_DateSelected;
EndDates.Add(enddatePicker1, count);
private void StartdatePicker1_DateSelected(object sender, DateChangedEventArgs e)。
{
DatePicker datePicker = sender as DatePicker;
startDateTimes.Add(1, datePicker.Date); /1對應的是你的計數。
}
private void EnddatePicker1_DateSelected(object sender, DateChangedEventArgs e)。
{
DatePicker datePicker = sender as DatePicker;
endDateTimes.Add(1, datePicker.Date); /1對應的是你的計數。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/333008.html
標籤:
上一篇:從串列中洗掉和移除一個指標
