0

我正在尝试将媒体库中的图像检索到列表框“lstImageFromMediaLibrary”内的扭曲面板,还尝试在图像加载时使用用户控件显示加载屏幕并将其添加到 popup.child 但我收到此异常“UnauthorizedAccessException” ' 当我删除所有与 backgrougWorker 相关的代码时,不存在此类未经授权的访问......

     void backroungWorker_DoWork(object sender, DoWorkEventArgs e)
      {

        foreach (Picture p in mediaLibrary.Pictures)
        {

            bitmapImage.SetSource(p.GetThumbnail());
            lstBitmapImage.Add(bitmapImage);
        }


        this.lstImageFromMediaLibrary.ItemsSource = lstBitmapImage;
    }

任何帮助都会得到帮助,我希望我能说清楚....

编辑:好的,所以现在我这样做

    BackgroundWorker backroungWorker = new BackgroundWorker();
    Popup popup = new Popup();
    public PanoramaPage1()
    {

        InitializeComponent();

        showpopup();
        init();
    }

    private void init()
    {
        backroungWorker.WorkerReportsProgress = true;
        backroungWorker.DoWork += new DoWorkEventHandler(backroungWorker_DoWork);
        backroungWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backroungWorker_RunWorkerCompleted);
        backroungWorker.ProgressChanged+=new ProgressChangedEventHandler(backroungWorker_ProgressChanged);
        backroungWorker.RunWorkerAsync();
    }
     void backroungWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        this.Dispatcher.BeginInvoke(() =>
        {
            popup.IsOpen = false;
        }
        );
    }
     void backroungWorker_DoWork(object sender, DoWorkEventArgs e)
     {
         backroungWorker.ReportProgress(10);

     }
     void backroungWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
     {
         this.InitializePage();
     }


    private void showpopup()
    {

        popup.Child = new SplashScreenControl();
        popup.Width = 480;
        popup.IsOpen = true;

    }

    private void InitializePage()
    {

        MediaLibrary mediaLibrary = new MediaLibrary();
        List<BitmapImage> lstBitmapImage = new List<BitmapImage>();
        foreach (Picture p in mediaLibrary.Pictures)
        {
            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.SetSource(p.GetThumbnail());
            lstBitmapImage.Add(bitmapImage);
        }


        this.lstImageFromMediaLibrary.ItemsSource = lstBitmapImage;
    }

但进度条仍然只显示一个点,没有别的.....

4

1 回答 1

3

您正在DoWork活动中访问您的用户界面。您应该通过诸如ProgressChangedRunWorkerCompleted事件之类的后台 Worker 事件与您的应用程序进行通信。

从第一个链接:

您必须小心不要在 DoWork 事件处理程序中操作任何用户界面对象。相反,通过 BackgroundWorker 事件与用户界面进行通信。

于 2012-10-05T15:44:33.990 回答