Xamarin/XAML/C# - 构建一个计算 DateTimes 数组中所有周末天数的方法
Xamarin/XAML/C# - Build a method that counts all the weekend days in an array of DateTime's
问题: 我正在构建一个应用程序来对假期天数进行分类。我目前正在处理的页面从用户那里获取 2 个日期(开始日期和结束日期),然后输出所选的天数,以在应用程序的其他地方使用。
解决方案: 最初,我想禁用星期六和星期日的选择,但是浮动在 HERE 附近的一个解决方案不适用于我的解决方案。我的第二个想法是取而代之的是采用用户选择的日期范围并构建这些日期的数组,我可以在其中计算星期六和星期日的数量并将它们从全天总数中删除。
这是计算器页面 class 背后的代码,我在其中创建了 '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
在您的循环中,只需检查 DayOfWeek
,然后再将其添加到您的列表
for (DateTime date = a; date <= b; date.AddDays(1))
{
if (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday)
{
allDates.Add(date);
}
}
问题: 我正在构建一个应用程序来对假期天数进行分类。我目前正在处理的页面从用户那里获取 2 个日期(开始日期和结束日期),然后输出所选的天数,以在应用程序的其他地方使用。
解决方案: 最初,我想禁用星期六和星期日的选择,但是浮动在 HERE 附近的一个解决方案不适用于我的解决方案。我的第二个想法是取而代之的是采用用户选择的日期范围并构建这些日期的数组,我可以在其中计算星期六和星期日的数量并将它们从全天总数中删除。
这是计算器页面 class 背后的代码,我在其中创建了 '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
在您的循环中,只需检查 DayOfWeek
,然后再将其添加到您的列表
for (DateTime date = a; date <= b; date.AddDays(1))
{
if (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday)
{
allDates.Add(date);
}
}