在 WPF 中使用调度程序加载用户控件

发布于 2024-10-26 09:17:47 字数 1186 浏览 0 评论 0原文

我有一个用户控件,其中有一个包含大约 2900 个项目的网格,对此我无能为力,因为这就是业务想要的方式...显然,这加载/渲染速度很慢,所以我使用以下方法创建了这个技巧调度程序,在处理事件的视图模型中(棱镜事件...不是标准的 Windows 事件)。

 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();
}

它工作得很好,但是在我的 SampleView (目前被称为)上有一个不确定的进度条,但是它永远不会更新,就像你知道的那样 - 绿色的 bara 返回第四个......这是它的 XAML 。

 <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>

是否与调度程序不让其更新有关?

有人做过这样的事吗?有什么建议吗?

谢谢!

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 bar 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如何视而不见 2024-11-02 09:17:47

我的猜测是调度程序线程正忙于尝试呈现您的控件并且无法更新进度栏。

弹出窗口是否有响应?尝试移动窗口或添加按钮,看看是否可以单击该按钮。这可能有助于确定是否是进度条的简单问题或调度程序太忙。

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.

微凉徒眸意 2024-11-02 09:17:47

实际上我最终做的是在视图上调用container.resolve,我在视图模型上调用container.resolve,这可以通过标准后台工作人员完成 - 然后在主线程上的RunWorkerCompleted中发生银行我创建视图传递我们在后台愉快等待的视图模型,并关闭等待屏幕,这当然不会受到我们长时间运行的进程的影响。这是代码。

 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();

希望这对某人有用...总体想法是在不同的线程上创建 ViewModel 因为它需要永远加载等等...

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文