Я обернул это в usercontrol для решения некоторых проблем с элементом управления WebBrowser. Установите свойство «Build Action» для HTML-страниц, а для любого внедренного содержимого - «Content» и «Copy to Output Directory».«Копировать, если новее».Поддерживайте любую иерархию ресурсов страницы в html, как это обычно делается на сервере.В модели представления или из выделенного кода вы просто устанавливаете одно из свойств HtmlUri, HtmlString или HtmlStream этого элемента управления (вы также можете отключить раздражающий звук щелчка; o) ..
Пример Uri:
public Uri DefaultUri
{
get { return new Uri(@"pack://siteoforigin:,,,/Html/h1.html"); }
}
private Uri GetUri(string ItemName)
{
if (string.IsNullOrEmpty(ItemName)) return null;
return new Uri(@"pack://siteoforigin:,,,/Html/" + ItemName + ".html");
}
Используйте его следующим образом:
<controls:SmartBrowser x:Name="wbData"
Grid.Row="0"
HtmlUri="{Binding HtmlUri}"
DefaultUri="{Binding DefaultUri}" />
SmartBrowser .xaml:
<Grid>
<WebBrowser Name="wbData" />
</Grid>
Код управления сзади:
public partial class SmartBrowser : UserControl
{
#region Constants
private const int Feature = 21;
private const int SetFeatureOnProcess = 0x00000002;
#endregion
#region Api
[DllImport("urlmon.dll")]
[PreserveSig]
[return: MarshalAs(UnmanagedType.Error)]
static extern int CoInternetSetFeatureEnabled(int featureEntry, [MarshalAs(UnmanagedType.U4)] int dwFlags, bool fEnable);
#endregion
#region Fields
private WebBrowser _wbData;
#endregion
#region Constructor
public SmartBrowser()
{
InitializeComponent();
}
#endregion
#region Overrides
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_wbData = (WebBrowser)FindElement("wbData");
if (_wbData != null)
if (DefaultUri != null)
_wbData.Navigate(DefaultUri);
}
#endregion
#region Properties
#region DefaultUri
/// <summary>
/// Gets/Sets
/// </summary>
public static readonly DependencyProperty DefaultUriProperty =
DependencyProperty.Register("DefaultUri", typeof(Uri), typeof(SmartBrowser),
new PropertyMetadata(null, new PropertyChangedCallback(OnDefaultUriChanged)));
public Uri DefaultUri
{
get { return (Uri)GetValue(DefaultUriProperty); }
set { SetValue(DefaultUriProperty, value); }
}
private static void OnDefaultUriChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
SmartBrowser sb = d as SmartBrowser;
if (e.NewValue == null) return;
if (sb._wbData == null) return;
Uri htmlUri = e.NewValue as Uri;
sb._wbData.Navigate(htmlUri);
}
#endregion
#region HtmlStream
/// <summary>
/// Gets/Sets
/// </summary>
public static readonly DependencyProperty HtmlStreamProperty =
DependencyProperty.Register("HtmlStream", typeof(Stream), typeof(SmartBrowser),
new PropertyMetadata(null, new PropertyChangedCallback(OnHtmlStreamChanged)));
public Stream HtmlStream
{
get { return (Stream)GetValue(HtmlStreamProperty); }
set { SetValue(HtmlStreamProperty, value); }
}
private static void OnHtmlStreamChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
SmartBrowser sb = d as SmartBrowser;
if (e.NewValue == null) return;
if (sb._wbData == null) return;
Stream htmlStream = e.NewValue as Stream;
sb.Navigate(htmlStream);
}
#endregion
#region HtmlString
/// <summary>
/// Gets/Sets
/// </summary>
public static readonly DependencyProperty HtmlStringProperty =
DependencyProperty.Register("HtmlString", typeof(string), typeof(SmartBrowser),
new PropertyMetadata(null, new PropertyChangedCallback(OnHtmlStringChanged)));
public string HtmlString
{
get { return (string)GetValue(HtmlStringProperty); }
set { SetValue(HtmlStringProperty, value); }
}
private static void OnHtmlStringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
SmartBrowser sb = d as SmartBrowser;
if (e.NewValue == null) return;
if (sb._wbData == null) return;
string htmlString = e.NewValue as string;
sb.Navigate(htmlString);
}
#endregion
#region HtmlUri
/// <summary>
/// Gets/Sets
/// </summary>
public static readonly DependencyProperty HtmlUriProperty =
DependencyProperty.Register("HtmlUri", typeof(Uri), typeof(SmartBrowser),
new PropertyMetadata(null, new PropertyChangedCallback(OnHtmlUriChanged)));
public Uri HtmlUri
{
get { return (Uri)GetValue(HtmlUriProperty); }
set { SetValue(HtmlUriProperty, value); }
}
private static void OnHtmlUriChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
SmartBrowser sb = d as SmartBrowser;
if (e.NewValue == null) return;
if (sb._wbData == null) return;
Uri htmlUri = e.NewValue as Uri;
sb.Navigate(htmlUri);
}
#endregion
#region SoundEffects
/// <summary>
/// Gets/Sets
/// </summary>
public static readonly DependencyProperty SoundEffectsProperty =
DependencyProperty.Register("SoundEffects", typeof(bool), typeof(SmartBrowser),
new PropertyMetadata(false, new PropertyChangedCallback(OnSoundEffectsChanged)));
public bool SoundEffects
{
get { return (bool)GetValue(SoundEffectsProperty); }
set { SetValue(SoundEffectsProperty, value); }
}
private static void OnSoundEffectsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
SmartBrowser sb = d as SmartBrowser;
if (e.NewValue == null) return;
if (sb._wbData == null) return;
bool enable = (bool)e.NewValue;
sb.EnableSoundEffects(enable);
}
#endregion
#endregion
#region Methods
private void EnableSoundEffects(bool Enable)
{
try
{
CoInternetSetFeatureEnabled(Feature, SetFeatureOnProcess, !Enable);
}
catch { }
}
private void Navigate(Uri Html)
{
if (Html == null) return;
try
{
_wbData.Navigate(Html);
}
catch { }
}
private void Navigate(string Html)
{
if (string.IsNullOrEmpty(Html)) return;
try
{
_wbData.NavigateToString(Html);
}
catch { }
}
private void Navigate(Stream Html)
{
if (Html == null) return;
try
{
_wbData.NavigateToStream(Html);
}
catch { }
}
#endregion
#region Helpers
/// <summary>
/// Find an element in the visual tree
/// </summary>
/// <param name="name">Element name</param>
/// <returns>Element [object]</returns>
private object FindElement(string name)
{
try
{
if (this.Template != null)
return this.FindName(name);
}
catch { }
return null;
}
#endregion
}