Welcome Guest Search | Active Topics | Sign In | Register

C# samples Options
Walter Grimm
Posted: Saturday, December 11, 2021 4:04:04 AM
Rank: Newbie
Groups: Member

Joined: 12/10/2021
Posts: 6
HI,

I installed the wpf webbrowser with nuget, works after referencing, will check how nuget updates will deal with it.

I than downloaded the install package and looked at the samples and the API section. The sample as one tabbed with the control since, the API shows the definitions.

I am looking for a browser Net object that can interact with the c#, which basically means the following with code behind: (i have multiple DT, and have a sections for payment and project management, where I just want to use one single HTML/JS page for all applications)

1. opening a webpage
2. accessing the WebControl and reading JS variables
3. accessing the WebControl and writing JS variables
4. accessing the WebControl and executing functions inside the loaded file, or external loaded JS functions
5. accessing the WebControl and manipulating the HTML DOM

Since I am not the great IT expert, and don't want to play around for hours figuring stuff out.
I have searched for samples but could find NON, besides one stackoverflow question.

I am sure for someone that has been developing this since years, that it should be a piece of cake to write 10+ c# samples from hello world to more complex stuff and put them on GIT

thanx

Walter
eo_support
Posted: Saturday, December 11, 2021 11:59:38 AM
Rank: Administration
Groups: Administration

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

For loading a page, you simply set the webView's Url property. For example, you can do:

Code: C#
webControl1.WebView.Url = "https://yoursite.com";


For anything has to do with things inside the page, you would use JavaScript. Anything inside the page lives inside the browser engine. That is a different "world" and you would use that world's language - JavaScript. For example, if you want to set a global JavaScript variable, the JavaScript code would be:

Code: JavaScript
window.someVariable = 1234;


If you wish to change the web page's background color to blue, you can use JavaScript code like this:

Code: C#
document.body.style.backgroundColor = "blue";


If you wish to set a textbox's value:

Code: JavaScript
document.getElementById("the_id_of_the_textbox").value = "123";


Note that we are not in a position to provide support on JavaScript code, these are just examples to show you the idea.

Once you have the correct JavaScript code, you would pass the code to the browser engine through WebView.EvalScript. So while the JavaScript code to set global variable someVariable to 1234 is

Code: JavaScript
window.someVariable = 1234;


The C# code would be:

Code: C#
webControl1.WebView.EvalScript("window.someVariable = 1234;");


If you wish to get the value of the variable, you would do:

Code: C#
int n = (int)webControl1.WebView.EvalScript("window.someVariable");


Note that this code assumes someVariable holds a integer value. If it holds another type of value (such as a string), the cast will fail in the above line.

So to sum it up, you write the JavaScript code to do whatever you need to do, then you use WebView.EvalScript to run it. You can find more information about this here:

https://www.essentialobjects.com/doc/webbrowser/advanced/js

Hope this points you to the right direction.

Thanks!
Walter Grimm
Posted: Monday, December 13, 2021 8:11:37 AM
Rank: Newbie
Groups: Member

Joined: 12/10/2021
Posts: 6
HI,

thanxs, shows clear how to get var in and out from C# hst, did not chekc the advanced topics, was focussing on the samples.

I assume that calling functions inside the webview from c# also works in the same manner,

thanx

Walter
eo_support
Posted: Monday, December 13, 2021 9:16:21 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
That's correct. For example, to call JavaScript function alert you can do EvalScript("alert('hi');");


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.