|
| 1 | +using System; |
| 2 | +using SharpWebview; |
| 3 | +using SharpWebview.Content; |
| 4 | + |
| 5 | +namespace DesktopApp |
| 6 | +{ |
| 7 | + class Program |
| 8 | + { |
| 9 | + // Set the application to Single Threaded Apartment |
| 10 | + // Otherwise the webview won't work on windows at least |
| 11 | + [STAThread] |
| 12 | + static void Main(string[] args) |
| 13 | + { |
| 14 | + // For a better startup expierience |
| 15 | + // I recommend to init the HostedContent in advance |
| 16 | + var hostedContent = new HostedContent(); |
| 17 | + |
| 18 | + // Wrap the usage of the webview into a using block |
| 19 | + // Otherwise the native window will not get disposed correctly |
| 20 | + using (var webview = new Webview(true)) |
| 21 | + { |
| 22 | + webview |
| 23 | + // Set the title of the application window |
| 24 | + .SetTitle("The Lost Hitchhicker") |
| 25 | + // Set the start size of the window |
| 26 | + .SetSize(1024, 768, WebviewHint.None) |
| 27 | + // Set the minimum size of the window |
| 28 | + .SetSize(800, 600, WebviewHint.Min) |
| 29 | + // Bind a c# function to the webview - Accessible with the name "evalTest" |
| 30 | + .Bind("evalTest", (id, req) => |
| 31 | + { |
| 32 | + // Req contains the parameters of the javascript call |
| 33 | + Console.WriteLine(req); |
| 34 | + // And returns a successful promise result to the javascript function, which executed the 'evalTest' |
| 35 | + webview.Return(id, RPCResult.Success, "{ result: 42 }"); |
| 36 | + }) |
| 37 | + // Navigate to this url on start |
| 38 | + .Navigate(hostedContent) |
| 39 | + // Run the webview loop |
| 40 | + .Run(); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | +} |
0 commit comments