WPF StatusBar update periodically via System.Timers.Timer

//xaml
<Window x:Class="WpfApp406.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:WpfApp406"
        mc:Ignorable="d"
        WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ToolBar RenderOptions.BitmapScalingMode="NearestNeighbor" Height="100" VerticalAlignment="Top"
                 ToolBar.OverflowMode="Always">
            <Button>
                <Image Source="/Images/1.jpg"/>
            </Button>
            <Separator/>
            <ToggleButton>
                <Image Source="/Images/2.jpg"/>
            </ToggleButton>
            <ToggleButton>
                <Image Source="/Images/3.jpg"/>
            </ToggleButton>
            <ToggleButton>
                <Image Source="/Images/4.jpg"/>
            </ToggleButton>
            <Separator/>
            <ToggleButton>
                <Image Source="/Images/5.jpg"/>
            </ToggleButton>
            <ToggleButton>
                <Image Source="/Images/6.jpg"/>
            </ToggleButton>
            <ToggleButton>
                <Image Source="/Images/7.jpg"/>
            </ToggleButton>
            <Separator/>
            <Label>Zoom</Label>
            <ComboBox>
                
            </ComboBox>
            <Separator/>
            <Button>
                <Image Source="/Images/8.jpg"/>
            </Button>
            <Button>
                <Image Source="/Images/9.jpg"/>
            </Button>
        </ToolBar>
        <StatusBar Height="50" VerticalAlignment="Bottom">
            <TextBlock x:Name="tbk" Width="1000" FontSize="30"/>
            <Separator/>
            <Image Source="/Images/10.jpg"/>
        </StatusBar>
    </Grid>
</Window>


//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 WpfApp406
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            System.Timers.Timer tmr = new System.Timers.Timer();
            tmr.Elapsed += Tmr_Elapsed;
            tmr.Interval = 1000;
            tmr.Start();
        }

        int idx = 0;
        private void Tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Dispatcher.BeginInvoke(new Action(() =>
            {
                tbk.Text = $"{++idx},{Guid.NewGuid().ToString("N")},{DateTime.Now.ToString("yyyyMMddHHmmssffff")}";
            }));
        }
    }
}

 

 

 

 

 

 

posted @ 2024-09-23 18:12  FredGrit  阅读(3)  评论(0编辑  收藏  举报