2016-03-30 8 views
0

私は、プリズム6を使用する最初のWPFアプリケーションで作業しています。 ボタンをクリックすると、約1-2分かかる操作が開始されます。Prism 6(WPF)を使用して時間のかかる操作を表示する操作の進行レイヤー

Iは、ウィンドウ内のすべての他のコントロールの上に半透明層を表示し、操作の進行状況を表示し

enter image description here

<Window x:Class="TestingApplication.Shell" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:prism="http://www.codeplex.com/prism"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 

     <Border Grid.Column="0"> 
      <ContentControl x:Name="NavigationItemsControl" 
          prism:RegionManager.RegionName="MainNavigationRegion" 
          Grid.Column="0"/> 
     </Border> 

     <ContentControl prism:RegionManager.RegionName="MainContentRegion" 
         Grid.Row="1"/> 

     <Grid Grid.RowSpan="2" Background="#5555"> 
      <Border BorderBrush="Gray" BorderThickness="1" 
        Width="400" Height="200" Background="White"> 
       <StackPanel Margin="10"> 
        <TextBlock Text="Loading... Please wait" 
           Margin="0,10" FontSize="18"/> 
        <ProgressBar HorizontalAlignment="Stretch" Height="40"/> 
        <Button Content="Cancel" HorizontalAlignment="Center" 
          Margin="0,10"/> 
       </StackPanel> 
      </Border> 
     </Grid> 
    </Grid> 
</Window> 
下の画像とコードのように、ボタンをキャンセルしたいです

これを実装してプリズムとMVVMを活用するのがベストプラクティスとなる提案をいただけますか?

は、私がeventAggrgatorを使用することになり、 ナディア

+0

私は混乱しています。あなたが説明することができます、これはあなたが必要なものです: ボタンをクリックすると、プロセスが終了するまで灰色のレイアウトが表示されるはずですか? – safi

+0

はい、それは私が必要なものです – melculetz

答えて

2

、ありがとうございました。

<ProgressBar HorizontalAlignment="Stretch" Height="40" Value{Binding ProgressBarValue}/> 

public class ViewModel:BindableBase 
{ 
    private readonly IEventAggregator _eventAggregator; 

    public ViewModel(IEventAggregator eventAggregator) 
    { 
     _eventAggregator = eventAggregator; 
     _eventAggregator.GetEvent<ProgressbarEvent>().Subscribe(PopulateProgress); 
    } 

    private void PopulateProgress(double value) 
    { 
     ProgressbarValue = value; 
    } 

    private double _progressbarValue; 

    public double ProgressbarValue 
    { 
     get { return _progressbarValue; } 
     set { SetProperty(ref(_progressbarValue),value); } 
    } 

//this is how you would send event 
     public void SendProgress() 
     { 
      for (double i = 0; i < 100; i++) 
      { 
       _eventAggregator.GetEvent<ProgressbarEvent>().Publish(i); 
      } 
     } 

    } 
    public class ProgressbarEvent : PubSubEvent<Double> { } 

長い操作をしているときは別スレッドになっていることを確認してください。

+0

ありがとう!それは動作します – melculetz

0

BusyIndicatorExtendedWPFToolkitからお伝えします。

あなたのXAMLは次のようにする必要があります

<extToolkit:BusyIndicator IsBusy="{Binding IsBusy}" BusyContent="Processing..." > 
    <extToolkit:BusyIndicator.ProgressBarStyle> 
     <Style TargetType="ProgressBar"> 
     <Setter Property="IsIndeterminate" Value="False"/> 
     <Setter Property="Height" Value="15"/> 
     <Setter Property="Margin" Value="8,0,8,8"/> 
     <Setter Property="Value" Value="{Binding ProgressValue}"/> 
     </Style> 
    </extToolkit:BusyIndicator.ProgressBarStyle> 

    <ListBox ItemsSource="{Binding Persons}"/> 

</extToolkit:BusyIndicator> 

次に、あなたのViewModelにブールIsBusyプロパティを実装:

private bool isBusy; 
public bool IsBusy 
{ 
    get { return isBusy; } 
    set 
    { 
     this.isBusy = value; 
     this.OnPropertyChanged("IsBusy"); 
    } 
} 

は、その後、あなたのViewModelにProgressValueプロパティを追加します。

private int progressValue ; 
public int ProgressValue 
{ 
    get { return progressValue ; } 
    set 
    { 
     this.progressValue = value; 
     this.OnPropertyChanged("ProgressValue "); 
    } 
} 

その後に時間のかかる操作をに置くBackgroundWorkerクラスのラムダ式UIをフリーズしないようにするには:

BackgroundWorker worker = new BackgroundWorker(); 
    worker.WorkerReportsProgress = true;   
    worker.DoWork += (o, ea) => 
    { 
      IsBusy = true; 
      //Do your heavy work here 
      worker.ReportProgress(10); 

      //Do your heavy work here 
      worker.ReportProgress(20); 

      //And so on 
    }; 


    worker.ProgressChanged += (o,ea) => 
    { 
     ProgressValue = e.ProgressPercentage; 
    }; 

    worker.RunWorkerCompleted += (o, ea) => 
    { 
     IsBusy = false; 
    }; 
+0

@ JamTay317任意の質問をお気軽に。私の返事があなたに役立つと感じたら、私の返事を答えとしてマークして、将来の他の人の検索を簡単にすることができます。 http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-workをご覧ください。 – StepUp

関連する問題