C# 当类中的属性发生更改时,如何接收表单中的属性更改通知?
我有一个具有INotifyPropertyChanged接口的类。有一个名为Total Progress的属性 我有一张有进度条的表格。我想将TotalProgress属性更改通知发送到此进度条并设置其值 我是否也需要捕获表单中的PropertyChangedEvent 编辑:WPF表单代码C# 当类中的属性发生更改时,如何接收表单中的属性更改通知?,c#,.net,wpf,.net-4.0,inotifypropertychanged,C#,.net,Wpf,.net 4.0,Inotifypropertychanged,我有一个具有INotifyPropertyChanged接口的类。有一个名为Total Progress的属性 我有一张有进度条的表格。我想将TotalProgress属性更改通知发送到此进度条并设置其值 我是否也需要捕获表单中的PropertyChangedEvent 编辑:WPF表单代码 using System.ComponentModel; using System.Windows; using System.Windows.Data; using System; using Syste
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
using System;
using System.Windows.Threading;
namespace SUpdater
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
BackgroundWorker bw = new BackgroundWorker();
DownloadFile FileDownloadClass = new DownloadFile();
public MainWindow()
{
InitializeComponent();
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
progressBar1.SetBinding(System.Windows.Controls.ProgressBar.ValueProperty, new Binding("TotalPercentCompleted"));
progressBar1.DataContext = FileDownloadClass;
FileDownloadClass.PropertyChanged +=new PropertyChangedEventHandler(FileDownloadClass_PropertyChanged);
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
FileDownloadClass.DownloadFiles();
if ((bw.CancellationPending == true))
e.Cancel = true;
else
{
bw.ReportProgress(FileDownloadClass.TotalPercentCompleted);
}
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if ((e.Cancelled == true))
{
this.lblConnectionStatus.Content = " Download Canceled!";
}
else if (!(e.Error == null))
{
this.lblConnectionStatus.Content = ("Error: " + e.Error.Message);
}
else
{
this.lblConnectionStatus.Content = "Done!";
}
}
private void FileDownloadClass_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
}
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
lblKbCompleted.Content = e.ProgressPercentage.ToString();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
bw.RunWorkerAsync();
}
}
}
sealed class DownloadFile:INotifyPropertyChanged
{
#region Private Fields
// These fields hold the values for the public properties.
private int progressBarValue = 0;
private int totalKbCompleted = 0;
private int totalBytesReceived = 0;
private int remoteFileSize = 0;
private string fileName = String.Empty;
private string statusMessage = String.Empty;
#endregion
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#region Public Properties
public int TotalKbCompleted
{
get { return this.totalKbCompleted; }
set
{
if (value != this.totalKbCompleted)
{
this.totalKbCompleted = value/1024;
NotifyPropertyChanged("TotalKbCompleted");
}
}
}
public int TotalBytesReceived
{
get { return this.totalBytesReceived; }
set
{
if (value != this.totalBytesReceived)
{
this.totalBytesReceived = value;
NotifyPropertyChanged("TotalBytesReceived");
}
}
}
public int RemoteFileSize
{
get { return this.remoteFileSize; }
set
{
if (value != this.remoteFileSize)
{
this.remoteFileSize = value;
NotifyPropertyChanged("RemoteFileSize");
}
}
}
public string CurrentFileName
{
get { return this.fileName; }
set
{
if (value != this.fileName)
{
this.fileName = value;
NotifyPropertyChanged("CurrentFileName");
}
}
}
public string StatusMessage
{
get { return this.statusMessage; }
set
{
if (value != this.statusMessage)
{
this.statusMessage = value;
NotifyPropertyChanged("StatusMessage");
}
}
}
#endregion
public Int16 DownloadFiles()
{
try
{
statusMessage = "Attempting Connection with Server";
DoEvents();
// create a new ftpclient object with the host and port number to use
FtpClient ftp = new FtpClient("mySite", 21);
// registered an event hook for the transfer complete event so we get an update when the transfer is over
//ftp.TransferComplete += new EventHandler<TransferCompleteEventArgs>(ftp_TransferComplete);
// open a connection to the ftp server with a username and password
statusMessage = "Connected. Authenticating ....";
ftp.Open("User Name", "Password");
// Determine File Size of the compressed file to download
statusMessage = "Getting File Details";
RemoteFileSize = Convert.ToInt32(ftp.GetFileSize("myFile.exe"));
ftp.TransferProgress += new EventHandler<TransferProgressEventArgs>(ftp_TransferProgress);
statusMessage = "Download from Server";
ftp.GetFile("myFile.exe", "E:\\Test\\myFile.exe", FileAction.Create);
// close the ftp connection
ftp.Close();
statusMessage = "Download Complete";
return 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return 0;
}
}
private void ftp_TransferProgress(object sender, TransferProgressEventArgs e)
{
totalBytesReceived = Convert.ToInt32(e.BytesTransferred.ToString());
totalKbCompleted = Convert.ToInt32(totalKbCompleted + Convert.ToInt32(totalBytesReceived));
progressBarValue = totalKbCompleted;
}
}
使用System.ComponentModel;
使用System.Windows;
使用System.Windows.Data;
使用制度;
使用System.Windows.Threading;
命名空间支持程序
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
BackgroundWorker bw=新的BackgroundWorker();
DownloadFile FileDownloadClass=新的DownloadFile();
公共主窗口()
{
初始化组件();
bw.WorkerReportsProgress=true;
bw.workersupport扫描单元=真;
bw.DoWork+=新DoWorkEventHandler(bw_DoWork);
bw.ProgressChanged+=新的ProgressChangedEventHandler(bw\U ProgressChanged);
bw.RunWorkerCompleted+=新的RunWorkerCompletedEventHandler(bw\u RunWorkerCompleted);
progressBar1.SetBinding(System.Windows.Controls.ProgressBar.ValueProperty,新绑定(“TotalPercentCompleted”);
progressBar1.DataContext=FileDownloadClass;
FileDownloadClass.PropertyChanged+=新的PropertyChangedEventHandler(FileDownloadClass\u PropertyChanged);
}
私有void bw_DoWork(对象发送方,DoWorkEventArgs e)
{
FileDownloadClass.DownloadFiles();
if((bw.CancellationPending==true))
e、 取消=真;
其他的
{
ReportProgress(FileDownloadClass.TotalPercentCompleted);
}
}
私有void bw_RunWorkerCompleted(对象发送方,RunWorkerCompletedEventArgs e)
{
如果((e.Cancelled==true))
{
this.lblConnectionStatus.Content=“下载已取消!”;
}
else如果(!(e.Error==null))
{
this.lblConnectionStatus.Content=(“错误:+e.Error.Message”);
}
其他的
{
this.lblConnectionStatus.Content=“完成!”;
}
}
private void FileDownloadClass_PropertyChanged(对象发送方,PropertyChangedEventArgs e)
{
}
私有void bw_ProgressChanged(对象发送方,ProgressChangedEventArgs e)
{
lblKbCompleted.Content=e.ProgressPercentage.ToString();
}
已加载私有无效窗口(对象发送器、路由目标)
{
RunWorkerAsync();
}
}
}
编辑:下载文件类代码
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
using System;
using System.Windows.Threading;
namespace SUpdater
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
BackgroundWorker bw = new BackgroundWorker();
DownloadFile FileDownloadClass = new DownloadFile();
public MainWindow()
{
InitializeComponent();
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
progressBar1.SetBinding(System.Windows.Controls.ProgressBar.ValueProperty, new Binding("TotalPercentCompleted"));
progressBar1.DataContext = FileDownloadClass;
FileDownloadClass.PropertyChanged +=new PropertyChangedEventHandler(FileDownloadClass_PropertyChanged);
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
FileDownloadClass.DownloadFiles();
if ((bw.CancellationPending == true))
e.Cancel = true;
else
{
bw.ReportProgress(FileDownloadClass.TotalPercentCompleted);
}
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if ((e.Cancelled == true))
{
this.lblConnectionStatus.Content = " Download Canceled!";
}
else if (!(e.Error == null))
{
this.lblConnectionStatus.Content = ("Error: " + e.Error.Message);
}
else
{
this.lblConnectionStatus.Content = "Done!";
}
}
private void FileDownloadClass_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
}
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
lblKbCompleted.Content = e.ProgressPercentage.ToString();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
bw.RunWorkerAsync();
}
}
}
sealed class DownloadFile:INotifyPropertyChanged
{
#region Private Fields
// These fields hold the values for the public properties.
private int progressBarValue = 0;
private int totalKbCompleted = 0;
private int totalBytesReceived = 0;
private int remoteFileSize = 0;
private string fileName = String.Empty;
private string statusMessage = String.Empty;
#endregion
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#region Public Properties
public int TotalKbCompleted
{
get { return this.totalKbCompleted; }
set
{
if (value != this.totalKbCompleted)
{
this.totalKbCompleted = value/1024;
NotifyPropertyChanged("TotalKbCompleted");
}
}
}
public int TotalBytesReceived
{
get { return this.totalBytesReceived; }
set
{
if (value != this.totalBytesReceived)
{
this.totalBytesReceived = value;
NotifyPropertyChanged("TotalBytesReceived");
}
}
}
public int RemoteFileSize
{
get { return this.remoteFileSize; }
set
{
if (value != this.remoteFileSize)
{
this.remoteFileSize = value;
NotifyPropertyChanged("RemoteFileSize");
}
}
}
public string CurrentFileName
{
get { return this.fileName; }
set
{
if (value != this.fileName)
{
this.fileName = value;
NotifyPropertyChanged("CurrentFileName");
}
}
}
public string StatusMessage
{
get { return this.statusMessage; }
set
{
if (value != this.statusMessage)
{
this.statusMessage = value;
NotifyPropertyChanged("StatusMessage");
}
}
}
#endregion
public Int16 DownloadFiles()
{
try
{
statusMessage = "Attempting Connection with Server";
DoEvents();
// create a new ftpclient object with the host and port number to use
FtpClient ftp = new FtpClient("mySite", 21);
// registered an event hook for the transfer complete event so we get an update when the transfer is over
//ftp.TransferComplete += new EventHandler<TransferCompleteEventArgs>(ftp_TransferComplete);
// open a connection to the ftp server with a username and password
statusMessage = "Connected. Authenticating ....";
ftp.Open("User Name", "Password");
// Determine File Size of the compressed file to download
statusMessage = "Getting File Details";
RemoteFileSize = Convert.ToInt32(ftp.GetFileSize("myFile.exe"));
ftp.TransferProgress += new EventHandler<TransferProgressEventArgs>(ftp_TransferProgress);
statusMessage = "Download from Server";
ftp.GetFile("myFile.exe", "E:\\Test\\myFile.exe", FileAction.Create);
// close the ftp connection
ftp.Close();
statusMessage = "Download Complete";
return 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return 0;
}
}
private void ftp_TransferProgress(object sender, TransferProgressEventArgs e)
{
totalBytesReceived = Convert.ToInt32(e.BytesTransferred.ToString());
totalKbCompleted = Convert.ToInt32(totalKbCompleted + Convert.ToInt32(totalBytesReceived));
progressBarValue = totalKbCompleted;
}
}
密封类下载文件:INotifyPropertyChanged
{
#区域专用字段
//这些字段包含公共属性的值。
private int progressBarValue=0;
私有整数totalKbCompleted=0;
私有int totalBytesReceived=0;
私有int-remoteFileSize=0;
私有字符串文件名=string.Empty;
私有字符串statusMessage=string.Empty;
#端区
公共事件属性更改事件处理程序属性更改;
私有void NotifyPropertyChanged(字符串信息)
{
if(PropertyChanged!=null)
{
PropertyChanged(此,新PropertyChangedEventArgs(信息));
}
}
#区域公共财产
公共整数总计已完成
{
获取{返回this.totalKbCompleted;}
设置
{
如果(值!=此.totalKbCompleted)
{
this.totalKbCompleted=值/1024;
NotifyPropertyChanged(“TotalKbCompleted”);
}
}
}
收到的公共整数总计字节数
{
获取{返回this.totalBytesReceived;}
设置
{
if(值!=此.totalBytesReceived)
{
this.totalBytesReceived=值;
NotifyPropertyChanged(“TotalBytesReceived”);
}
}
}
公共int远程文件大小
{
获取{返回this.remoteFileSize;}
设置
{
if(值!=此.remoteFileSize)
{
this.remoteFileSize=值;
NotifyPropertyChanged(“RemoteFileSize”);
}
}
}
公共字符串CurrentFileName
{
获取{返回this.fileName;}
设置
{
if(值!=此.fileName)
{
this.fileName=值;
NotifyPropertyChanged(“当前文件名”);
}
}
}
公共字符串状态消息
{
获取{返回this.statusMessage;}
设置
{
if(值!=此.statusMessage)
{
this.statusMessage=值;
NotifyPropertyChanged(“StatusMessage”);
}
}
}
#端区
公共Int16下载文件()
{
尝试
{
statusMessage=“正在尝试与服务器连接”;
DoEvents();
//使用要使用的主机和端口号创建新的ftpclient对象
Foo _foo = new Foo();
DispatcherTimer _dispatcherTimer = new DispatcherTimer();
public MainWindow()
{
InitializeComponent();
_dispatcherTimer.Interval = TimeSpan.FromSeconds(1);
_dispatcherTimer.Tick += _dispatcherTimer_Tick;
_dispatcherTimer.Start();
progressBar1.SetBinding(ProgressBar.ValueProperty, new Binding("ProgressTotal"));
progressBar1.DataContext = _foo;
}
private void _dispatcherTimer_Tick(object sender, EventArgs e)
{
_foo.ProgressTotal = (_foo.ProgressTotal + 10) % progressBar1.Maximum;
}
public class Foo : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private double _progressTotal = 0;
public double ProgressTotal
{
get { return _progressTotal; }
set
{
if (value != _progressTotal)
{
_progressTotal = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("ProgressTotal"));
}
}
}
}
}
progressBar1.SetBinding(System.Windows.Controls.ProgressBar.ValueProperty, new Binding("TotalKbCompleted"));