【问题标题】:How to use current date time in xamarin forms date picker?如何在 xamarin 表单日期选择器中使用当前日期时间?
【发布时间】:2020-10-08 16:29:10
【问题描述】:

我有一个申请休假的 xamarin 应用程序。学生应在请假之日起 3 天内提出请假申请。如果他们在 2020 年 10 月 3 日休假,那么他们应该在 10 月 6 日或之前申请休假。我正在使用日历选择日期并将最长日期设置为今天,将最短日期设置为今天 -3。所以最后剩下的几天将不可用。它正在工作。但是当学生手动将设备日期设置为任何过去的日期时。然后应用程序中的日历也更改为该日期。如何防止这种情况发生?

请帮我解决这个问题

【问题讨论】:

  • 您将需要检查一些外部时间源 - 时间服务器或您自己的服务器需要在提交数据时验证数据

标签: c# xamarin.forms datepicker


【解决方案1】:

这是一个从互联网获取日期的小示例,不依赖于您的本地计算机

查看:

<StackLayout>
   <Label x:Name="dateLabel" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
   <Button Text="Get Date" Clicked="Button_Clicked" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
</StackLayout>

代码背后:

private void Button_Clicked(object sender, EventArgs e)
        {
            using (var httpClient = new HttpClient())
            {
                var response = httpClient.GetStringAsync(new Uri("http://worldtimeapi.org/api/timezone/Asia/Kolkata")).Result;

                ServerDateTime sDateTime = Newtonsoft.Json.JsonConvert.DeserializeObject<ServerDateTime>(response);

                dateLabel.Text = sDateTime.datetime.ToString("dd.MM.yyyy");
            }
        }

型号

public class ServerDateTime
    {
        public string abbreviation { get; set; }
        public string client_ip { get; set; }
        public DateTime datetime { get; set; }
        public int day_of_week { get; set; }
        public int day_of_year { get; set; }
        public bool dst { get; set; }
        public object dst_from { get; set; }
        public int dst_offset { get; set; }
        public object dst_until { get; set; }
        public int raw_offset { get; set; }
        public string timezone { get; set; }
        public int unixtime { get; set; }
        public DateTime utc_datetime { get; set; }
        public string utc_offset { get; set; }
        public int week_number { get; set; }
    }

关于 cleartext HTTP 问题的一点旁注:

https://forums.xamarin.com/discussion/164771/cleartext-http-traffic-not-permitted

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    相关资源
    最近更新 更多