当我调用Web浏览器时发生错误....

时间:2011-07-28 13:03:46

标签: windows-phone-7 webbrowser-control

for (int i = 0; i < list.Count; i++) {
  ds = new Discription();

  PivotItem pivotItem = new PivotItem();
  pivotItem.Header = list.ElementAt(i).header.ToString();

  StackPanel sta = new StackPanel();
  WebBrowser wb = new WebBrowser();

  sta.Children.Add(wb);

  pivotItem.Content = sta;         
  Pivot_item1.Items.Add(pivotItem);
  wb.NavigateToString(list.ElementAt(i).Detail.ToString());
}

调用Web浏览器控件时发生错误 在它位于可视树中之前,您无法调用WebBrowser方法。

2 个答案:

答案 0 :(得分:4)

订阅Webbrowser控件的Loaded事件,并将导航代码移动到加载的处理程序。

替换

wb.NavigateToString(list.ElementAt(i).Detail.ToString());

var address = list.ElementAt(i).Detail.ToString();
wb.Loaded += (sender, e) => { wb.NavigateToString(address); }

答案 1 :(得分:0)

    for (int i = 0; i < list.Count; i++) {
        ds = new Discription();

          PivotItem pivotItem = new PivotItem();
            pivotItem.Header = list.ElementAt(i).header.ToString();

       Grid sta = new Grid();
         WebBrowser wb = new WebBrowser();
         var address = list.ElementAt(i).Detail.ToString();
          wb.Loaded += (sender, e) => { wb.NavigateToString(address); };
       sta.Children.Add(wb);

         pivotItem.Content = sta;         
         Pivot_item1.Items.Add(pivotItem);
        wb.NavigateToString(list.ElementAt(i).Detail.ToString());
         }