上传文件时Silverlight 3进度条

时间:2010-01-09 00:02:24

标签: silverlight-3.0 progress-bar

我是Silverlight的新手,我创建了一个Silverlight应用程序来调整大小和上传图像。问题是在文件上传完成之前滚动条不会更新。我已经包含了PushData函数和事件处理程序的示例代码。请有人告诉我,我做错了这个问题让我发疯了。

void btnTotalSizes_Click(object sender, RoutedEventArgs e)
{
    // ...
    UriBuilder ub = new UriBuilder("http://localhost:21636/FileReceiver.ashx");
    WebClient wc = new WebClient();
    wc.OpenWriteCompleted += (s2, e2) =>
    {
        PushData(outStream, e2.Result);
        e2.Result.Close();
        outStream.Close();
    };
    wc.OpenWriteAsync(ub.Uri);
}

private static void PushData(Stream input, Stream output)
{
    byte[] buffer = new byte[4096];
    int bytesRead = 0;
    int tempTotal = 0;

    while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
    {
        output.Write(buffer, 0, bytesRead);
        tempTotal += bytesRead;
        _uploadProg++;
        int percentDone = (int)(
            ((decimal)tempTotal / (decimal)input.Length) * 100);
        ProgressUpdated(null, new UploadProgressChangedEventArgs(percentDone));
    } 
}

void MainPage_ProgressUpdated(object sender, UploadProgressChangedEventArgs e)
{
    progFileProg.Maximum = 100;
    progFileProg.Value = e.ProgressPercentage;
    sbUpdateProg.Begin();
} 

2 个答案:

答案 0 :(得分:1)

您需要处理WebClient.UploadProgressChanged事件。

事件处理程序应定期更新。在处理程序中,更新您的UI。

答案 1 :(得分:0)

我设法让它现在开始。我确实使用了由Rubens Farias评论的Dispatcher.BeginInvoke()。此外,我不得不将上传循环添加到新线程中。我假设在异步回调之前将是一个新线程,但我猜不是。

public static event EventHandler UploadCompleted;
        public static event ProgressChangedEvent ProgressUpdated;

        public static int _fileLength;
        public static int _uploadProg;


        private List<FileInfo> _allSelectedFiles = new List<FileInfo>();


        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);

            btnAnimate.Click += new RoutedEventHandler(btnAnimate_Click);
            btnTotalSizes.Click += new RoutedEventHandler(btnTotalSizes_Click);
            ProgressUpdated += new ProgressChangedEvent(MainPage_ProgressUpdated);
        }

        void MainPage_ProgressUpdated(object sender, UploadProgressChangedEventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                progFileProg.Maximum = 100;
                progFileProg.Value = _uploadProg;
                sbUpdateProg.Begin();
            });
        }


        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
        }

        void btnTotalSizes_Click(object sender, RoutedEventArgs e)
        {
            foreach (FileInfo fi in _allSelectedFiles)
            {
                Stream fileStream = fi.OpenRead();


                //DecodedJpeg jpegIn = new JpegDecoder(fileStream).Decode();
                //DecodedJpeg jpegOut = new DecodedJpeg(
                //    new ImageResizer(jpegIn.Image)
                //        .Resize(1500, ResamplingFilters.NearestNeighbor),
                //    jpegIn.MetaHeaders); // Retain EXIF details
                //MemoryStream outStream = new MemoryStream();
                //new JpegEncoder(jpegOut, 90, outStream).Encode();

                //outStream.Seek(0, SeekOrigin.Begin);
                //outStream.Position = 0;
                //UploadFile(fi.Name, outStream, "resources");

                UriBuilder ub = new UriBuilder("http://localhost:21636/FileReceiver.ashx");
                WebClient wc = new WebClient();

                //wc.UploadProgressChanged += (s2, e2) =>
                //{
                //    ProgressUpdated(null, new UploadProgressChangedEventArgs(_uploadProg));
                //};



                wc.OpenWriteCompleted += (s2, e2) =>
                {
                    System.Threading.Thread thr = new Thread(delegate()
                    {
                        PushData(fileStream, e2.Result);
                        e2.Result.Close();
                        fileStream.Close();
                    });
                    thr.Start();
                };
                wc.OpenWriteAsync(ub.Uri);

            }
        }





        private static void PushData(Stream input, Stream output)
        {
            byte[] buffer = new byte[4096];
            int bytesRead = 0;
            int tempTotal = 0;



            while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
            {
                output.Write(buffer, 0, bytesRead);
                tempTotal += bytesRead;
                _uploadProg++;
                int percentDone = (int)(((decimal)tempTotal / (decimal)input.Length) * 100);
                _uploadProg = percentDone;
                ProgressUpdated(null, new UploadProgressChangedEventArgs(percentDone));

                Thread.Sleep(50);
            } 
        }