复制日期选择器以进行自定义

本文关键字:自定义 日期 选择器 复制 | 更新日期: 2023-09-27 18:26:08

我的目标是自定义一个DatePickerPage,通过从phone.codeplex.com(Windows phone Toolkit DatePickerPage)复制xaml和cs来创建我自己的。

我创建了一个名为CustomDatepickerPage的类,它的工作方式与源代码中所示的相同,但当我必须设置LoopingSelectors DataSources:时,我的类的构造函数出现了问题

     public CustomDatepickerPage()
        {
            InitializeComponent();
            // Hook up the data sources
            PrimarySelector.DataSource = new YearDataSource();
            SecondarySelector.DataSource = new MonthDataSource();
            TertiarySelector.DataSource = new DayDataSource();
            InitializeDateTimePickerPage(PrimarySelector, SecondarySelector, TertiarySelector);
        }

事实上,我无法初始化这3个DataSource,Visual Studio告诉我,由于保护级别的原因,我无法访问。

我不知道如何获得这些数据源。

谢谢你的帮助。

以下是CustomDatepickerPage的完整代码:

XAML:

    <primitives:DateTimePickerPageBase
    x:Class="MyNamespace.CustomDatepickerPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True"
    xmlns:primitives="clr-namespace:Microsoft.Phone.Controls.Primitives;assembly=Microsoft.Phone.Controls.Toolkit"
    xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit">
    <primitives:DateTimePickerPageBase.Resources>
        <controls:DateTimePickerResources x:Key="DateTimePickerResources"/>
        <ExponentialEase x:Key="Ease" EasingMode="EaseIn"/>
    </primitives:DateTimePickerPageBase.Resources>
    <Grid Background="{StaticResource PhoneChromeBrush}">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="VisibilityStates">
                <VisualState x:Name="Open">
                    <Storyboard>
                        <DoubleAnimation
                            Storyboard.TargetName="PlaneProjection"
                            Storyboard.TargetProperty="RotationX"
                            From="-50"
                            To="0"
                            Duration="0:0:0.2"/>
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="Closed">
                    <Storyboard>
                        <DoubleAnimation
                            Storyboard.TargetName="PlaneProjection"
                            Storyboard.TargetProperty="RotationX"
                            To="90"
                            Duration="0:0:0.2"
                            EasingFunction="{StaticResource Ease}"/>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Grid.Projection>
            <PlaneProjection x:Name="PlaneProjection"/>
        </Grid.Projection>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <!-- SystemTray placeholder -->
        <Rectangle
            x:Name="SystemTrayPlaceholder"
            Grid.Row="0"
            Height="32"/>
        <!-- Title -->
        <TextBlock
            x:Name="HeaderTitle"
            Grid.Row="1"
            Text="{Binding DatePickerTitle, Source={StaticResource DateTimePickerResources}}"
            FontFamily="{StaticResource PhoneFontFamilySemiBold}"
            FontSize="{StaticResource PhoneFontSizeMedium}"
            Foreground="{StaticResource PhoneForegroundBrush}"
            Margin="24,16,24,24"/>
        <!-- LoopingSelectors -->
        <Grid
            Grid.Row="2"
            HorizontalAlignment="Center">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <primitives:LoopingSelector
                x:Name="SecondarySelector"
                Grid.Column="0"
                Width="148"
                ItemSize="148,148"
                ItemMargin="6">
                <primitives:LoopingSelector.ItemTemplate>
                    <DataTemplate>
                        <StackPanel
                            HorizontalAlignment="Left"
                            VerticalAlignment="Bottom"
                            Margin="6">
                            <TextBlock
                                Text="{Binding MonthNumber}"
                                FontSize="54"
                                FontFamily="{StaticResource PhoneFontFamilySemiBold}"
                                Margin="0,-8"/>
                            <TextBlock
                                Text="{Binding MonthName}"
                                FontSize="20"
                                FontFamily="{StaticResource PhoneFontFamilyNormal}"
                                Margin="0,-2"/>
                        </StackPanel>
                    </DataTemplate>
                </primitives:LoopingSelector.ItemTemplate>
            </primitives:LoopingSelector>
            <primitives:LoopingSelector
                x:Name="TertiarySelector"
                Grid.Column="1"
                Width="148"
                ItemSize="148,148"
                ItemMargin="6">
                <primitives:LoopingSelector.ItemTemplate>
                    <DataTemplate>
                        <StackPanel
                            HorizontalAlignment="Left"
                            VerticalAlignment="Bottom"
                            Margin="6">
                            <TextBlock
                                Text="{Binding DayNumber}"
                                FontSize="54"
                                FontFamily="{StaticResource PhoneFontFamilySemiBold}"
                                Margin="0,-8"/>
                            <TextBlock
                                Text="{Binding DayName}"
                                FontSize="20"
                                FontFamily="{StaticResource PhoneFontFamilyNormal}"
                                Margin="0,-2"/>
                        </StackPanel>
                    </DataTemplate>
                </primitives:LoopingSelector.ItemTemplate>
            </primitives:LoopingSelector>
            <primitives:LoopingSelector
                Grid.Column="2"
                x:Name="PrimarySelector"
                Width="148"
                ItemSize="148,148"
                ItemMargin="6">
                <primitives:LoopingSelector.ItemTemplate>
                    <DataTemplate>
                        <StackPanel
                            HorizontalAlignment="Left"
                            VerticalAlignment="Bottom"
                            Margin="6">
                            <TextBlock
                                Text="{Binding YearNumber}"
                                FontSize="54"
                                FontFamily="{StaticResource PhoneFontFamilySemiBold}"
                                Margin="0,-8"/>
                            <TextBlock
                                Text=" "
                                FontSize="20"
                                FontFamily="{StaticResource PhoneFontFamilyNormal}"
                                Margin="0,-2"/>
                        </StackPanel>
                    </DataTemplate>
                </primitives:LoopingSelector.ItemTemplate>
            </primitives:LoopingSelector>
        </Grid>
    </Grid>
    <primitives:DateTimePickerPageBase.ApplicationBar>
        <shell:ApplicationBar IsVisible="True">
            <!--
            Due to platform restrictions, ApplicationBarIconButton.IconUri content can not come from the
            Microsoft.Phone.Controls.Toolkit assembly, so it is up to the application developer to ensure
            these resources are placed in the right location and marked as Build Action=Content. For
            convenience, platform-consistent images are included with the Toolkit installer.
            Note: ApplicationBarIconButton.Text automatically comes from the Microsoft.Phone.Controls.Toolkit
            resources (where it can be localized).
            -->
            <shell:ApplicationBarIconButton
                IconUri="/Toolkit.Content/ApplicationBar.Check.png"
                Text="DONE"/>
            <shell:ApplicationBarIconButton
                IconUri="/Toolkit.Content/ApplicationBar.Cancel.png"
                Text="CANCEL"/>
        </shell:ApplicationBar>
    </primitives:DateTimePickerPageBase.ApplicationBar>
</primitives:DateTimePickerPageBase>

CS:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Globalization;
using Microsoft.Phone.Controls.Primitives;
namespace MyNamespace
{
    public partial class CustomDatepickerPage : DateTimePickerPageBase
    {
       /// <summary>
        /// Initializes a new instance of the DatePickerPage control.
        /// </summary>
        public CustomDatepickerPage()
        {
            InitializeComponent();
            // Hook up the data sources
            PrimarySelector.DataSource = new YearDataSource();
            SecondarySelector.DataSource = new MonthDataSource();
            TertiarySelector.DataSource = new DayDataSource();
            InitializeDateTimePickerPage(PrimarySelector, SecondarySelector, TertiarySelector);
        }
        /// <summary>
        /// Gets a sequence of LoopingSelector parts ordered according to culture string for date/time formatting.
        /// </summary>
        /// <returns>LoopingSelectors ordered by culture-specific priority.</returns>
        protected override IEnumerable<LoopingSelector> GetSelectorsOrderedByCulturePattern()
        {
            string pattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.ToUpperInvariant();
            //if (DateTimePickerBase.DateShouldFlowRTL())
            //{
            //    char[] reversedPattern = pattern.ToCharArray();
            //    Array.Reverse(reversedPattern);
            //    pattern = new string(reversedPattern);
            //}
            return GetSelectorsOrderedByCulturePattern(
                pattern,
                new char[] { 'Y', 'M', 'D' },
                new LoopingSelector[] { PrimarySelector, SecondarySelector, TertiarySelector });
        }
        /// <summary>
        /// Handles changes to the page's Orientation property.
        /// </summary>
        /// <param name="e">Event arguments.</param>
        protected override void OnOrientationChanged(OrientationChangedEventArgs e)
        {
            if (null == e)
            {
                throw new ArgumentNullException("e");
            }
            base.OnOrientationChanged(e);
            SystemTrayPlaceholder.Visibility = (0 != (PageOrientation.Portrait & e.Orientation)) ?
                Visibility.Visible :
                Visibility.Collapsed;
        }
        /// <summary>
        /// Sets the selectors and title flow direction.
        /// </summary>
        /// <param name="flowDirection">Flow direction to set.</param>
        public override void SetFlowDirection(FlowDirection flowDirection)
        {
            HeaderTitle.FlowDirection = flowDirection;
            PrimarySelector.FlowDirection = flowDirection;
            SecondarySelector.FlowDirection = flowDirection;
            TertiarySelector.FlowDirection = flowDirection;
        }
    }
}

复制日期选择器以进行自定义

为了自定义DataPickerPage,我还必须将DataSource.csDataTimePickerPageBase.cs复制到我的项目中,并适当地更改名称空间(我想我还注释了一些关于从左到右流的行,这些行也调用了一些内部方法)。

好的,现在可以工作了。对于那些想要自定义自己的日期选择器的人,以下是步骤:

  • 转到Windows Phone Toolkit CodePlex,从Microsoft.Phone.Controls.Toolkit中的DateTimePickers文件夹获取文件。Grab DatePickerPage.xaml、DatePickerPage.xaml.cs、DateTimePickerPageBase.cs、DataSource.cs
  • 将DatePickerPage.xaml重命名为您选择的名称
  • 在您获得的所有文件中,将默认名称空间更改为您的个人名称空间
  • 在自定义DatePickerPage中,不要调用"primitives:DateTimePickerPageBase",而是使用您的个人命名空间调用DateTimePicker Page
  • 为了调用Validation和Cancellation的操作,请在DateTimePickerPageBase中将OnDoneButtonClick和OnCancelButtonClick方法转换为公共方法,以便从DatePickerPage中调用它们。
    • 并且,将DatePicker中的PickerPageUri更改为新创建的DatePickerPage

和"voilà":)

感谢lisp的宝贵贡献!