8,736
社区成员
发帖
与我相关
我的任务
分享
private void RefreshWindow(StorageLocation enumOP)
{
//这个函数是执行复杂的串口通讯 得到的一个list 结果, 如何在这里 添加一个进度条啊?
this.list = this.currentSC.GetAllInformation();
// 绑定数据到 datagrid 中
this.Select(enumOP, string.Empty);
}
public class SCAssociate : INotifyPropertyChanged
{
private double finished;
public double Finished
{
get
{
return this.finished;
}
private set
{
this.finished = value;
NotifyPropertyChanged("Finished");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public List<ATCAssociate> GetAllInformation()
{
for(int i = 0; i < 50; i++)
{
this.Finished++;
this.Dosomething(i);
}
......
}
///获取数据的
public void XXXX()
{
//this.currentSC是上面业务类的实例
W_PBar win = new W_PBar("正在加载联系人数据请稍候......", this.currentSC);
win.Show();
try
{
this.list = win.GetAssociateList();
}
catch
{
win.Close();
}
}
public List<ATCAssociate> GetAssociateList()
{
if (this.currentSCBase is SCAssociate)
{
sc.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(sc_PropertyChanged);
return sc.GetAllInformation();
}
return null;
}
double value = 50;
void sc_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
///每当 finished +1的时候,就自动更新了UI了, 当然这里是简单的例子,具体的需要重新编写代码
///更新ui 需要用 Dispatcher.Invoke
pb_Main.Dispatcher.Invoke(new Action<DependencyProperty, object>(pb_Main.SetValue), DispatcherPriority.Background, ProgressBar.ValueProperty, value);
value++;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Window2 win = new Window2();
win.ShowDialog();
}
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
}
// 实例化业务类
SC currentSC = new SC();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Window1 win = new Window1();
win.DataContext = currentSC;
currentSC.DoSomething();
win.ShowDialog();
}
}
<Grid>
<ProgressBar Value="{Binding Finished}" Height="24" HorizontalAlignment="Left" Margin="12,12,0,0" Name="pb" VerticalAlignment="Top" Width="144" />
<TextBlock Text="{Binding Finished}" Height="23" HorizontalAlignment="Left" Margin="12,51,0,0" Name="txtProgress" VerticalAlignment="Top" />
</Grid>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
using System.ComponentModel;
public class SC : INotifyPropertyChanged
{
private double finished;
public double Finished
{
get { return finished; }
set
{
finished = value;
NotifyPropertyChanged("Finished");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public void DoSomething()
{
System.Threading.Thread th = new System.Threading.Thread(() => {
while (this.Finished < 100)
{
Finished++;
// 模拟业务,停止1秒
System.Threading.Thread.Sleep(1000);
}
});
th.Start();
}
}
public class SC:INotifyPropertyChanged {
private double finished;
public double Finished{
get { return finished; }
set
{
finished=value;
NotifyPropertyChanged("Finished");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
<ProgressBar Name="pb" Value={Binding Finished ,Mode=OneWay} />
this.pb.DataContext = this.currentSC;