ProgressBar未指示设置值



我有一个按钮,它最终会打开一个显示多个按钮的新窗口。当这个窗口正在加载并且加载它的标准正在确定时,我想显示一个进度条。

private void Select_Button_Click(object sender, RoutedEventArgs e)
{
Loading_Window progress = new Loading_Window(20);
progress.Show();
//BackgroundWorker worker = new BackgroundWorker();
//worker.WorkerReportsProgress = true;
//worker.DoWork += DoWork;
//worker.ProgressChanged += progressChanged;
//worker.RunWorkerCompleted += workerCompleted;
//worker.RunWorkerAsync();
try
{
if (Requests_listbox.SelectedIndex != -1)
{
if (Requests_listbox.SelectedItems.Count > 3)
{
Popup Error = new Popup("message");
Error.ShowDialog();
Requests_listbox.SelectedItems.Clear();
}
else
{
progress.Update_Progress(40);
foreach (Class1 jargon in Requests_listbox.SelectedItems)
{
Selection.Add(jargon);
}
progress.Update_Progress(80);
new_window my_new_window = new new_window(Selection, Requests_listbox.SelectedItems.Count) { Owner = this };
my_new_Window.ShowDialog();
FillChart();
}
}
else
{
Popup Error = new Popup("message");
Error.Show();
FillChart();
}
}
catch (Exception ex) { MessageBox.Show(ex.ToString()); }
}

并且我的Loading_Window类如下:

public partial class Loading_Window : Window
{
public Loading_Window(int _progress)
{
InitializeComponent();
Loading_progress.Value = _progress;
}
public void Update_Progress(int updated_progress)
{
Loading_progress.Value = updated_progress;
Loading_progress.UpdateLayout();
}
}

我多次尝试使用后台工作者,但都无法成功实现。。。我不知道我是不是错过了一些简单的东西,或者是什么情况。

如有任何建议,不胜感激。。

编辑:

我添加了以下按钮以简化操作,但在实现INotifyPropertyChanged时遇到了问题。。。

private void Test_Click(object sender, RoutedEventArgs e) //"Log Out" button
{
Loading_Window test = new Loading_Window(0);
test.Show();
test.updated_progress = 10;
test.updated_progress = 20;
test.updated_progress = 30;
test.updated_progress = 40;
test.updated_progress = 50;
test.updated_progress = 60;
test.updated_progress = 70;
test.updated_progress = 80;
test.updated_progress = 90;
test.updated_progress = 100;
test.Close();
}

public partial class Loading_Window : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public Loading_Window(double _progress)
{
InitializeComponent();
Loading_progress.Value = _progress;
}
public double updated_progress
{
get
{
return Loading_progress.Value;
}
set
{
Loading_progress.Value = value;
OnPropertyChanged("updated_progress");
}
}
protected virtual void OnPropertyChanged(string property_name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(property_name));
}
}
}

}

<Window x:Class="jawn.Loading_Window"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStyle="None" Background="Black"
WindowStartupLocation="CenterScreen"
Title="Loading_Window" Height="200" Width="600">
<Grid>
<StackPanel Grid.Column="0" Margin="10,0,0,0" VerticalAlignment="Center">
<TextBlock Text="Please wait...Operation in progress..." FontSize="25" Foreground="Wheat"/>
<ProgressBar x:Name="Loading_progress" Value="{Binding updated_progress}" Height="20" HorizontalAlignment="Left" VerticalAlignment="Center"  Width="572"/>
</StackPanel>
</Grid>

您遇到的具体问题是什么?我不建议在这里尝试在后台线程上做任何事情,因为您将从不同的线程访问UI元素,这不仅是一种糟糕的做法,而且是危险的(如果允许而不抛出异常)。

虽然我更喜欢通过MVVM做这样的事情,并在同一个窗口上覆盖一个进度指示器(或者在现有控件上放置一个深色半透明层,以获得更高级的方法),但我认为根据您提供的代码,最快的方法是:

Loading_Window progress = new Loading_Window(20);
try
{
progress.Show();
if (Requests_listbox.SelectedIndex != -1)
{
if (Requests_listbox.SelectedItems.Count > 3)
{
progress.Close();
Popup Error = new Popup("message");
Error.ShowDialog();
Requests_listbox.SelectedItems.Clear();
}
else
{
progress.Update_Progress(40);
foreach (Class1 jargon in Requests_listbox.SelectedItems)
{
Selection.Add(Selected_item);
}
progress.Update_Progress(80);
new_window my_new_window = new new_window(Selection, Requests_listbox.SelectedItems.Count) { Owner = this };
progress.Close();
my_new_Window.ShowDialog();
FillChart();
}
}
else
{
progress.Close();
Popup Error = new Popup("message");
Error.Show();
FillChart();
}
}
catch (Exception ex) { MessageBox.Show(ex.ToString()); }

我猜你的主要问题是进度条没有在你想要的时候消失,这将是预期的行为,因为我看不到任何关闭它的代码,而且你必须在对ShowDialog()进行阻塞模式对话框调用之前关闭它。

编辑:

您的主要问题是在XAML中绑定进度条的值,同时试图在代码中设置它,这将失败。由于您没有绑定到ViewModel/DataContext,因此不要在XAML中设置任何绑定,而是让代码保持原样,直接设置控件的Value。

您可能遇到的第二个问题是,您可能在窗口实际显示之前修改ProgressBar上的值,这不一定是坏事,但可能会导致窗口显示为非零初始值。你可以做一些类似的事情:

Loading_Window test = new Loading_Window(0);
test.Loaded += delegate
{
// Perform progress status update logic here
}

最新更新