为什么我得到.UnauthorizedAccessException?

时间:2012-10-05 15:37:22

标签: c# wpf windows-phone-7 backgroundworker

我正在尝试将媒体库中的图像检索到列表框'lstImageFromMediaLibrary'内的warp面板,也正在尝试使用用户控件显示加载屏幕并将其添加到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;
    }

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

1 个答案:

答案 0 :(得分:3)

您正在DoWork活动中访问您的用户界面。您应该通过后台工作人员事件(例如ProgressChangedRunWorkerCompleted事件)与您的应用程序进行通信。

来自第一个链接:

  

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