#author("2016-12-17T13:36:20+09:00","default:wikiwriter","wikiwriter")
#author("2016-12-17T13:43:33+09:00","default:wikiwriter","wikiwriter")
[[WPF]]

&tag(WPF/プログレスバー);
*目次 [#u99e820f]
#contents
*関連ページ [#q1db7793]
*参考情報 [#iaa9c05a]


*プログレスバー付きのダイアログを実装 [#kf9e1ecd]
-独立したウィンドウに開始、終了ボタンを追加し、開始ボタンを押したらプログレスバーが開始するサンプルを作成すうr。
-xamlファイル。
#pre{{
<Window x:Class="ControlDemo.DialogDemo.ProgressDialogBasic.ProgressDialogBasicWindow"
        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:ControlDemo.DialogDemo.ProgressDialogBasic"
        mc:Ignorable="d"
        Loaded="UserControl_Loaded"
        Title="プログレスバー付きのダイアログ" Height="140" Width="300">
        Title="プログレスバー付きのダイアログ" Height="140" Width="400">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <ProgressBar x:Name="progressBar" Height="25" Margin="10" Minimum="0" Maximum="100" Value="50"/>
        <StackPanel Grid.Row="1" Orientation="Horizontal" Margin="10">
            <Button x:Name="buttonStart" Width="100" Click="Button_Click">開始</Button>
            <Button x:Name="buttonFinish" Width="100" Margin="10,0,0,0" Click="Button_Click_1" IsEnabled="False">終了</Button>
            <Button x:Name="buttonStart" Width="100" Click="ButtonStart_Click">開始</Button>
            <Button x:Name="buttonChange" Width="100" Click="ButtonChange_Click">変更</Button>
            <Button x:Name="buttonFinish" Width="100" Margin="10,0,0,0" Click="ButtonFinish_Click" IsEnabled="False">終了</Button>
        </StackPanel>
    </Grid>
</Window>

}}
-対応するクラス。
#pre{{
namespace ControlDemo.DialogDemo.ProgressDialogBasic
{
    /// <summary>
    /// ProgressDialogBasicWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class ProgressDialogBasicWindow : Window
    {
        private BackgroundWorker backgroundWorker = new BackgroundWorker();
        private ProgressDialogBasicVM viewModel;
        public ProgressDialogBasicWindow()
        {
            InitializeComponent();
            this.viewModel = new ProgressDialogBasicVM();
            DataContext = viewModel;

            //BackgroundWorkerのセットアップ
            //進捗状況を報告したい場合trueに設定
            backgroundWorker.WorkerReportsProgress = true;
            //途中でキャンセルしたい場合trueに設定
            backgroundWorker.WorkerSupportsCancellation = true;
            //プログレス値が変更した場合の処理
            backgroundWorker.ProgressChanged += ProgressChanged;
            //BackgroundWorkerで実行する処理
            backgroundWorker.DoWork += DoWork;
            // not required for this question, but is a helpful event to handle
            backgroundWorker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
            //終了時に呼び出される
            backgroundWorker.RunWorkerCompleted += RunWorkerCompleted;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 50; i <= 100; i++)
            {
                if (backgroundWorker.CancellationPending)
                {
                    //キャンセルしたい場合作業中に明示的にチェックを行う必要がある
                    e.Cancel = true;
                    return;
                }

                Thread.Sleep(200);
                backgroundWorker.ReportProgress(i);
            }
        }

        private void ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // This is called on the UI thread when ReportProgress method is called
            progressBar.Value = e.ProgressPercentage;
        }

        private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        private void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                MessageBox.Show("Cancel");
            } else
            {
                MessageBox.Show("Completed");
            }

            // This is called on the UI thread when the DoWork method completes
            // so it's a good place to hide busy indicators, or put clean up code
            buttonStart.IsEnabled = true;
            buttonFinish.IsEnabled = false;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        private void ButtonStart_Click(object sender, RoutedEventArgs e)
        {
            buttonStart.IsEnabled = false;
            buttonFinish.IsEnabled = true;
            backgroundWorker.RunWorkerAsync();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        private void ButtonFinish_Click(object sender, RoutedEventArgs e)
        {
            backgroundWorker.CancelAsync();

            buttonStart.IsEnabled = true;
            buttonFinish.IsEnabled = false;

        }
    }
}

}}
-DoWorkイベントハンドラを引数で渡したい場合。
#pre{{
        private void ButtonChange_Click(object buttonSender, RoutedEventArgs routedEvent)
        {
            DoWorkEventHandler handler = (sender, e) =>
            {
                for (int i = 50; i <= 100; i++)
                {
                    BackgroundWorker worker = sender as BackgroundWorker;
                    if (worker.CancellationPending)
                    {
                        e.Cancel = true;
                        return;
                    }

                    Thread.Sleep(2);
                    worker.ReportProgress(i);
                }
            };
            ChangeDoWork(handler);
        }

        private void ChangeDoWork(DoWorkEventHandler handler)
        {
            backgroundWorker.DoWork -= DoWork;
            backgroundWorker.DoWork += handler;
        }
}}

トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS