본문 바로가기

Silverlight

[Sample] 다운로드 진행률 출력(Percentage:%)

다운로드 진행률 Percentage(%)

1.1에서는 Download 클래스의 DownloadProgressChanged 이벤트를 이용하여 % 를 출력했지만 2.0에서는 WebClient의 DownloadProgressChanged() 이벤트를 사용하며, Silverlight 2.0에 새로 추가된 이벤트 입니다. 

MediaElement media = null;
       
        public Page()
        {
            InitializeComponent();
            WebClient webClient = new WebClient();
           // 동영상 경로
            webClient.OpenReadAsync(new Uri("sl2.wmv", UriKind.Relative));
            webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);

            webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
        }

        void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
           // DownloadProgressChangedEventArgs는 미디어를
           // 다운로드 하는 동안 일어나는 이벤트 아규먼트입니다. 
            var done = e.ProgressPercentage;
            downProgress.Text = Math.Floor(done) + "%";
        }

        void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            media = new MediaElement();
            media.SetSource(e.Result);

            LayoutRoot.Children.Add(media);
            media.Width = 300;
            media.Height = 300;
            media.Play();
        }