-
Notifications
You must be signed in to change notification settings - Fork 108
/
WebViewExtension.cs
89 lines (82 loc) · 3.97 KB
/
WebViewExtension.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// Copyright (c) 2012 Tim Heuer
//
// Licensed under the Microsoft Public License (Ms-PL) (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://opensource.org/licenses/Ms-PL.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Derived from http://blogs.msdn.com/b/delay/archive/2011/04/14/quot-those-who-cannot-remember-the-past-are-condemned-to-repeat-it-quot-webbrowserextensions-stringsource-attached-dependency-property-makes-silverlight-windows-phone-wpf-s-webbrowser-control-more-xaml-and-binding-friendly.aspx
using System;
using System.Diagnostics.CodeAnalysis;
using System.Windows;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace Callisto
{
/// <summary>
/// Useful extensions for the WebBrowser class.
/// </summary>
public static class WebViewExtension
{
/// <summary>
/// Gets the value of the StringSource attached dependency property.
/// </summary>
/// <param name="view">WebBrowser instance for which to get the value.</param>
/// <returns>Value of the property.</returns>
[SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Method applies only to WebView instances.")]
public static string GetHtmlSource(WebView view)
{
if (null == view)
{
throw new ArgumentNullException("view");
}
return (string)view.GetValue(HtmlSourceProperty);
}
/// <summary>
/// Sets the value of the StringSource attached dependency property.
/// </summary>
/// <param name="view">WebBrowser instance for which to set the value.</param>
/// <param name="value">Value to set.</param>
[SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Method applies only to WebView instances.")]
public static void SetHtmlSource(WebView view, string value)
{
if (null == view)
{
throw new ArgumentNullException("view");
}
view.SetValue(HtmlSourceProperty, value);
}
/// <summary>
/// Defines the StringSource attached dependency property for the WebBrowser class.
/// </summary>
public static readonly DependencyProperty HtmlSourceProperty = DependencyProperty.RegisterAttached(
"HtmlSource", typeof(string), typeof(WebViewExtension), new PropertyMetadata(null, OnHtmlSourcePropertyChanged));
/// <summary>
/// Handles changes to the StringSource attached dependency property.
/// </summary>
/// <param name="o">Instance for which the property changed.</param>
/// <param name="e">Property change information.</param>
[SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "WebView", Justification = "Class name.")]
[SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "HtmlSource", Justification = "Property name.")]
private static void OnHtmlSourcePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
var view = o as WebView;
if (null == view)
{
throw new NotSupportedException("The HtmlSource attached dependency property is only valid for WebView instances.");
}
// Determine new HTML content
var newString = e.NewValue as string;
var newHtml = string.IsNullOrEmpty(newString) ? "<html></html>" : newString;
view.NavigateToString(newHtml);
}
}
}