【问题标题】:How to show ProgressBar when method is call调用方法时如何显示 ProgressBar
【发布时间】:2014-06-04 08:27:38
【问题描述】:

我想在调用方法时显示一个 ProgressBar,所以我尝试执行此代码:

    if (parametro.chiave == "VIDEO")
    {
       //BISOGNA CARICARE IL VIDEO
       String urlVideo=caricaVideoOnline(parametro.valore);
       ....
    }

    String caricaVideoOnline(string nomeFileVideo)
    {
       try
       {
          libreriaYouTube = new UploadVideo(ConfigurationManager.AppSettings["usernameYoutube"],
                        ConfigurationManager.AppSettings["passwordYouTube"],
                        ConfigurationManager.AppSettings["developmentKeyYouTube"],
                        ConfigurationManager.AppSettings["applicationNameYouTube"]);

         libreriaYouTube.listaVideoToUpload.Add(nomeFileVideo);
         // String video = libreriaYouTube.caricaVideo();
         Thread t = new Thread(delegate()
         {
            for (int i = 0; i < 100000; i++)
            {
               if (i == 0)
               {
                  ProgressBar progress = new ProgressBar();
                  progress.Show();
               }
               Console.WriteLine(i);
            }
         });
         t.SetApartmentState(ApartmentState.STA);
         t.Start();
         String video= libreriaYouTube.caricaVideo();
         video = libreriaYouTube.caricaVideo();
         return video;
      }
      catch (Exception e)
      {
         log.Error(e);
         return null;
      }
   }

找到此代码,但我显示 ProgressBar 但它已锁定,我看不到进度条运行。

这是ProgressBar.xaml的代码

<Window x:Class="Gestore.ProgressBar"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Caricamento video" Height="100" Width="300"
        >
    <Grid Margin="20">
        <ProgressBar Minimum="0" Maximum="100" Name="pbStatus" IsIndeterminate="True" Tag="Attendere....."/>
        <Viewbox>
            <TextBlock Text='Attendere ...' FontSize="2" x:Name="textVox"/>
        </Viewbox>
    </Grid>
</Window>

【问题讨论】:

标签: c# wpf multithreading progress-bar


【解决方案1】:

我发现您的代码存在一些问题:

  1. 您应该访问 pbStatus,而不是在代码中创建新的进度条
  2. 您永远不会更新进度条的值 ("pbStatus.Value=...")
  3. 您无法从线程 t 更新进度条。您需要调用应用程序的 UI 线程来执行此操作
  4. 循环立即运行。插入一些 sleep 语句来控制其计时行为。

以下示例代码应该可以工作。

Xaml:

<StackPanel Orientation="Vertical">
    <ProgressBar Name="pbStatus" Minimum="0" Maximum="100" Height="20"></ProgressBar>
    <Button Click="ButtonBase_OnClick">Press me</Button>
</StackPanel>

后面的代码:

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        Thread t = new Thread(delegate()
        {
            for (int i = 0; i < 100; i++)
            {
                this.Dispatcher.Invoke(()=>
                {
                    pbStatus.Value = i;
                }); 
                Thread.Sleep(500);
            }
        });
        t.Start();
    }

【讨论】:

  • 我无法在 myPb 中声明,因为应用程序运行但它不可见,所以我声明此 ProgressBar p = new ProgressBar()... 我尝试将此代码用于 (int i = 0; i
  • “我不能在 myPb 中声明”是什么意思?您可以在 XAML 或代码中创建进度条。如果你两者都做,你最终会得到两个进度条,一个是可见的,另一个是不可见的。您必须决定两种方式中的一种。如果您想使用“ProgressBar p = new ...”在代码中创建进度条,则需要通过将其添加到任何可见的 UI 容器控件来将其添加到用户界面,例如“myGrid.Children.Add(p) ;"。 “p.Show()”在这里不起作用。我的建议:像你已经做的那样在 XAML 中创建 pbStatus,然后使用那个。不要在代码中创建新的。
猜你喜欢
  • 2015-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多