1

我正在开发一个带有全景页面的 Windows Phone 7 应用程序。

这个页面有两个项目,第一个带有一些文本,第二个带有控制图。第二个加载时间太长,所以我决定以这种方式加载它:

namespace Tested
{
    public partial class DemoDetail : PhoneApplicationPage
    {
        private bool isFirstNavigateToMap;
        private BackgroundWorker bgWorker;
        private Map map;

        public DemoDetail()
        {
            InitializeComponent();
            map = null;
            isFirstNavigateToMap = true;
            bgWorker = new BackgroundWorker();

            bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
            bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
        }

        private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            map = new Map();
        }

        private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (map != null)
                mapGrid.Children.Add(map);
        }

        private void Panorama_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            Panorama panorama = (Panorama)sender;
            PanoramaItem panoramaItem = (PanoramaItem)(panorama.SelectedItem);

            if ((isFirstNavigateToMap) && (panoramaItem.Name.Equals("mapPanoramaItem")))
            {
                isFirstNavigateToMap = false;
                bgWorker.RunWorkerAsync();
            }
        }
    }
}

但是当我移动到第二个全景项目时,我在线得到了这个异常map = new Map();

No se controló System.UnauthorizedAccessException
  Message=Invalid cross-thread access.
  StackTrace:
       at MS.Internal.XcpImports.CheckThread()
       at System.Windows.DependencyObject..ctor(UInt32 nativeTypeIndex, IntPtr constructDO)
       at System.Windows.DependencyObject..ctor(UInt32 nativeTypeIndex)
       at System.Windows.UIElement..ctor(UInt32 nKnownTypeIndex)
       at System.Windows.FrameworkElement..ctor(UInt32 nKnownTypeIndex)
       at System.Windows.Controls.Control..ctor(UInt32 nKnownTypeIndex)
       at System.Windows.Controls.ContentControl..ctor()
       at Microsoft.Phone.Controls.Maps.Core.MapBase..ctor()
       at Microsoft.Phone.Controls.Maps.Core.MapCore..ctor()
       at Microsoft.Phone.Controls.Maps.Map..ctor()
       at Demonstration.DemoDetail.bgWorker_DoWork(Object sender, DoWorkEventArgs e)
       at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
       at System.ComponentModel.BackgroundWorker.OnRun(Object argument)
       at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadPool.WorkItem.doWork(Object o)
       at System.Threading.Timer.ring()

如何异步创建和加载控制图?

4

2 回答 2

2

你将不得不做

Deployment.Current.Dispatcher.BeginInvoke(()=>
{ 
      map = new Map();   
});

任何与 UI 有关的事情都必须在启动它的线程中完成,即 UI 线程。当您从后台线程访问 UI 时,您应该通过要求 UI 线程从队列中调用它来让 UI 线程对其进行操作。

PS:SO上有很多类似的帖子,请cross thread error搜索一下。

于 2012-10-28T18:47:42.097 回答
0

AFAIK,你不能。Bing地图控件是UI对象,所以必须在UI线程上创建。

于 2012-10-28T16:41:51.330 回答