EventHandler在Silverlight中使用MVVM不触发;RIA
本文关键字:RIA MVVM Silverlight EventHandler | 更新日期: 2023-09-27 18:11:34
所以我一定是做错了什么,只是不确定它是什么。我原来的PoC工作良好,但不是MVVM,所以我在翻译中丢失了一些东西。
所以我浏览到一个文件选择上传,一切都开始正常工作。它将第一个块传输到服务器,然后我的完整事件永远不会被调用。这将发送数组中所有额外的块,所以文件是不完整的。
我是不是漏了一步?
如果重要的话,服务是在IIS7上设置的,但我不确定是否需要在服务端设置任何东西来实现这一点。当我使用XAML代码时,它只是在PoC代码中"工作"。
模型代码:
public void uploadChunks(int index, string fileName)
{
FileUpload fileUpload = new FileUpload();
FileUpload.FileName = fileName;
FileUpload.chunk = fileChunks[index];
context.UploadFileAsync(fileUpload);
}
FileUploadServiceSoapClient context = new FileUploadServiceSoapClient("BasicHttpBinding_FileUploadServiceSoap");
int chunkSize = 15306;
public List<byte[]> fileChunks;
public double TotalChunks { get; set; }
/// <summary>
/// Convert file to an array of file chunks to stream to the upload location
/// </summary>
/// <param name="imageFile"></param>
public void convertToChunks(byte[] imageFile)
{
TotalChunks = Math.Ceiling((double)imageFile.Length / (double)chunkSize);
fileChunks = new List<byte[]>();
for (int i = 0; i < TotalChunks; i++)
{
byte[] chunk;
int startIndex = i * chunkSize;
if (startIndex + chunkSize > imageFile.Length)
chunk = new byte[imageFile.Length - startIndex];
else
chunk = new byte[chunkSize];
Array.Copy(imageFile, startIndex, chunk, 0, chunk.Length);
fileChunks.Add(chunk);
}
}
<<p> ViewModel代码/strong> public UploadViewModel()
{
uploadModel = new UploadModel(); // important – must initialize model
OpenFileCommand = new RelayCommand(OpenDialog);
StatusText = "Please select a file to upload";
context.UploadFileCompleted += new EventHandler<AsyncCompletedEventArgs>(context_UploadFileCompleted);
}
private void OpenDialog()
{
OpenFileDialog ofd = new OpenFileDialog();
if ((bool)ofd.ShowDialog())
{
_fileName = ofd.File.Name;
FileStream fs = ofd.File.OpenRead();
fileSize = (double)fs.Length;
index = 0;
sendData = 0;
byte[] file = new byte[fs.Length];
fs.Read(file, 0, file.Length);
// call our model and convert file into chunks
uploadModel.convertToChunks(file);
// start upload process, this only sends the first chunk all subsquent chunks
// are sent on the context_UploadFileToCrmCompleted function
uploadModel.uploadChunks(index, _fileName);
}
}
void context_UploadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error == null)
{
sendData += uploadModel.fileChunks[index].Length;
TotalFileSize = byteTranslation(sendData) + "/" + byteTranslation(fileSize);
if ((index + 1) < uploadModel.fileChunks.Count)
{
this.CurrentProgress = index / uploadModel.fileChunks.Count;
index += 1;
uploadModel.uploadChunks(index, _fileName);
}
else
{
StatusText = "Successfully uploaded. Submitting to the file repository...";
// Submit the upload to SharePoint
}
}
}
我希望我没有错过任何东西…你有两个FileUploadServiceSoapClients吗?
一个在ViewModel中,一个在模型中。您正在调用模型中的服务,但已完成的处理程序被分配给ViewModel的上下文。
所以你必须写
uploadModel.context.UploadFileCompleted += new EventHandler<AsyncCompletedEventArgs>(context_UploadFileCompleted);
不是context.UploadFileCompleted += new EventHandler<AsyncCompletedEventArgs>(context_UploadFileCompleted);
也许你最好不要在viewModel中直接使用ServiceClient,而是传递一个回调或让模型引发一个自己的事件。