C# WPF 加载进度显示
By
jerryxjr1220
at 2023-11-10 • 0人收藏 • 376人看过
<hc:GlowWindow x:Class="WPFAutomation.Views.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:model="clr-namespace:WPFAutomation.Models" xmlns:vm="clr-namespace:WPFAutomation.ViewModels" xmlns:view="clr-namespace:WPFAutomation.Views" xmlns:i="http://schemas.microsoft.com/xaml/behaviors" xmlns:local="clr-namespace:WPFAutomation" xmlns:hc="https://handyorg.github.io/handycontrol" d:DataContext="{d:DesignInstance Type=vm:MainViewModel}" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800" FontFamily="JetBrains Mono" FontSize="{StaticResource TextFontSize}"> <Window.Resources> <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" /> </Window.Resources> <hc:SimplePanel> <ScrollViewer x:Name="scrollViewer" VerticalScrollBarVisibility="Auto"> <StackPanel> <Button Content="Load" Click="ButtonBase_OnClick" Style="{StaticResource ButtonPrimary}"/> <hc:ImageBlock Height="400" Width="400" x:Name="ImageBlock"/> </StackPanel> </ScrollViewer> <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Visibility="{Binding IsLoading, Converter={StaticResource BooleanToVisibilityConverter}}" > <hc:LoadingCircle DotDiameter="10" Height="100" Width="100" /> <TextBlock Text="{Binding LoadingProgress, StringFormat=Loading...{0}%}" FontSize="20" Style="{StaticResource TextBlockDefaultPrimary}"/> </StackPanel> </hc:SimplePanel> </hc:GlowWindow>
using CommunityToolkit.Mvvm.ComponentModel; using WPFAutomation.Models; namespace WPFAutomation.ViewModels; public partial class MainViewModel : ObservableObject { [ObservableProperty] private bool _isLoading; [ObservableProperty] private int _loadingProgress; public MainViewModel() { IsLoading = false; LoadingProgress = 0; } }
using System; using System.Windows; using System.Windows.Media.Imaging; using System.Windows.Threading; using HandyControl.Controls; using WPFAutomation.ViewModels; namespace WPFAutomation.Views; /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : GlowWindow { private readonly MainViewModel mainViewModel; public MainWindow() { InitializeComponent(); mainViewModel = new MainViewModel(); DataContext = mainViewModel; } private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { mainViewModel.LoadingProgress = 0; var timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromMilliseconds(50); timer.Tick += (o, args) => { mainViewModel.LoadingProgress++; if (mainViewModel.LoadingProgress >= 100) { timer.Stop(); mainViewModel.IsLoading = false; ImageBlock.Source = new BitmapImage(new Uri( "D:\\Programming\\Rider Projects\\WPFAutomation\\WPFAutomation\\bin\\Debug\\net7.0-windows\\h42.png")); } }; mainViewModel.IsLoading = true; timer.Start(); } }
1 个回复 | 最后更新于 2023-11-10
登录后方可回帖
还有任务栏进度