XAML - 時間選擇器



TimePicker 是一種允許使用者選擇時間值的控制元件。TimePicker 類的層次繼承如下:

TimePicker Hierarchy

屬性

序號 屬性及描述
1

ClockIdentifier

獲取或設定要使用的時鐘系統。

2

ClockIdentifierProperty

獲取 ClockIdentifier 依賴屬性的識別符號。

3

Header

獲取或設定控制元件標題的內容。

4

HeaderProperty

標識 Header 依賴屬性。

5

HeaderTemplate

獲取或設定用於顯示控制元件標題內容的 DataTemplate。

6

HeaderTemplateProperty

標識 HeaderTemplate 依賴屬性。

7

MinuteIncrement

獲取或設定一個值,該值指示分鐘選擇器中顯示的時間增量。例如,15 指定 TimePicker 分鐘控制元件僅顯示 00、15、30、45 選項。

8

MinuteIncrementProperty

獲取 MinuteIncrement 依賴屬性的識別符號。

9

Time

獲取或設定當前在時間選擇器中設定的時間。

10

TimeProperty

獲取 Time 依賴屬性的識別符號。

事件

序號 事件及描述
1

ManipulationCompleted

當對 UIElement 的操作完成時發生。(從 UIElement 繼承)

2

ManipulationDelta

當輸入裝置在操作過程中改變位置時發生。(從 UIElement 繼承)

3

ManipulationInertiaStarting

當輸入裝置在操作過程中與 UIElement 物件失去接觸並且慣性開始時發生。(從 UIElement 繼承)

4

ManipulationStarted

當輸入裝置開始對 UIElement 進行操作時發生。(從 UIElement 繼承)

5

ManipulationStarting

當首次建立操作處理器時發生。(從 UIElement 繼承)

6

TimeChanged

當時間值更改時發生。

方法

序號 方法及描述
1

OnManipulationCompleted

在 ManipulationCompleted 事件發生之前呼叫。(從 Control 繼承)

2

OnManipulationDelta

在 ManipulationDelta 事件發生之前呼叫。(從 Control 繼承)

3

OnManipulationInertiaStarting

在 ManipulationInertiaStarting 事件發生之前呼叫。(從 Control 繼承)

4

OnManipulationStarted

在 ManipulationStarted 事件發生之前呼叫。(從 Control 繼承)

5

OnManipulationStarting

在 ManipulationStarting 事件發生之前呼叫。(從 Control 繼承)

示例

以下示例演示了在 XAML 應用程式中使用 TimePicker 的方法。以下是建立和初始化具有某些屬性的 TimePicker 的 XAML 程式碼。

<Page x:Class = "XAMLTimePicker.MainPage" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:local = "using:XAMLTimePicker" 
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" 
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
   mc:Ignorable = "d">

   <Grid Background = "{ThemeResource ApplicationPageBackgroundThemeBrush}">
      <StackPanel Orientation = "Horizontal" Height = "60" Margin = "46,67,-46,641">
         <TimePicker x:Name = "arrivalTimePicker" Header = "Arrival Time" Margin = "0,1"/> 
         <Button Content = "Submit" Click = "SubmitButton_Click"
            Margin = "5,0,0,-2" VerticalAlignment = "Bottom"/> 
         <TextBlock x:Name = "Control1Output" FontSize = "24"/> 
      </StackPanel> 
   </Grid> 
	
</Page>

以下是 C# 中的點選事件實現:

using System; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls;

namespace XAMLTimePicker {
   public sealed partial class MainPage : Page {
      public MainPage() { 
         this.InitializeComponent(); 
      }
      private void SubmitButton_Click(object sender, RoutedEventArgs e) {
         if (VerifyTimeIsAvailable(arrivalTimePicker.Time) == true) {
            Control1Output.Text = string.Format("Thank you. Your appointment is set for {0}.",
               arrivalTimePicker.Time.ToString());
         } else {
            Control1Output.Text = "Sorry, we're only open from 8AM to 5PM.";
         }
      }
      private bool VerifyTimeIsAvailable(TimeSpan time) {
         // Set open (8AM) and close (5PM) times. 
         TimeSpan openTime = new TimeSpan(8, 0, 0); 
         TimeSpan closeTime = new TimeSpan(17, 0, 0);
		
         if (time >= openTime && time < closeTime) { 
            return true; // Open 
         }
         return false; // Closed 
      }
   }
}

編譯並執行以上程式碼時,將顯示以下輸出。當選擇的時間在上午 8 點到下午 5 點之間時,將顯示以下訊息:

TimePicker Output

否則,將顯示以下訊息:

TimePicker Output 1

建議您執行以上示例程式碼,並嘗試其他一些屬性和事件。

xaml_controls.htm
廣告

© . All rights reserved.