开发者

Load a project resource into a WPF Webbrowser control

开发者 https://www.devze.com 2023-01-19 11:10 出处:网络
OK, so I thought this would be really simple even though I have only just got by previously when working with WPF project resource files.

OK, so I thought this would be really simple even though I have only just got by previously when working with WPF project resource files.

When my application starts I want to load in a local (shipped with the application) html file into the Webbrowser control. However开发者_如何学Python I cannot find an easy way to do this! Any ideas?


I just now ran into this same problem. I was hoping to simply do this:

<WebBrowser x:Name="myWebBrowser" Source="page.html"/>

But instead, I get this error:

Relative URIs are not allowed. Parameter name: source

So that's annoying. Instead, I ended up with a solution very similar to yours in code behind:

myWebBrowser.Source = new Uri(new System.IO.FileInfo("page.html").FullName);

I'm sure there's some XAML contortionist kung-fu method with which to get around that issue, but I have no clue what it is. ^_^


Got round this in the end by setting the build action of the file to 'Copy Always' and then using Environment.CurrentDirectory to get the application directory:

string appDir = Environment.CurrentDirectory;
Uri pageUri = new Uri(appDir + "/page.html");
myWebBrowser.Source = pageUri;

That seems to work fine.


Late to the show, but I stumbled on this when I was looking for the same thing. Got it to work in a more WPF fashioned way. The WebBrowser has a method NavigateToStream() so you can simply set the Resources Stream.

 StreamResourceInfo info = Application.GetResourceStream(new Uri("Page.html", UriKind.Relative));
 if (info != null)
     myWebBrowser.NavigateToStream(info.Stream);

This way you can keep it a resource and you do not have to copy it to disk.


Set "Build Action" of the file to "Embedded Resource" and then use this:

webBrowserMain.NavigateToStream(System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream("PsHandler.Resources.gnu.html"));

Here is how it looks in project: http://i.stack.imgur.com/dGkpH.png


You may want to use the following scheme instead:

  1. Change your resource's Build Action to Content.
  2. Set Copy to Output Directory to Copy if newer.
  3. Use the following URI scheme:

    <WebBrowser Source="pack://siteoforigin:,,,/Resources/page1.html" />
    

As of .NET 4.5, this is working fine for me.


I wrapped this in a usercontrol to deal with some of the issues with the WebBrowser control.. Set the 'Build Action' property of the Html pages, and any embedded content to 'Content' and the 'Copy to Output Directory' to 'Copy if Newer'. Maintain any page resource hierarchy in html just as you normally would on a server. In the viewmodel, or from code-behind, you just set one of the HtmlUri, HtmlString or HtmlStream properties of this control (you can also turn off the annoying click sound ;o)..

Example 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");
}

Use it like this:

<controls:SmartBrowser x:Name="wbData" 
                       Grid.Row="0"
                       HtmlUri="{Binding HtmlUri}" 
                       DefaultUri="{Binding DefaultUri}" />

SmartBrowser .xaml:

<Grid>
    <WebBrowser Name="wbData" />
</Grid>

Control code-behind:

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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消