首先我想谈谈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;
}
}
}
注意:这里我只是在Header 的DatePicker 中设置星期几,因为还没有找到修改Picker UI 的方法。如果继续研究document,我认为需要更多时间来找到最佳解决方案。
目前的显示效果如下: