example WPF Timer

タイマー(PGソース)

XAML(VB)


<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Wpf-Appl-Timer" Height="240" Width="575">
<Grid>
<Label Content="Date"
Height="50"
HorizontalAlignment="Left"
Margin="224,37,0,0"
Name="Label_Date"
VerticalAlignment="Top"
Width="298"
FontSize="24"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center" />
<Label Content="Time"
FontSize="24"
Height="50"
HorizontalAlignment="Left"
HorizontalContentAlignment="Center"
Margin="224,102,0,0"
Name="Label_Time"
VerticalAlignment="Top"
VerticalContentAlignment="Center"
Width="298" />
<Calendar Height="177"
HorizontalAlignment="Left"
Margin="20,12,0,0"
Name="Calendar1"
VerticalAlignment="Top"
Width="180" />
</Grid>
</Window>

VB2010


Imports System.Windows.Threading

Class MainWindow
Private DispatcherTimer As New DispatcherTimer()

Public Sub New()
InitializeComponent()

DispatcherTimer = New DispatcherTimer(DispatcherPriority.Normal)
DispatcherTimer.Interval = New TimeSpan(0, 0, 1)
AddHandler DispatcherTimer.Tick, AddressOf dispatcherTimer_Tick
DispatcherTimer.Start()
End Sub

Public Sub dispatcherTimer_Tick(ByVal sender As Object, ByVal e As EventArgs)
Me.Label_Date.Content = DateTime.Now
Me.Label_Time.Content = Format(DateTime.Now, "hh:mm:ss")
End Sub
End Class

内容は同じ

XAML(C#)



<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="249" Width="525">
<Grid Height="201">
<Label Content="Label" Height="42" HorizontalAlignment="Center" Margin="266,18,33,0" Name="Label_Date" VerticalAlignment="Top" Width="204" FontSize="20" HorizontalContentAlignment="Center" />
<Label Content="Label" Height="52" HorizontalAlignment="Center" Margin="266,65,34,0" Name="Label_Time" VerticalAlignment="Top" Width="204" FontSize="20" HorizontalContentAlignment="Center" />
<Calendar Height="192" HorizontalAlignment="Left" Margin="20,12,0,0" Name="calendar1" VerticalAlignment="Top" Width="210" />
</Grid>
</Window>

C#2010



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WpfApplication2
{
///


/// MainWindow.xaml の相互作用ロジック
///

public partial class MainWindow : Window
{
private DispatcherTimer dispatcherTimer = new DispatcherTimer();
public MainWindow()
{
InitializeComponent();
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer.Start();
}

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
this.Label_Date.Content = DateTime.Now;
this.Label_Time.Content = DateTime.Now.ToString("hh:mm:ss");
}
}
}



AX

TYKYUNC index