【问题标题】:EventHandler not firing in Silverlight using MVVM & RIA?EventHandler 没有在 Silverlight 中使用 MVVM 和 RIA 触发?
【发布时间】:2011-10-19 04:13:06
【问题描述】:

所以我一定是在这里做错了,只是还不确定是什么。我原来的 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
            }
        }
    }

【问题讨论】:

  • 只是一个更新。如果我将上传块从模型中移到视图模型中,一切都会正确触发。需要做什么才能使模型发挥作用?谢谢。

标签: c# silverlight mvvm-light


【解决方案1】:

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

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

所以你必须写

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

而不是

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

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

【讨论】:

    猜你喜欢
    • 2011-10-15
    • 2011-02-13
    • 2012-02-12
    • 2010-10-19
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    相关资源
    最近更新 更多