問題:我正在構建一個應用程式來對假期天數進行分類。我目前正在處理的頁面從用戶那里獲取 2 個日期(開始和結束日期),然后輸出所選的天數,以在應用程式的其他地方使用。
解決方案:最初,我想禁用星期六和星期日的選擇,但在此處浮動的一種解決方案不適用于我的解決方案。我的第二個想法是取用戶選擇的日期范圍并構建這些天的陣列,我可以計算星期六和星期日的數量并將它們從全天總數中洗掉。
這是計算器頁面的類背后的代碼,我在其中創建了“removeWeekends”方法。我只需要一些幫助來弄清楚如何在星期六和星期日掃描這個陣列,然后在頁面上實作它。任何幫助或建議表示贊賞。如果我可以提供更多代碼或詳細說明我的問題,請告訴我。
public Calculator()
{
InitializeComponent();
OnDateSelected(null, null);
dateFrom.SetValue(DatePicker.MinimumDateProperty, DateTime.Now);
dateTo.SetValue(DatePicker.MinimumDateProperty, DateTime.Now);
}
public DateTime[] removeWeekends(DateTime a, DateTime b)
{
List<DateTime> allDates = new List<DateTime>();
for (DateTime date = a; date <= b; date.AddDays(1))
allDates.Add(date);
return allDates.ToArray();
}
void OnDateSelected(object sender, DateChangedEventArgs e)
{
int days = (dateTo.Date - dateFrom.Date).Days;
if(dateTo.Date < dateFrom.Date)
{
leaveDaysLabel.Text = "Days: Invalid Selection";
}
else if (dateTo.Date == dateFrom.Date)
{
leaveDaysLabel.Text = "Days: 1";
}
else
{
leaveDaysLabel.Text = String.Format("Days: {0}", days);
leaveHoursLabel.Text = "Hours: " (days * 7.4).ToString();
}
}
結果:已經實作了 Jason 的回答,所以代碼現在像這樣:
public partial class Calculator : ContentPage
{
public Calculator()
{
InitializeComponent();
OnDateSelected(null, null);
dateFrom.SetValue(DatePicker.MinimumDateProperty, DateTime.Now);
dateTo.SetValue(DatePicker.MinimumDateProperty, DateTime.Now);
}
public int removeWeekends(DateTime a, DateTime b)
{
List<DateTime> allDates = new List<DateTime>();
for (DateTime date = a; date <= b; date.AddDays(1))
{
if (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday)
{
allDates.Add(date);
}
}
int weekendCount = allDates.Count();
return weekendCount;
}
void OnDateSelected(object sender, DateChangedEventArgs e)
{
int count = removeWeekends(dateFrom.Date, dateTo.Date);
int days = (dateTo.Date - dateFrom.Date).Days - count;
if(dateTo.Date < dateFrom.Date)
{
leaveDaysLabel.Text = "Days: Invalid Selection";
}
else if (dateTo.Date == dateFrom.Date)
{
leaveDaysLabel.Text = "Days: 1";
}
else
{
leaveDaysLabel.Text = String.Format("Days: {0}", days);
leaveHoursLabel.Text = "Hours: " (days * 7.4).ToString();
}
}
當我進入此頁面的選項卡時,現在遇到了 LOS 溢位問題。有人對此有經驗嗎?除錯日志如下。
10-05 15:14:52.125 I/ViewRootImpl(21404): ViewRoot's Touch Event : ACTION_DOWN
10-05 15:14:52.218 I/ViewRootImpl(21404): ViewRoot's Touch Event : ACTION_UP
10-05 15:14:52.237 I/AudioManagerEx(21404): AudioManagerEx created
10-05 15:14:52.366 I/zygote64(21404): Do full code cache collection, code=107KB, data=89KB
10-05 15:14:52.366 I/zygote64(21404): After code cache collection, code=105KB, data=70KB
10-05 15:14:52.616 I/zygote64(21404): Explicit concurrent copying GC freed 21890(9MB) AllocSpace objects, 1(20KB) LOS objects, 90% free, 1235KB/13MB, paused 274us total 8.763ms
10-05 15:14:52.617 D/Mono (21404): GC_TAR_BRIDGE bridges 139 objects 140 opaque 1 colors 139 colors-bridged 139 colors-visible 139 xref 3 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.02ms tarjan 0.06ms scc-setup 0.02ms gather-xref 0.00ms xref-setup 0.01ms cleanup 0.04ms
10-05 15:14:52.617 D/Mono (21404): GC_BRIDGE: Complete, was running for 10.06ms
10-05 15:14:52.617 D/Mono (21404): GC_MINOR: (Concurrent start) time 6.23ms, stw 10.52ms promoted 940K major size: 1984K in use: 1240K los size: 17424K in use: 17168K
10-05 15:14:52.617 D/Mono (21404): GC_MAJOR_CONCURRENT_START: (LOS overflow)
10-05 15:14:52.846 I/zygote64(21404): Explicit concurrent copying GC freed 649(38KB) AllocSpace objects, 0(0B) LOS objects, 91% free, 1213KB/13MB, paused 132us total 6.842ms
10-05 15:14:52.847 D/Mono (21404): GC_TAR_BRIDGE bridges 62 objects 63 opaque 1 colors 62 colors-bridged 62 colors-visible 62 xref 3 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.09ms tarjan 0.03ms scc-setup 0.02ms gather-xref 0.00ms xref-setup 0.00ms cleanup 0.05ms
10-05 15:14:52.847 D/Mono (21404): GC_BRIDGE: Complete, was running for 8.09ms
10-05 15:14:52.847 D/Mono (21404): GC_MAJOR_CONCURRENT_FINISH: (finishing) time 233.73ms, stw 14.81ms los size: 25608K in use: 24683K
10-05 15:14:52.847 D/Mono (21404): GC_MAJOR_SWEEP: major size: 1984K in use: 948K
10-05 15:14:52.850 D/Mono (21404): Requesting loading reference 1 (of 2) of Java.Interop.dll
10-05 15:14:52.851 D/Mono (21404): Loading reference 1 of Java.Interop.dll asmctx DEFAULT, looking for System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
10-05 15:14:52.851 D/Mono (21404): Assembly Ref addref Java.Interop[0x79fce51500] -> System.Core[0x7a0cbee180]: 5
10-05 15:14:53.267 D/Mono (21404): GC_TAR_BRIDGE bridges 0 objects 0 opaque 0 colors 0 colors-bridged 0 colors-visible 62 xref 3 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.09ms tarjan 0.03ms scc-setup 0.02ms gather-xref 0.00ms xref-setup 0.00ms cleanup 0.00ms
10-05 15:14:53.267 D/Mono (21404): GC_BRIDGE: Complete, was running for 0.13ms
10-05 15:14:53.267 D/Mono (21404): GC_MINOR: (Concurrent start) time 0.69ms, stw 5.17ms promoted 0K major size: 1984K in use: 949K los size: 58380K in use: 57451K
10-05 15:14:53.267 D/Mono (21404): GC_MAJOR_CONCURRENT_START: (LOS overflow)
10-05 15:14:54.108 I/zygote64(21404): Explicit concurrent copying GC freed 3(16KB) AllocSpace objects, 0(0B) LOS objects, 91% free, 1213KB/13MB, paused 129us total 5.950ms
10-05 15:14:54.108 D/Mono (21404): GC_TAR_BRIDGE bridges 58 objects 59 opaque 1 colors 58 colors-bridged 58 colors-visible 58 xref 3 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.03ms tarjan 0.02ms scc-setup 0.03ms gather-xref 0.00ms xref-setup 0.00ms cleanup 0.02ms
10-05 15:14:54.108 D/Mono (21404): GC_BRIDGE: Complete, was running for 6.91ms
10-05 15:14:54.108 D/Mono (21404): GC_MAJOR_CONCURRENT_FINISH: (finishing) time 836.63ms, stw 6.62ms los size: 99336K in use: 98411K
10-05 15:14:54.108 D/Mono (21404): GC_MAJOR_SWEEP: major size: 1984K in use: 939K
10-05 15:14:55.768 D/Mono (21404): GC_TAR_BRIDGE bridges 0 objects 0 opaque 0 colors 0 colors-bridged 0 colors-visible 58 xref 3 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.03ms tarjan 0.02ms scc-setup 0.03ms gather-xref 0.00ms xref-setup 0.00ms cleanup 0.00ms
10-05 15:14:55.768 D/Mono (21404): GC_BRIDGE: Complete, was running for 0.13ms
10-05 15:14:55.768 D/Mono (21404): GC_MINOR: (Concurrent start) time 0.65ms, stw 7.14ms promoted 0K major size: 1984K in use: 939K los size: 230412K in use: 229483K
10-05 15:14:55.768 D/Mono (21404): GC_MAJOR_CONCURRENT_START: (LOS overflow)
10-05 15:14:59.116 I/zygote64(21404): Explicit concurrent copying GC freed 3(16KB) AllocSpace objects, 0(0B) LOS objects, 91% free, 1213KB/13MB, paused 115us total 6.849ms
10-05 15:14:59.117 D/Mono (21404): GC_TAR_BRIDGE bridges 58 objects 59 opaque 1 colors 58 colors-bridged 58 colors-visible 58 xref 3 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.02ms tarjan 0.02ms scc-setup 0.07ms gather-xref 0.00ms xref-setup 0.00ms cleanup 0.02ms
10-05 15:14:59.117 D/Mono (21404): GC_BRIDGE: Complete, was running for 7.92ms
10-05 15:14:59.117 D/Mono (21404): GC_MAJOR_CONCURRENT_FINISH: (finishing) time 3341.47ms, stw 15.83ms los size: 394248K in use: 393323K
10-05 15:14:59.117 D/Mono (21404): GC_MAJOR_SWEEP: major size: 1984K in use: 937K
uj5u.com熱心網友回復:
在您的回圈中,只需DayOfWeek在將其添加到您的串列之前檢查
for (DateTime date = a; date <= b; date.AddDays(1))
{
if (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday)
{
allDates.Add(date);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/313160.html
下一篇:為什么在我的XamarinAndroidnetwork_security_config.xml檔案的debug-overrides標記中包含cleartextTrafficPermitted標記時未檢
