Xamarin DatePicker如何显示星期几?

Renintx

我在iOS和UWP中尝试使用格式=“ D”的Xamarin Forms DatePicker。
在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" />
少年江-MSFT

首先我想谈一谈Format = "D"从文档Long Date(“ D”)格式说明符开始,这是一个字符串值。但是,不能在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中,使用自定义渲染器可以DatePciker在UWP中自定义

在Forms中创建一个自定义的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,因为不是创始修改选择器alredy的UI的方式。我认为,如果继续研究文档,将需要更多时间来找到最佳解决方案

当前的显示效果如下

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章