envolví esto en un control de usuario para hacer frente a algunos de los problemas con el control WebBrowser .. Establecer la 'Acción de generación' propiedad de las páginas HTML, y cualquier contenido incorporado a 'contenido' y la 'Copia de salida Directorio 'a' Copiar si es nuevo '. Mantenga cualquier jerarquía de recursos de página en html como lo haría normalmente en un servidor. En el modelo de vista, o de código subyacente, que acaba de establecer uno de los HtmlUri, HtmlString o propiedades HtmlStream de este control (también se puede desactivar el sonido de clic molesto; o) ..
Ejemplo 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");
}
utilizar de esta manera:
<controls:SmartBrowser x:Name="wbData"
Grid.Row="0"
HtmlUri="{Binding HtmlUri}"
DefaultUri="{Binding DefaultUri}" />
SmartBrowser.xaml:
<Grid>
<WebBrowser Name="wbData" />
</Grid>
de control de código subyacente:
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
}
_Mejor_respuesta para .NET 4.5+. Las URL de paquete le permiten usar imágenes y otros recursos dentro del archivo .html. –