閲覧者向けに雑実装^^;
閉じるボタンは消していませんが、押しても閉じません。
コードからキャンセルとかアイコン表示とかいろいろあるでしょうが、わたしは使わないのでこれ以上凝る気はありません(ダイアログ嫌いなので^^;
xml
1<Window
2 x:Class="Questions374184.MainWindow"
3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5 Width="800"
6 Height="450">
7 <StackPanel>
8 <Button Content="TaskDialog" Click="Button_Click" />
9 <Button Content="ProgressDialog" Click="Button_Click_1" />
10 </StackPanel>
11</Window>
cs
1using System;
2using System.Threading;
3using System.Threading.Tasks;
4using System.Windows;
5using Microsoft.WindowsAPICodePack.Dialogs;
6
7namespace Questions374184
8{
9 public partial class MainWindow : Window
10 {
11 public MainWindow() => InitializeComponent();
12
13 private void Button_Click(object sender, RoutedEventArgs e)
14 {
15 var dialog = new TaskDialog
16 {
17 Caption = "Caption",
18 InstructionText = "InstructionText",
19 Text = "Text",
20 ProgressBar = new TaskDialogProgressBar(),
21 };
22
23 IProgress<int> progress = new Progress<int>(i =>
24 {
25 dialog.ProgressBar.Value = i;
26 if (100 <= i) dialog.Close();
27 });
28
29 Task.Run(() =>
30 {
31 for (var i = 0; i <= 100; i++)
32 {
33 Thread.Sleep(100);
34 progress.Report(i);
35 }
36 });
37
38 dialog.Show();
39 }
40
41 private void Button_Click_1(object sender, RoutedEventArgs e)
42 {
43 var dialog = new ProgressDialog
44 {
45 Caption = "Caption",
46 InstructionText = "InstructionText",
47 Text = "Text",
48 Owner = this,
49 };
50
51 Task.Run(() =>
52 {
53 for (var i = 0; i <= 100; i++)
54 {
55 Thread.Sleep(100);
56 dialog.Progress.Report(i);
57 }
58 });
59
60 dialog.ShowDialog();
61 }
62 }
63}
xml
1<Window
2 x:Class="Questions374184.ProgressDialog"
3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5 Title="ProgressDialog"
6 Width="380"
7 ResizeMode="NoResize"
8 SizeToContent="Height"
9 WindowStartupLocation="CenterOwner">
10 <StackPanel Margin="10">
11 <TextBlock
12 x:Name="instructionText"
13 Margin="5"
14 Foreground="#FF003399"
15 Text="InstructionText" />
16 <TextBlock
17 x:Name="text"
18 Margin="5"
19 Text="Text" />
20 <ProgressBar
21 x:Name="progressBar"
22 Height="20"
23 Margin="5" />
24 </StackPanel>
25</Window>
cs
1using System;
2using System.ComponentModel;
3using System.Threading.Tasks;
4using System.Windows;
5
6namespace Questions374184
7{
8 public partial class ProgressDialog : Window
9 {
10 public string Caption { get => Title; set => Title = value; }
11 public string InstructionText { get => instructionText.Text; set => instructionText.Text = value; }
12 public string Text { get => text.Text; set => text.Text = value; }
13 public IProgress<int> Progress { get; }
14
15 public ProgressDialog()
16 {
17 InitializeComponent();
18
19 Progress = new Progress<int>(i =>
20 {
21 progressBar.Value = i;
22 if (100 <= i) Close();
23 });
24 }
25
26 protected override void OnClosing(CancelEventArgs e)
27 {
28 e.Cancel = progressBar.Value < 100;
29 }
30 }
31}
対比のために入れただけなので、WindowsAPICodePackを入れる必要はないです。
わたしが使用したのは↓で、マニュフェストでコモンコントロールを有効にする必要があります。
NuGet Gallery | WindowsAPICodePack-Core 1.1.2
視覚スタイルを有効にする - Win32 apps | Microsoft Docs