Welcome Guest Search | Active Topics | Sign In | Register

Simplest way to load embedded html file? Options
cocoMonkey
Posted: Wednesday, February 3, 2021 12:49:45 PM
Rank: Newbie
Groups: Member

Joined: 2/3/2021
Posts: 6
Hi all,

I've been trying for a few days now - I will say I am totally new to .new - i'm trying to use vb.net - and the (Very complicated!) tabbed browsing demo.

I would like to just create a simple form - with the webbrowser control on it, and load a local embedded index.html file (Which the eventually will references js files, css, image, etc). To run one of our html5/canvas games locally for people. All packed into a single exe file. I have "test_load.htm" set as embedded into my project.

Am I getting close with this? This code is all on Form1.vb:

Quote:

Imports EO.WebBrowser

Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

'EO.WebBrowser.Runtime.RegisterCustomSchemes("sample", "test")

WebControl1.WebView.RegisterResourceHandler(New SampleHandler())

Dim url As String = SampleHandler.EmbeddedPageUrl

WebControl1.WebView.Url = url

End Sub
End Class


Friend Class SampleHandler
Inherits ResourceHandler
Public Const SampleUrlPrefix As String = "sample://"
Public Const EmbeddedPageUrl As String = "sample://test_load/index.html"

Public Overrides Function Match(context As ResourceHandlerContext) As Boolean
'Return true if the request Url matches our scheme. In that
'case ProcessRequest will be called to handle the request
Return context.Request.Url.StartsWith(SampleUrlPrefix, StringComparison.OrdinalIgnoreCase)
End Function

Public Overrides Sub ProcessRequest(context As ResourceHandlerContext)
'Only process EmbeddedPageUrl
If String.Compare(context.Request.Url, EmbeddedPageUrl, True) = 0 Then
'Set content type
context.Response.WriteResource(GetType(SampleHandler).Assembly, "test_load.htm", "text/html")
context.Response.ContentType = "text/html"
End If
End Sub
End Class


It runs - then I get an error:

System.NullReferenceException: 'Object reference not set to an instance of an object.' relating to this line:
context.Response.WriteResource(GetType(SampleHandler).Assembly, "test_load.htm", "text/html")

If I then continue to run the project - it then downloads an empty index.html file.

Thanks so much for the help!
eo_support
Posted: Thursday, February 4, 2021 10:03:37 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
Hi,

Your code looks fine. Most likely it has to do with the path of the resource ("test_load.htm") not being correct. Inside WriteResource we use the following code to load the resource:

Code: C#
//Arguments:
//assembly - the first argument you pass to WriteResource
//resourceName - the second argument you pass to WriteResource
stream = assembly.GetManifestResourceStream(resourceName);


If for whatever reason, the resource identified by resourceName can not be found in assembly, then the returned value stream will be null, and you will get the NullReferenceException. So you can use the above code to test if you have embedded the resource and have the resource name correctly.

Thanks!
cocoMonkey
Posted: Thursday, February 4, 2021 10:54:28 AM
Rank: Newbie
Groups: Member

Joined: 2/3/2021
Posts: 6
Thank you so much for the reply!

I tried adding this line:

Quote:
Dim stream As Stream =
GetType(SampleHandler).Assembly.GetManifestResourceStream(
"EmbeddedPage.htm")


And it throws no errors - it still throws an exception for this line:

context.Response.WriteResource(GetType(SampleHandler).Assembly, "EmbeddedPage.htm", "text/html")

Just to check - to use "EmbeddedPage.htm" - I right-clicked on my project title, selected "Add" > "Existing Item", selected the htm file, then in the file properties - selected "Build Action" : "Embedded Resource"


Is there anything else I need to do in my project to make this file usable?

Thanks again :)
eo_support
Posted: Thursday, February 4, 2021 11:01:09 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
Hi,

The call to Assembly.GetManifestResourceStream is NOT to fix the problem. The code is for you to test whether you have the resource name correctly. You need to check the return value (your "stream" variable). If it is null, then you have a problem with your resource name.

Thanks!
cocoMonkey
Posted: Thursday, February 4, 2021 11:05:38 AM
Rank: Newbie
Groups: Member

Joined: 2/3/2021
Posts: 6
Sorry - I assumed that call would also throw an error if the resource could not be found. But it would just be valued as “null”. I understand now. Thank you.

I will try this soon.

If the resource is null - and I have included the file as I mentioned in my last post, how would I include the html file correctly?

Thanks again for the replies and help.
eo_support
Posted: Thursday, February 4, 2021 3:57:00 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
Hi,

The steps you described is correct. So I am not sure what went wrong on your side. One simply way to check this is to use a .NET dissembler (such as Reflector) to examine the assembly. It should show you the full path of the resource under "Resources" section. You can then make sure you pass exactly the same resource name to WriteResource. This particular part (embed a resource into an assembly and then load it through code) doesn't really have anything to do with our product. So if you continue to have problem, you may want to search online to see if you can find more information.

Thanks!
cocoMonkey
Posted: Friday, February 5, 2021 7:28:19 AM
Rank: Newbie
Groups: Member

Joined: 2/3/2021
Posts: 6
Thanks for all the help - as i said in my first post - I'm completely new to vb.net (or anything other then PHP and JavaScript!) So the answer was as you all said - the reference was wrong, it needed to be:

context.Response.WriteResource(GetType(SampleHandler).Assembly, "WebViewApp2.EmbeddedPage.htm", "text/html")

In your tabbed browser - you did not need the namespace in the path - but in my code it did!

So thanks again for the help! :)
eo_support
Posted: Friday, February 5, 2021 11:37:44 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
Great. Glad to hear that you got it working!
cocoMonkey
Posted: Friday, February 5, 2021 11:58:44 AM
Rank: Newbie
Groups: Member

Joined: 2/3/2021
Posts: 6
Hi,

Can i request some help for the following...

In my embedded page - how can I use standard http requests to other embedded files?

So In EmbeddedPage.htm

How can I do this:

<script src="jquery.min.js"></script>


It works when I do this:

<script src="sample://jquery.min.js"></script>

BUT I get the following error - as it is not using standard http requests...

Uncaught DOMException: Failed to execute 'open' on 'XMLHttpRequest': Invalid URL

Thanks again!

eo_support
Posted: Friday, February 5, 2021 12:07:37 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
Hi,

You can't use XMLHttpRequest over a custom protocol scheme. XMLHttpRequest by definition is HTTP/HTTPS only. It won't work over any other protocol. If you must support this, your only option is to have a built-in mini Web server.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.