gpt4 book ai didi

Silverlight 跨线程 ui 更新问题

转载 作者:行者123 更新时间:2023-12-01 12:00:49 24 4
gpt4 key购买 nike

我有这门课:

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 服务执行此操作。我在我的 UI 中进行了双向绑定(bind),现在我想上传文件。我正在使用此代码执行此操作

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,但没有成功,因为我不知道在哪里插入它。

最佳答案

我猜你正试图在

中更新你的用户界面
           file.Percentage = newPercentage;
remainingBytes = s.Length - offset;

您必须将其包装在委托(delegate)中,并对其执行 Dispatcher.BeginInvoke 以从另一个线程更新您的 UI:

Dispatcher.BeginInvoke(()=>{
file.Percentage=newPercentage;
remainingBytes = s.Length - offset;
});

这样你在主线程上给messagequeue发送消息,主线程有空就去执行delegate。

关于Silverlight 跨线程 ui 更新问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1734614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com