我有一个按钮,最终会打开一个显示多个按钮的新窗口。 当这个窗口正在加载并且正在确定加载它的标准时,我想显示一个进度条。
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>
答案 0 :(得分:0)
你对此有什么具体问题?我不建议尝试在后台线程上做任何事情,因为你将从不同的线程访问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中设置任何绑定,而是将代码保留为直接设置控件的值。
您可能遇到的第二个问题是您可能在实际显示窗口之前修改ProgressBar上的值,这不一定是坏事但可能导致窗口显示非零初始值。你可以这样做:
Loading_Window test = new Loading_Window(0);
test.Loaded += delegate
{
// Perform progress status update logic here
}