【问题标题】:How can Xamarin DatePicker show day of week?Xamarin DatePicker 如何显示星期几?
【发布时间】:2020-01-01 20:48:58
【问题描述】:

我在 iOS 和 UWP 中尝试了 Xamarin Forms DatePicker,格式为“D”。
在 iOS 中,星期几显示在 DatePicker 中,看起来像

2016 年 4 月 24 日,星期日

但在 UWP 中,DatePicker 不显示星期几,它显示:

2016 年 4 月 24 日

所以,我的问题是:

如何在 UWP 的 DatePicker 中显示星期几?

这是我在 xaml 中的代码:

<DatePicker WidthRequest="350" HeightRequest="30" Margin="20,0,0,200" x:Name="DtPicker" Format="D" />

【问题讨论】:

  • 如果您自己进行格式化,它仍然不显示星期几吗?类似于 Format="dddd, MMMM d, yyyy"。
  • 不,没有任何改变。

标签: xamarin.forms datepicker


【解决方案1】:

首先我想谈谈Format = "D"。来自文档The Long Date ("D") Format Specifier,这是一个字符串值。但是不能在DatePicker中显示。

DateTime date1 = new DateTime(2008, 4, 10);
Console.WriteLine(date1.ToString("D", 
                  CultureInfo.CreateSpecificCulture("en-US")));
// Displays Thursday, April 10, 2008                        
Console.WriteLine(date1.ToString("D", 
                  CultureInfo.CreateSpecificCulture("pt-BR")));
// Displays quinta-feira, 10 de abril de 2008                        
Console.WriteLine(date1.ToString("D", 
                  CultureInfo.CreateSpecificCulture("es-MX")));
// Displays jueves, 10 de abril de 2008

现在你可以看看在 UWP 中设计的 DatePicker,它不能显示星期几。

所以唯一的办法就是自定义 DatePicker。

在 Xamarin Forms 中,使用 Custom Renderers 可以在 UWP 中自定义 DatePciker

在表单中创建一个自定义 DatePicker

public class CustomDatePicker : DatePicker
{
}

Xaml 中使用它:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local="clr-namespace:App8"
             mc:Ignorable="d"
             x:Class="App8.MainPage">

    <StackLayout Padding="100">
        <!-- Place new controls here -->
        <Label Text="Welcome to Xamarin.Forms!" 
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />
        <local:CustomDatePicker />
    </StackLayout>

</ContentPage>

然后在 UWP 中,创建 自定义渲染器类

[assembly: ExportRenderer(typeof(CustomDatePicker), typeof(CustomDatePickerRenderer))]
namespace App8.UWP
{
    public class CustomDatePickerRenderer : DatePickerRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                Control.Background = new SolidColorBrush(Colors.Cyan);
                var formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("dayofweek");
                DateTime dateToFormat = DateTime.Now;
                var mydate = formatter.Format(dateToFormat);
                Control.Header = mydate;
                Control.DateChanged += Control_DateChanged;
            }
        }

        private void Control_DateChanged(object sender, Windows.UI.Xaml.Controls.DatePickerValueChangedEventArgs e)
        {
            //throw new NotImplementedException();
            var formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("month day dayofweek year");
            DateTime dateToFormat = e.NewDate.DateTime;
            var mydate = formatter.Format(dateToFormat);
            Control.Header = mydate;
        }
    }
}

注意:这里我只是在HeaderDatePicker 中设置星期几,因为还没有找到修改Picker UI 的方法。如果继续研究document,我认为需要更多时间来找到最佳解决方案。

目前的显示效果如下

【讨论】:

  • 你说得对,现在我确实看到了 dayOfWeek,谢谢!我认为这是在 UWP 下运行时 datePicker 的问题,因为 dayOfWeek 在 iOS 中显示。你认为 datePicker 可以切换月份和日期,比如“29 | August | 2019”吗?
  • @renintx 有可能。这是一个参考讨论,你可以看看。 stackoverflow.com/questions/26422526/…
猜你喜欢
  • 2020-10-23
  • 2014-09-22
  • 1970-01-01
  • 2015-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-10
  • 1970-01-01
相关资源
最近更新 更多