【万里征程——Windows App开发】DatePickerFlyout、TimePickerFlyout的使用

已经有挺长时间没有更新这个专栏了,不过刚才有网友私信问我一个问题现在就火速更新上一篇~

这一篇讲解在WP上DataPickerFlyout和TimePickerFlyout的使用,但它们只能在WP上跑哦~

 <Grid Background="Blue">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

            <StackPanel Grid.Row="0" Margin="12" Orientation="Vertical">
                <Button Content="Let's Show DatePicker" >
                    <Button.Flyout>
                        <DatePickerFlyout x:Name="datePickerFlyout" Title="选择日期"
                                      DatePicked="datePickerFlyout_DatePicked" Closed="datePickerFlyout_Closed" />
                    </Button.Flyout>
                </Button>

                <DatePicker Header="Date"  Margin="4" />
                <TextBlock Name="textBlockDate" FontSize="20" Margin="4" />
            </StackPanel>

        <StackPanel Grid.Row="1" Margin="12" Orientation="Vertical">
            <Button Content="Let's Show TimePicker" >
                <Button.Flyout>
                    <TimePickerFlyout x:Name="timePickerFlyout" Title="选择时间"
                                      TimePicked="timePickerFlyout_TimePicked" Closed="timePickerFlyout_Closed" />
                </Button.Flyout>
            </Button>

            <TimePicker Header="Time" Margin="4" />
            <TextBlock Name="textBlockTime" FontSize="20" Margin="4"/>
        </StackPanel>
    </Grid>

后台事件如下:

 public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // 令天数加1
            datePickerFlyout.Date = DateTimeOffset.Now.AddDays(1);

            // 设置可选择的最大年份和最小年份
            datePickerFlyout.MinYear = DateTimeOffset.Now.AddYears(-100);
            datePickerFlyout.MaxYear = DateTimeOffset.Now.AddYears(100);

            // 此处也可以令“年“、”月“、”日“中的某一个不显示
            datePickerFlyout.YearVisible = true;
            datePickerFlyout.MonthVisible = true;
            datePickerFlyout.DayVisible = true;

            // 选择的历法
            // (Gregorian 公历, Hebrew 希伯来历, Hijri 回历, Japanese 日本历, Julian 罗马儒略历, Korean 朝鲜历, Taiwan 台湾历, Thai 泰国历, UmAlQura 古兰经历)
            datePickerFlyout.CalendarIdentifier = CalendarIdentifiers.Gregorian;                                                                                                         

            // Time - TimePicker 控件当前显示的时间
            timePickerFlyout.Time = new TimeSpan(18, 0, 0);

            // 设置TimePickerFlyout的分钟选择框内数字的增幅
            timePickerFlyout.MinuteIncrement = 2;                                                                                                           

            //设置为24小时制,也可以为12小时制
            timePickerFlyout.ClockIdentifier = ClockIdentifiers.TwentyFourHour;
        }

        // 当用户点击DatePicker的完成按钮后激活该事件
        private void datePickerFlyout_DatePicked(DatePickerFlyout sender, DatePickedEventArgs args)
        {
            textBlockDate.Text = args.NewDate.ToString("yyyy-MM-dd hh:mm:ss");
            textBlockDate.Text += Environment.NewLine;
        }

        // 当用户点击DatePicker的取消按钮或手机的返回按钮后激活该事件,当点击完成按钮后也将调用该事件
        private void datePickerFlyout_Closed(object sender, object e)
        {
            textBlockDate.Text += "You just close the DatePickerFlyout.";
            textBlockDate.Text += Environment.NewLine;
        }

        // 当用户点击TimePicker的完成按钮后激活该事件
        private void timePickerFlyout_TimePicked(TimePickerFlyout sender, TimePickedEventArgs args)
        {
            // e.OldTime - 原时间
            // e.NewTime - 新时间
            textBlockTime.Text = args.NewTime.ToString("c");
            textBlockTime.Text += Environment.NewLine;
        }

        // 当用户点击TimePicker的取消按钮或手机的返回按钮后激活该事件,当点击完成按钮后也将调用该事件
        private void timePickerFlyout_Closed(object sender, object e)
        {
            textBlockTime.Text += "You just close the TimePickerFlyout.";
            textBlockTime.Text += Environment.NewLine;
        }
    }

时间仓促就记录到这里咯,掰掰~

该网友说我刚写的不是他所需要的,so……重来一遍吧……

简单的讲,Flyout有两种创建方式,一种就是上面的通过Button的Flyout属性。另一种是通过FlyoutBase.AttachedFlyout属性给任何的FrameworkElement对象添加它。

关于FrameworkElement的更多知识,可以访问以下链接。

https://msdn.microsoft.com/zh-cn/library/vstudio/system.windows.frameworkelement(v=vs.100).aspx

https://msdn.microsoft.com/en-us/library/system.windows.frameworkelement(v=vs.110).aspx

而Flyout则有6种不同的类型:Flyout、DatePickerFlyout、ListPickerFlyout、MenuFlyout、TimePickerFlyout。

时间紧迫就直接Show代码了。

XAML代码:

    <Page.Resources>
        <Style TargetType="Button">
            <Setter Property="Margin" Value="12"/>
            <Setter Property="FontSize" Value="20"/>
            <Setter Property="Foreground"  Value="White"/>
            <Setter Property="Background" Value="Green"/>
        </Style>
    </Page.Resources>

    <Grid Background="Blue">
        <StackPanel Orientation="Vertical">
            <!-- Flyout -->
            <Button Content="Let's Show Flyout">
                <Button.Flyout>
                    <Flyout>
                        <StackPanel >
                            <TextBox PlaceholderText="What do you want to say?"/>
                            <Button HorizontalAlignment="Right" Content="Yup"/>
                        </StackPanel>
                    </Flyout>
                </Button.Flyout>
            </Button>

            <!-- DatePickerFlyout -->
            <Button Content="Let's Show DatePicker" HorizontalAlignment="Right">
                <Button.Flyout>
                    <DatePickerFlyout Title="You need to choose Date: "  DatePicked="DatePickerFlyout_DatePicked"/>
                </Button.Flyout>
            </Button>

            <!-- ListPickerFlyout -->
            <Button Content="Let's Show ListPicker" >
                <Button.Flyout>
                    <ListPickerFlyout x:Name="listPickerFlyout" Title="选择操作系统:" ItemsPicked="listPickerFlyout_ItemsPicked"  >
                        <ListPickerFlyout.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding}" FontSize="30"></TextBlock>
                            </DataTemplate>
                        </ListPickerFlyout.ItemTemplate>
                    </ListPickerFlyout>
                </Button.Flyout>
            </Button>

            <!--  MenuFlyout -->
            <Button x:Name="menuFlyoutButton" Content="Let's Show MenuFlyout" HorizontalAlignment="Right">
                <Button.Flyout >
                    <MenuFlyout>
                        <MenuFlyoutItem Text="You just say yes?" Click="MenuFlyoutItem_Click"/>
                        <MenuFlyoutItem Text="You just say no?" Click="MenuFlyoutItem_Click"/>
                        <MenuFlyoutItem Text="You say nothing..." Click="MenuFlyoutItem_Click"/>
                    </MenuFlyout>
                </Button.Flyout>
            </Button>

            <!--  PickerFlyout -->
            <Button Content="Let's Show Picker" >
                <Button.Flyout>
                    <PickerFlyout  Confirmed="PickerFlyout_Confirmed" ConfirmationButtonsVisible="True">
                        <TextBlock Text="Are you ok?" FontSize="30" Margin="0 100 0 0"/>
                    </PickerFlyout>
                </Button.Flyout>
            </Button>

            <!-- TimePickerFlyout -->
            <Button Content="Let's Show TimePicker" HorizontalAlignment="Right">
                <Button.Flyout>
                    <TimePickerFlyout Title="You need to choose Time: "  TimePicked="TimePickerFlyout_TimePicked"/>
                </Button.Flyout>
            </Button>

            <!-- FlyoutBase -->
            <TextBlock Text="Game Over" Margin="12" Foreground="Wheat" Tapped="TextBlock_Tapped" FontSize="20">
                <FlyoutBase.AttachedFlyout>
                    <Flyout>
                        <TextBox Text="哎哟,不错哦!"/>
                    </Flyout>
                </FlyoutBase.AttachedFlyout>
            </TextBlock>
        </StackPanel>
    </Grid>

后台C#代码:

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            // 绑定List数据到ListPickerFlyout
            listPickerFlyout.ItemsSource = new List<string> { "Windows 10", "Windows 8/8.1", "Windows 7", "Windows Vista", "Windows XP","Others" };
        }

        // DatePickerFlyout的日期选中事件,此处事件内有是包含日期的MessageDialog控件
        private async void DatePickerFlyout_DatePicked(DatePickerFlyout sender, DatePickedEventArgs args)
        {
            await new MessageDialog(args.NewDate.ToString()).ShowAsync();
        }

        // ListPickerFlyout的选中事件,选择列表中的一项后会以弹窗的方式显示出来
        private async void listPickerFlyout_ItemsPicked(ListPickerFlyout sender, ItemsPickedEventArgs args)
        {
            if (sender.SelectedItem != null)
            {
                await new MessageDialog("You choose: " + sender.SelectedItem.ToString()).ShowAsync();
            }
        }

        // MenuFlyout的菜单选项的点击事件,将选择的本文赋值给Content
        private void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
        {
            menuFlyoutButton.Content = (sender as MenuFlyoutItem).Text;
        }

        // PickerFlyout的确认事件,此处事件内有是包含字符串的MessageDialog控件
        private async void PickerFlyout_Confirmed(PickerFlyout sender, PickerConfirmedEventArgs args)
        {
            await new MessageDialog("You choose ok").ShowAsync();
        }

        // TimePickerFlyout的时间选中事件,此处事件内有是包含所选时间的MessageDialog控件
        private async void TimePickerFlyout_TimePicked(TimePickerFlyout sender, TimePickedEventArgs args)
        {
            await new MessageDialog(args.NewTime.ToString()).ShowAsync();
        }          

        // 通过FlyoutBase.ShowAttachedFlyout方法来展示出Flyout控件
        private void TextBlock_Tapped(object sender, TappedRoutedEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            if (element != null)
            {
                FlyoutBase.ShowAttachedFlyout(element);
            }
        }
    }

好了代码就到这里了,来几张截图。童鞋们看出来我的手机型号了么?

那这一篇就结束啦,我的手机是Lumia 638……




感谢您的访问,希望对您有所帮助。 欢迎大家关注、收藏以及评论。



为使本文得到斧正和提问,转载请注明出处:
http://blog.csdn.net/nomasp


时间: 2024-07-30 03:46:04

【万里征程——Windows App开发】DatePickerFlyout、TimePickerFlyout的使用的相关文章

【万里征程——Windows App开发】文件&amp;amp;数据——写入与读取

在前面 [万里征程--Windows App开发]文件&数据--读取文件/文件夹名我们简单得获取了文件名和文件夹名,很明显没有太大的意思对吧,这里就来写真正的文件.而在 [万里征程--Windows App开发]文件&数据--文件选取器中,已经能够通过文件选取器保存和打开文件了,这里是对保存和读取文件的一些补充. 准备工作 在XAML中添加一个TextBlock用于显示相关信息,添加一个Button来使用它的Click事件,当然了,最后分别创建2个. 创建文件和读取文件 1.实例化Stor

【万里征程——Windows App开发】控件大集合2

下面再来看看一些前面还没有讲过的控件,不过控件太多以至于无法全部列出来,大家只好举一反三啦. Button 前面最常用的控件就是Button啦,Button还有一个有意思的属性呢,当把鼠标指针放在Button上时,就会在Button的头顶冒出一串文本啦.这个不太截图哎-- <Button ToolTipService.ToolTip="Go to www.blog.csdn.net/nomasp" Margin="692,458,0,230" /> Bu

【万里征程——Windows App开发】SemanticZoom视图切换

相信用过Windows Phone或者Windows 8/8.1/10的朋友对下面这张截图肯定不陌生.这就是通过SemanticZoom来实现的,当数据过多时,这种控件尤其适用.它有一个放大视图ZoomedInView和一个缩小试图ZoomedOutView,前者主要用来显示当前页面的详细信息,后者则致力于快速导航. 那么我就自己来动手实践咯,首先我们在XAML中添加大致的界面,就像画画要先画轮廓一样. <Grid Name="grid1" Background="{T

【万里征程——Windows App开发】应用栏

基本的用法我们在 [万里征程--Windows App开发]页面布局和基本导航中已经讲过了,这里继续补充关于应用栏的更多用法. Icon 在之前的学习中,我们知道Icon属性中有很多很多系统预定义,但也许这些还是不够的,现在就来增加几种用法咯. 字符集应用 <AppBarToggleButton Label="Sigma" Click="AppBarButton_Click"> <AppBarToggleButton.Icon> <Fo

【万里征程——Windows App开发】ListView&amp;amp;GridView之分组

本文承接[万里征程--Windows App开发]ListView&GridView之添加数据. 在上一篇中我们已经了解了怎样将数据绑定到ListView或GridView,但既然要用到这两个控件往往是因为数据繁多,那么几乎就不可避免的要让其能够分组.我们所绑定的数据源可能是项列表,其中的每个项甚至还有其自己的项,那么问题就来了. 一时不会也想不出什么宏伟的例子,就做一个简单的闹钟的时间表的ListView和GridView吧.那么先在项目中添加一个类,最好在Shared下.内容都是很简易的,闹

【万里征程——Windows App开发】页面布局和基本导航

上一篇博客中大概的新建了一个应用,几乎是空白的.如果是初学者的话现在当然想往里面加点东西对不对.那么这篇博客就来看看页面的布局都是怎样的. 首先安装上一篇博客中的顺序来新建一个项目.新建好之后就点开MainPage.xaml开始敲代码啦.^_^ <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid.RowDefinitions> <RowDefinitio

【万里征程——Windows App开发】设置共享(共享源和共享目标)

上一篇博客简单介绍了通过粘贴板来共享数据,这一节将会添加更为强大的功能哦. 以下就是大概的样式了,随便看看就好了,这都不是重点. <Grid Background="AliceBlue"> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition /> <RowDefinition Height="auto"/>

【万里征程——Windows App开发】如何保存、读取、删除应用数据

在前面的几篇博客中,都是关于数据的,这方面的内容其实还有很多很多,省略掉一部分后,也还是有很多.这一篇将是很重要的一部分,关于保存和读取数据,对于游戏而言,这一点尤其重要. 先来看看一个大概的背景吧,我这里写的很简单啦^_^ 保存的内容就是这四个框框里填写的数据咯.先上XAML代码. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <StackPanel Orientati

【万里征程——Windows App开发】开发准备

操作系统及SDK 操作系统 如果打算开发Windows App,那么你的电脑就不能再用老旧的Windows 7了.推荐使用Windows 8.1.写这篇博客的时候,我用的操作系统是Windows 10 Pro Technical Preview [Build 10041]. 操作系统除了在官网下载之外,还可以在DreamSpark等地方下载.DreamSpark上除了Office其他微软操作系统.开发工具及其他软件对学生均免费开放. 另外再推荐一个网站:MSDN i tell you Visua