开发者

using the dispatcher to load usercontrols in WPF

I have a user control that has a grid with like 2900 items in it, there is nothing I can do about this cause that is the way the business want's it... Obviously this is slow to load/render so I created this trick using the dispatcher, in the view model that handles the event (prism event... not a standard windows event).

 public void ShowPopUp(Type viewType)
{
  var waitScreen = new Controls.Views.SampleView();
  var popUp = new ShellBlank();
  popUp.Content = waitScreen;
  popUp.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
  popUp.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, new Action(delegate() { 
    popUp.Content = container.Resolve(viewType);})
  );
  popUp.ShowDialog();
}

It works just fine however on my SampleView (as it is called at the moment) there is an in-determinant progress 开发者_如何学Gobar however it never updates, like you know - the green bara going back and fourth... Here is the XAML for it.

 <Border>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Label Margin="12" FontSize="16" Foreground="WhiteSmoke" Content="Loading... Please wait"/>
        <ProgressBar Grid.Row="1" IsIndeterminate="True"  Width="280" Height="24"/>
    </Grid>
</Border>

Is it something to do with the dispatcher not letting it update?

Anyone ever done something like this? got any suggestions?

Thanks!


My guess is that the Dispatcher thread is busy trying to render your control and hasn't been able to update the ProgressBar.

Is the popup window responsive? Try moving the window or adding a button and seeing if you can click on the button. This might help identify whether its a simple problem with the progress bar or the Dispatcher being too busy.


Actually I ended up doing was calling container.resolve on the view, I called called container.resolve on the view model, this could be done with a standard background worker - then in the RunWorkerCompleted with happens bank on the main thread I create the view passing in the view model that we happily waited for in the background and switch out the wait screen which of course wasn't impacted by our long running process. Here is the code.

 AddVendorModalityViewModel viewModel;
    var waitScreen = new Controls.Views.SampleView();
    var popUp = new ShellBlank();
    popUp.Content = waitScreen;
    popUp.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;

    var bw = new BackgroundWorker() { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
    bw.DoWork += (s, e) =>
    {
      viewModel = container.Resolve<AddVendorModalityViewModel>();
      e.Result = viewModel;
    };
    bw.RunWorkerCompleted += (s, e) =>
    {
      viewModel = (AddVendorModalityViewModel)e.Result;
      AddVendorModalityView view = new AddVendorModalityView(viewModel);
      popUp.Content = view;
    };
    bw.RunWorkerAsync();
    popUp.ShowDialog();

Hope this is useful for someone... The general idea is create the ViewModel on a different thread cause it is the thing that will take forever to load etc...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜