EventHandler没有使用MVVM&amp ;;在Silverlight中触发RIA?

时间:2011-10-19 04:13:06

标签: c# silverlight mvvm-light

所以我必须在这里做错事,只是不确定它到底是什么。我原来的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);
        }
    }

ViewModel代码

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
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

我希望我没有遗漏任何东西......你有两个FileUploadServiceSoapClients吗?

ViewModel中的一个和模型中的一个。您正在模型中调用服务,但已完成的处理程序将分配给ViewModel的上下文。

所以你必须写

uploadModel.context.UploadFileCompleted += new EventHandler<AsyncCompletedEventArgs>(context_UploadFileCompleted); 

而不是

context.UploadFileCompleted += new EventHandler<AsyncCompletedEventArgs>(context_UploadFileCompleted);

您最好不要直接在viewModel中使用ServiceClient,而是传递回调或让模型引发自己的事件。