2013-10-29 35 views
0

我使用下面的代码,使我的应用程序加载一个URL,如何在Windows Phone中使用WebBrowser?

WebBrowser wb = new WebBrowser(); 
wb.Navigate(new Uri(uri,UriKind.Absolute)); 

但仍是不加载该页面?可能是什么问题呢??

回答

1

呦不能有任何的回调,在WP默认WebBrowserTask,如果你需要更多的控制,使用WebBrowser因为你一直在做,

的XAML

<phone:WebBrowser IsScriptEnabled="True" LoadCompleted="UriContentLoaded" x:Name="browserControl" /> 

代码隐藏

public MainPage() //Your page constructor 
    { 
     InitializeComponent(); 

     this.browserControl.Loaded += SetBrowserUri; 
    } 
    private void SetBrowserUri(object sender, RoutedEventArgs e) 
    { 
     browserControl.Navigate(new Uri("http://www.bing.com")); 
    } 

    private void UriContentLoaded(object sender, NavigationEventArgs e) 
    { 
     if (MessageBox.Show("Do you want to load a second uri?", "Load again", MessageBoxButton.OKCancel) == MessageBoxResult.OK) 
     { 
      browserControl.LoadCompleted -= this.UriContentLoaded; //Remove previous handler 
      browserControl.LoadCompleted += this.SecondUriContentLoaded; //Add new handler 
      browserControl.Navigate(new Uri("http://www.google.com")); 
     } 
    } 

    private void SecondUriContentLoaded(object sender, NavigationEventArgs e) 
    { 
     MessageBox.Show("Finished loading"); 
    } 
+0

你上面提到的代码工作完美。但在我的情况下,首先我必须加载一个网址和基于值将导航到另一个url.So我必须显示一个网址(这是满足您的上述代码),并且我的LoadCompleted事件处理程序应该是为另一个网址。我怎样才能做到这一点?? – Aju

+0

更新了答案,出于好奇,为了加载第二个uri,你在寻找什么? – FunksMaName

+0

在我的第一个uri中,我将提供的数据很少,根据数据我将导航到page2,如果我的数据对于page3是有效的,如果它无效的话。在导航页面中,我将把我的值作为cookie。非常感谢您的回答! – Aju

3

WebBrowser是一个控件。就像按钮或文本块一样,除非将其放在页面的某个位置,否则您将看不到任何内容。

启动外部浏览器,使用WebBrowserTask

var webBrowserTask = new WebBrowserTask(); 

webBrowserTask.Uri = new Uri(uri, UriKind.Absolute); 

webBrowserTask.Show(); 
+0

我的任务是加载该页面,并且在给出该页面中的一些细节后,我将导航到另一个页面,在那里我必须获取在该导航页面中设置的cookie。我该怎么做? – Aju

+0

我只想知道,在使用WebBrowserTask时,我可以检测到另一个URL的加载。 – Aju

+0

您无法使用WebBrowserTask检测任何内容,因为它在您的应用程序之外执行。如果您想检测导航(例如OAuth身份验证),请将WebBrowser控件放入您的页面中,然后订阅“Navigating”事件 –

相关问题