Visual Studio/WPF > ObjectDataProvider > JST 및 UTC 보기 > Provider.Refresh() | 이벤트 추가 | DateTime.Now.ToLongTimeString()

운영 환경
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community

h tp : /// s ぇ l. ldb㎉g. jp / archi s / 52333863. HTML
를 바탕으로 학습 중.

다음과 같이 변경해 보았다.
  • UTC 용 Provider 추가
  • DateTime.UtcNow를 사용하여 UTC 표시

  • MainWindow.xaml.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    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;
    
    namespace _170608_t1300_timer
    {
        /// <summary>
        /// MainWindow.xaml の相互作用ロジック
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                MyClock myClock = this.Resources["keyMyClock"] as MyClock;
                myClock.ProviderJST = this.Resources["keyNowTime"] as ObjectDataProvider;
                myClock.ProviderUTC = this.Resources["keyUtcTime"] as ObjectDataProvider;
            }
        }
        public class MyClock
        {
            private System.Timers.Timer _timer = new System.Timers.Timer();
            public ObjectDataProvider ProviderJST { get; set; }
            public ObjectDataProvider ProviderUTC { get; set; }
            public MyClock()
            {
                _timer.Interval = 1000; // msec
                _timer.Elapsed += _timer_Elapsed; // イベント追加
                _timer.Enabled = true;
            }
            private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                ProviderJST.Refresh();
                ProviderUTC.Refresh();
            }
            public string NowTime()
            {
                return "JST:" + DateTime.Now.ToLongDateString() + DateTime.Now.ToLongTimeString();
            }
            public string UtcTime()
            {
                return "UTC:" + DateTime.UtcNow.ToLongDateString() + DateTime.UtcNow.ToLongTimeString();                
            }
        }
    }
    

    MainWindow.xaml
    <Window x:Class="_170608_t1300_timer.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:_170608_t1300_timer"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <local:MyClock x:Key="keyMyClock" />
            <ObjectDataProvider MethodName="NowTime" x:Key="keyNowTime"
                                ObjectInstance="{StaticResource keyMyClock}"/>
            <ObjectDataProvider MethodName="UtcTime" x:Key="keyUtcTime"
                                ObjectInstance="{StaticResource keyMyClock}"/>
        </Window.Resources>
        <Grid DataContext="{Binding Source={StaticResource keyNowTime}}">
            <StackPanel Margin="20">
                <TextBlock Height="28" Name="textJST" FontSize="18"
                Text="{Binding Source={StaticResource keyNowTime}}"/>
                <TextBlock Height="28" Name="textUTC" FontSize="18"
                Text="{Binding Source={StaticResource keyUtcTime}}"/>
            </StackPanel>
        </Grid>
    </Window>
    



    흠집



    사경하는 가운데 XAML에서 이하의 실수를 한 것으로 시계가 표시되지 않았다.
            <ObjectDataProvider MethodName="NowTime" x:Key="keyNowTime"
                                ObjectInstance="={StaticResource keyMyClock}"/>
    

    정확하게는 아래.
            <ObjectDataProvider MethodName="NowTime" x:Key="keyNowTime"
                                ObjectInstance="{StaticResource keyMyClock}"/>
    
    {StaticResource 앞에 여분의 =를 입력했습니다.

    관련 링크



    [XAML/C#] WPF에서 타이머를 사용하려면(Windows Form에서 WPF로)
    htps : // 여기. msd 응. 미 c 로소 ft. 이 m / 우동 ws에서 sk와 p / MLCVB - WPF - 우동 ws - WPF - 2 아 b6085

    v0.2



    XAML 변경.

    MainWindow.xaml
    <Window x:Class="_170608_t1300_timer.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:_170608_t1300_timer"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <local:MyClock x:Key="keyMyClock" />
            <ObjectDataProvider MethodName="NowTime" x:Key="keyNowTime"
                                ObjectInstance="{StaticResource keyMyClock}"/>
            <ObjectDataProvider MethodName="UtcTime" x:Key="keyUtcTime"
                                ObjectInstance="{StaticResource keyMyClock}"/>
        </Window.Resources>
        <!--<Grid DataContext="{Binding Source={StaticResource keyNowTime}}">-->
        <Grid>
            <StackPanel Margin="20">
                <TextBlock Height="28" Name="textJST" FontSize="18"
                Text="{Binding Source={StaticResource keyNowTime}}"/>
                <TextBlock Height="28" Name="textUTC" FontSize="18"
                Text="{Binding Source={StaticResource keyUtcTime}}"/>
            </StackPanel>
        </Grid>
    </Window>
    

    Grid에 Binding하지 않아도 좋은 것 같다.
    (전 기사의 코멘트란에 불필요한 것이 기재되어 있었다).

    좋은 웹페이지 즐겨찾기