3
我有這個類:Silverlight的跨線程UI更新問題
public class UploadFile : INotifyPropertyChanged {
private string name;
public string Name {
get {
return name;
}
set {
name = value;
OnPropertyChanged("Name");
}
}
private FileInfo fileInfo;
public FileInfo FileInfo { get; set; }
private string length;
public string Length {
get {
return length;
}
set {
length = value;
OnPropertyChanged("Length");
}
}
private int percentage;
public int Percentage {
get {
return percentage;
}
set {
percentage = value;
OnPropertyChanged("Percentage");
}
}
public string ProgressValue {
get { return string.Format("{0}%", Percentage); }
}
private string imageSource;
public string ImageSource {
get {
return imageSource;
}
set {
imageSource = value;
OnPropertyChanged("ImageSource");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
private void OnPropertyChanged(string property) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
我試圖創建一個文件的上傳是能夠大和多文件上傳。我正在使用Web服務來做這件事。我已經在我的用戶界面中進行了雙向綁定,現在我想要上傳文件。我使用此代碼
private void DoUpload() {
foreach (UploadFile file in fileInfos){
int BUFFERSIZE = 1024;
int offset = 0; //get from webservice, when partial file
FileStream s = file.FileInfo.OpenRead();
byte[] buffer = null;
long remainingBytes = s.Length - offset;
while (remainingBytes > 0) {
if (remainingBytes < BUFFERSIZE) {
buffer = new byte[remainingBytes];
BUFFERSIZE = (int)remainingBytes;
}
else if (remainingBytes > BUFFERSIZE) {
buffer = new byte[BUFFERSIZE];
}
s.Read(buffer, 0, BUFFERSIZE);
//push to webservice
Thread.Sleep(20);
//UploadService.Service1SoapClient client = new MultiLargeFileUpload.UploadService.Service1SoapClient();
//client.LargeUploadCompleted +=new EventHandler<AsyncCompletedEventArgs>(client_LargeUploadCompleted);
//client.LargeUploadAsync(1, buffer, file.Name);
offset += BUFFERSIZE;
int newPercentage = offset/(int)file.FileInfo.Length * 100;
file.Percentage = newPercentage;
remainingBytes = s.Length - offset;
}
//file.Percentage = 100;
//file.ImageSource = "accept.png";
}
}
的問題是,我不能更新UI,或橫紗操作這樣做。我試圖使用Dispatcher,但是我沒有成功,因爲我不知道在哪裏插入它。