Visual Studio/WPF > ObjectDataProvider > 버튼을 누를 때 이후 경과 시간 표시 > TimeSpan | string.Format() | Button Click 처리

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

h tp : /// s ぇ l. ldb㎉g. jp / archi s / 52333863. HTML
참고로 다음을 구현했습니다.
  • 버튼을 누르면 처리 시작
  • 경과 시간 표시

  • 사용한 것은
    - TimeSpan
    - string.Format()
    - 버튼 클릭 처리

    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_t1408_elapsedTime
    {
        /// <summary>
        /// MainWindow.xaml の相互作用ロジック
        /// </summary>
        public partial class MainWindow : Window
        {
            private MyClock _myClock;       
            public MainWindow()
            {
                InitializeComponent();
                _myClock = this.Resources["keyMyClock"] as MyClock;
                _myClock.Provider = this.Resources["keyElapsed"] as ObjectDataProvider;
            }
    
            private void B_start_Click(object sender, RoutedEventArgs e)
            {
                _myClock.SetStart();
            }
        }
        public class MyClock
        {
            private System.Timers.Timer _timer = new System.Timers.Timer(); // タイマーイベント用
            private System.DateTime _startTime; // B_start押下時の時刻
            private bool isStart = false; // 経過時間を測定するか
            public ObjectDataProvider Provider { 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)
            {
                Provider.Refresh();
            }
            public string GetElapsedTime()
            {
                if (isStart)
                {
                    TimeSpan ts = DateTime.Now - _startTime;
                    return string.Format("{0:000.###}", ts.TotalSeconds);
                }
                return "";
            }
            public void SetStart()
            {
                isStart = true;
                _startTime = DateTime.Now;
            }
        }
    }
    

    MainWindow.xaml
    <Window x:Class="_170608_t1408_elapsedTime.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_t1408_elapsedTime"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <local:MyClock x:Key="keyMyClock"/>
            <ObjectDataProvider MethodName="GetElapsedTime" x:Key="keyElapsed"
                                ObjectInstance="{StaticResource keyMyClock}"/>
            <ObjectDataProvider MethodName="SetStart" x:Key="keyStart"
                                ObjectInstance="{StaticResource keyMyClock}"/>
        </Window.Resources>
        <Grid>
            <StackPanel>
                <TextBlock FontSize="18"
                Text="{Binding Source={StaticResource keyElapsed}}"/>
                <Button x:Name="B_start" Content="Start"
                        Click="B_start_Click"/>
            </StackPanel>
        </Grid>
    </Window>
    

    좋은 웹페이지 즐겨찾기