-1

I have seen a few applications do this. They use HTML/CSS/Javascript for the styling (which is a million times easier than creating the same thing natively), but they have the logic of the program in C#, I have no idea how to interconnect the two. I was trying to google around, but I couldn't find anything with my phrasing. Any quick explanation or example would be awesome!

Thanks, Jon

EDIT: As clarification (sorry) I mean a desktop application built with something like C# or C++ or something of the like

Dibesjr
  • 496
  • 3
  • 12

2 Answers2

2

First create a ComVisible class

[ComVisible(true)]
public class JSObject
{
    public void ShowMessage(string s)
    {
        MessageBox.Show(s);
    }
}

Then set WebBrowser control's ObjectForScripting property

webBrowser1.ObjectForScripting = new JSObject();

Set html content

webBrowser1.DocumentText = @"
                      <script>
                          external.ShowMessage('test'); 
                          function ShowIt(s){alert(s);} 
                      </script>";

and call in Javascript C#'s ShowMessage method

And this the the other way,,

webBrowser1.DocumentCompleted += (s, e) =>
    {
          webBrowser1.Document.InvokeScript("ShowIt",new object[]{"test2"});
    };
L.B
  • 114,136
  • 19
  • 178
  • 224
  • I fail to see how this is a viable solution. How is anyone supposed to create a professional application using such a rudimentary technique? javascript as a `string`? Are you serious? method names as strings too? and parameters? – Federico Berasategui Sep 27 '13 at 18:37
  • @HighCore Whether you like it or not. As OP requested, It shows the interaction between javascript and c# . – L.B Sep 27 '13 at 19:01
0

What you're looking for is called WPF.

It uses XAML for the UI and styling, while allowing you to create the application logic in pure C# or any .Net language.

it is the replacement of winforms, which is a really old technology no one cares about anymore, and which doesn't support anything.

WPF brings the flexibility and declarative paradigm of the Web into the Windows Desktop.

It goes hand and hand with a new development paradigm called MVVM, which you need to learn if you want to create Professional Looking, Awesome Windows Applications

Don't try to hack something together by trying to put a WebBrowser or anything like that in a Windows Application. The Web is the Web and Desktop is Desktop.

Community
  • 1
  • 1
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154