Reputation: 3264
I have a WebBrowser embedded in tabcontrol in my WPF app as follows.
<Grid>
<TabControl SelectionChanged="tabwindow_SelectionChanged">
<TabItem Header="Preview" Name="PreviewWindow">
<WebBrowser x:Name="PreviewBrowser" Loaded="PreviewBrowser_OnLoad"/>
</TabItem>
<TabItem Header="XML">
<ew:CommonXmlEditor
x:Name="rawXmlEditor"
DataContext="{Binding ElementName=XMLDockMainWindow}"
ShowLineNumbers="True"
FontFamily="Consolas"
FontSize="10pt"
SyntaxHighlighting="XML"
ContextMenu="{StaticResource XmlEditorContextMenu}"/>
</TabItem>
<TabItem Header="Not Used">
The Mushrooms Tab
</TabItem>
</TabControl>
</Grid>
In tabwindow_SelectionChanged() event I am trying to update the contents of the browser as follows
private void tabwindow_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string previewFile = "/session/PreviewXml.data";
XmlMethodCall.Instance.Put(previewFile, this.XmlEditor.Text, true);
//WebBrowser browser = new WebBrowser();
string urlForPreview = XmlMethodCall.Instance.ServerUri.ToString() + "/admin/framework/form_preview.php?path=" + previewFile;
App app = Application.Current as App;
string sessionState = XmlMethodCall.Instance.HttpResponse.Headers.Get("Cookie");
int index = sessionState.IndexOf(";");
if (index >= 0)
{
sessionState = sessionState.Substring(0, index);
}
sessionState = sessionState.Replace("ESPSessionState=", "");
InternetSetCookie(XmlMethodCall.Instance.ServerUri.ToString() + "/admin/", "EspSessionId", sessionState);
PreviewBrowser.Source = new Uri(urlForPreview);
PreviewBrowser.Navigate(urlForPreview);
bool isloa = PreviewBrowser.IsLoaded;
}
However, the tabview always shows the first rendered screen of the webbrowser. I have to right click and say refresh on the tabview to see the updated contents of the webbrowser. On closer inspection the problem is tracked to the webbrowser which is not loaded even a different source is specified(or calling navigate(), refresh()....). On the previous code the boolean variable isloa is false.
Can someone throw some light into the issue. I see someone used a thread.sleep() as the last method in window_loaded to solve a similiar issue. Is there any better approach?
Upvotes: 0
Views: 1812
Reputation: 3407
Jimmy, in my example below when you type some URI in Address text box and then switch to preview tab - browser content is loaded and rendered as usual.
<TabControl SelectionChanged="TabControl_SelectionChanged">
<TabItem Header="Preview" Name="PreviewWindow">
<WebBrowser x:Name="PreviewBrowser" Loaded="PreviewBrowser_Loaded"/>
</TabItem>
<TabItem Header="XML">
<TextBox Name="Address"/>
</TabItem>
<TabItem Header="Not Used">
The Mushrooms Tab
</TabItem>
</TabControl>
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (string.IsNullOrEmpty(Address.Text))
{
PreviewBrowser.Navigate("http://google.com");
}
else
{
PreviewBrowser.Navigate(Address.Text);
}
}
I suppose the problem is in magic before line PreviewBrowser.Source
. For example I could suppose that in your scenario URI does not changes (you just upload another data), so when you call Navigate nothing happened because browser could think that you are requesting the same page.
Try to add to URI some parameter that varies from request to request, timestamp will be perfect ("http://server/?param1=x¶m2=y×tamp="+DateTime.Ticks.ToString()).
Upvotes: 2