Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load a project resource into a WPF Webbrowser control

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 I cannot find an easy way to do this! Any ideas?

like image 466
dgwyer Avatar asked Oct 12 '10 10:10

dgwyer


4 Answers

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. ^_^

like image 168
BJennings Avatar answered Sep 28 '22 06:09

BJennings


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.

like image 23
dgwyer Avatar answered Sep 28 '22 06:09

dgwyer


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.

like image 24
Y C Avatar answered Sep 28 '22 07:09

Y C


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

like image 29
chainerlt Avatar answered Sep 28 '22 05:09

chainerlt