Skip to content

Instantly share code, notes, and snippets.

@lambda125
Created September 2, 2012 08:44
Show Gist options
  • Save lambda125/3595968 to your computer and use it in GitHub Desktop.
Save lambda125/3595968 to your computer and use it in GitHub Desktop.
Web browser control
<UserControl
x:Class="MetroControls.WebViewHostControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Converters="using:Metro.Controls.Converters" mc:Ignorable="d"
d:DesignHeight="768"
d:DesignWidth="320"
VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<UserControl.Resources>
<Converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
<Style x:Key="MediumContentTextStyle" TargetType="TextBlock">
<!-- style content omitted for brevity -->
</Style>
</UserControl.Resources>
<Grid Opacity="0.8">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- HEADER -->
<!-- Header area for panel: omitted for brevity -->
<Grid Grid.Row="1" Margin="10">
<WebView x:Name="webView" Visibility="Collapsed"/>
<Rectangle x:Name="blockingRect" />
<TextBlock x:Name="failedMessage" Text="{Binding FailedMessage}"
Style="{StaticResource MediumContentTextStyle}"
TextWrapping="Wrap" TextAlignment="Justify"
VerticalAlignment="Center" HorizontalAlignment="Center"
Visibility="Collapsed"/>
<ProgressRing x:Name="progressRing" />
</Grid>
</Grid>
</UserControl>
public sealed partial class WebViewHostControl : UserControl, INotifyPropertyChanged
{
//...
public WebViewHostControl()
{
this.InitializeComponent();
this.DataContext = this;
webView.NavigationFailed += OnWebViewNavigationFailed;
webView.LoadCompleted += OnWebViewLoadCompleted;
this.Loaded += OnControlLoaded;
this.Unloaded += OnControlUnLoaded;
}
private void OnControlUnLoaded(object sender, RoutedEventArgs e)
{
if (webView != null)
{
webView.LoadCompleted -= OnWebViewLoadCompleted;
webView.NavigationFailed -= OnWebViewNavigationFailed;
}
}
//...
public string Uri
{
get { return _uri; }
set
{
SetProperty(ref _uri, value);
}
}
public string LocalFallbackUri
{
get { return _localFallbackUri; }
set { SetProperty(ref _localFallbackUri, value); }
}
private void OnControlLoaded(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(Uri))
NavigateAsync(Uri, false);
}
private void OnWebViewLoadCompleted(object sender, NavigationEventArgs e)
{
if (!_webViewNavigationFailed)
{
//Debug.WriteLine("Load completed, _webViewNavigationFailed is false");
ShowWebView();
}
}
private void OnWebViewNavigationFailed(object sender, WebViewNavigationFailedEventArgs e)
{
//try to get it from the local fallback uri
_webViewNavigationFailed = true;
//Debug.WriteLine("Nav failed: " + e.Uri + ", reason: " + e.WebErrorStatus);
if (e.Uri.ToString() != LocalFallbackUri && !string.IsNullOrWhiteSpace(LocalFallbackUri))
NavigateAsync(LocalFallbackUri, isLocal: true);
else
ShowLoadingFailedMessage();
}
private void ShowLoadingFailedMessage()
{
HideWebView(showProgress: false);
failedMessage.Visibility = Visibility.Visible;
}
private async void NavigateAsync(string uri, bool isLocal = false)
{
HideWebView();
try
{
//Debug.WriteLine("Navigating to " + uri);
if (!isLocal)
webView.Navigate(new Uri(uri));
else
{
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(uri));
var randomAccessStream = await file.OpenReadAsync();
using (var stream = randomAccessStream.AsStreamForRead())
{
using (var streamReader = new StreamReader(stream))
{
var fileContents = await streamReader.ReadToEndAsync();
if (!string.IsNullOrWhiteSpace(fileContents))
{
webView.NavigateToString(fileContents);
}
}
}
}
_webViewNavigationFailed = false;
}
catch (Exception ex)
{
//This should be replaced with proper runtime logging
Debug.WriteLine("Error navigating: " + ex);
ShowLoadingFailedMessage();
}
}
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment