首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Windows Mobile >

界面卡住不动,NavigationService.Navigate 不转入下一个页面

2013-11-30 
界面卡住不动,NavigationService.Navigate 不转向下一个页面 public static async void SavingDataThread(

界面卡住不动,NavigationService.Navigate 不转向下一个页面

 public static async void SavingDataThread()
        {
          //在当前页面进行相关进度操作
            string struid = null;
            int persent = 0;
            int TotalCount = App.Global.AllCons.Count;
            ContactStore store1 = await ContactStore.CreateOrOpenAsync(ContactStoreSystemAccessMode.ReadWrite, ContactStoreApplicationAccessMode.ReadOnly);
            //serach
            IReadOnlyDictionary<string, StoredContact> conlistWP = await SaveData.QueryDuplicateContacts(store1);
            for ( int i = 0; i < TotalCount;i++)
            {
                struid = SaveData.IsExistContact(conlistWP, App.Global.AllCons[i]);
                if (struid != null)
                {
                        App.GlobalContactinfo.GlobalDuplicateContacts.Add(App.Global.AllCons[i]);
                        await SaveData.Info(struid, store1, App.Global.AllCons[i]);
                }
                else
                {
                    await SaveData.AddContacts(store1, App.Global.AllCons[i]);
                    App.Global.AddCount++;
                }
                persent = (int)((float)(i + 1) * 100 / (float)TotalCount);
                _mainPage.Dispatcher.BeginInvoke(delegate()
                {
                    _mainPage.mainProgressbar.Value = persent;
                });
            }
            await Task.Run(() =>
            {
                if (persent >= 100)
                    return;
            });
          //转向下一个页面
            this.Dispatcher.BeginInvoke(delegate()
            {
                if (App.GlobalContactinfo.GlobalDuplicateContacts.Count == 0)
                {
                      NavigationService.Navigate(new Uri("/ResultPage.xaml", UriKind.Relative));
                }
                else
                {
                        NavigationService.Navigate(new Uri("/DetailPage.xaml", UriKind.Relative));
                }
            });
        }


上面代码的作用是在向手机添加完联系人后,转向下一个处理页面,可是有些时候当进度条走完(到100%),界面一直卡在这里不转到下个页面,这种情况随机发生,不知道是怎么回事,我使用弹messagebox的方法跟踪,
 this.Dispatcher.BeginInvoke(delegate() 


            {
                if (App.GlobalContactinfo.GlobalDuplicateContacts.Count == 0)
                {
                    //程序可以走到这里
                      System.Windows.MessageBox.Show("0");
                      NavigationService.Navigate(new Uri("/ResultPage.xaml", UriKind.Relative));
                }
                else
                {
                        //程序可以执行到这里
                       System.Windows.MessageBox.Show("else");
                        NavigationService.Navigate(new Uri("/DetailPage.xaml", UriKind.Relative));
                }
            });


使用try捕捉异常NavigationService.Navigate也不产生任何异常,但是就是不到下个页面,一直保持进度条最后的状态,不知道是怎么回事?请教下大家
[解决办法]
去掉 async 试试
[解决办法]
 await Task.Run(() =>
            {
                if (persent >= 100)
                    return;
            });
这个干吗用,直接这样吧if (persent >= 100){return;}

SavingDataThread这个方法不是在ui线程里面调用的?
这个删掉this.Dispatcher.BeginInvoke(delegate()
[解决办法]
for ( int i = 0; i < TotalCount;i++)
            {
                struid = SaveData.IsExistContact(conlistWP, App.Global.AllCons[i]);
                if (struid != null)
                {
                        App.GlobalContactinfo.GlobalDuplicateContacts.Add(App.Global.AllCons[i]);
                         SaveData.Info(struid, store1, App.Global.AllCons[i]);
                }
                else
                {
                    SaveData.AddContacts(store1, App.Global.AllCons[i]);
                    App.Global.AddCount++;
                }
                persent = (int)((float)(i + 1) * 100 / (float)TotalCount);
                _mainPage.Dispatcher.BeginInvoke(delegate()
                {
                    _mainPage.mainProgressbar.Value = persent;


                });
            }
           
                if (persent >= 100){
                    return;
            }
          //转向下一个页面
            this.Dispatcher.BeginInvoke(delegate()
            {
                if (App.GlobalContactinfo.GlobalDuplicateContacts.Count == 0)
                {
                      NavigationService.Navigate(new Uri("/ResultPage.xaml", UriKind.Relative));
                }
                else
                {
                        NavigationService.Navigate(new Uri("/DetailPage.xaml", UriKind.Relative));
                }
            });

热点排行