예제 #1
0
        static void Main(string[] args)
        {
            // For a better startup expierience
            // I recommend to init the HostedContent in advance
            var hostedContent = new HostedContent();

            // Wrap the usage of the webview into a using block
            // Otherwise the native window will not get disposed correctly.
            // By setting the second parameter to true, sharpWebview intercepts
            // all external links and opens them in the system browser.
            using (var webview = new Webview(true, true))
            {
                webview
                // Set the title of the application window
                .SetTitle("The Lost Hitchhicker")
                // Set the start size of the window
                .SetSize(1024, 768, WebviewHint.None)
                // Set the minimum size of the window
                .SetSize(800, 600, WebviewHint.Min)
                // Bind a c# function to the webview - Accessible with the name "evalTest"
                .Bind("evalTest", (id, req) =>
                {
                    // Req contains the parameters of the javascript call
                    Console.WriteLine(req);
                    // And returns a successful promise result to the javascript function, which executed the 'evalTest'
                    webview.Return(id, RPCResult.Success, "{ result: 42 }");
                })
                // Navigate to this url on start
                .Navigate(hostedContent)
                // Run the webview loop
                .Run();
            }
        }
예제 #2
0
 static void Main(string[] args)
 {
     // Wrap the usage of the webview into a using block
     // Otherwise the native window will not get disposed correctly
     using (var webview = new Webview(true))
     {
         webview
         // Set the title of the application window
         .SetTitle("The Hitchhicker")
         // Set the start size of the window
         .SetSize(1024, 768, WebviewHint.None)
         // Set the minimum size of the window
         .SetSize(800, 600, WebviewHint.Min)
         // This script gets executed after navigating to the url
         .InitScript("window.x = 42;")
         // Bind a c# function to the webview - Accessible with the name "evalTest"
         .Bind("evalTest", (id, req) =>
         {
             // Executes the javascript on the webview
             webview.Evaluate("console.log('The anwser is ' + window.x);");
             // And returns a successful promise result to the javascript function, which executed the 'evalTest'
             webview.Return(id, RPCResult.Success, "{ result: 'We always knew it!' }");
         })
         // Navigate to this url on start
         .Navigate(new UrlContent("https://en.wikipedia.org/wiki/The_Hitchhiker%27s_Guide_to_the_Galaxy_(novel)"))
         // Run the webview loop
         .Run();
     }
 }
예제 #3
0
 public static void OpenInLine(string address)
 {
     Console.WriteLine($"Try to view blazor application on {address}");
     using (var webview = new Webview())
     {
         webview
         .SetTitle("Blazor in webview_csharp")
         .SetSize(1024, 768, WebviewHint.None)
         .SetSize(800, 600, WebviewHint.Min)
         .Navigate(new UrlContent(address))
         .Run();
     }
 }
예제 #4
0
        public void Run(FileInfo model)
        {
            var is3MF = model?.Extension.ToUpper() == ".3MF";

            if (model == null || is3MF)
            {
                // System.CommandLine parses and executes the CLI arguments
                // in a new Task. Because of this, it is necessary to execute
                // a new STA Thread to run the webview. Will throw exceptions otherwise.
                Thread thread = new Thread(() =>
                {
                    var initScript =
                        @"window.printProject = {};
                        window.printProject.dropCallback = dropCallback;";
                    if (is3MF)
                    {
                        ClearCreateExtractionPath();
                        var model3mf = new Model3MF(model.FullName);
                        model3mf.ExtractPrintProject(_extractionPath);
                        initScript += "window.printProject.projectFolderUrl='/extracted';";
                    }

                    var hostedContent = new HostedContent();
                    using (_webview = new Webview(false, true))
                    {
                        _webview
                        .SetTitle("3D2P")
                        .SetSize(1024, 800, WebviewHint.None)
                        .SetSize(800, 800, WebviewHint.Min)
                        .Bind("dropCallback", DropCallback)
                        .InitScript(initScript)
                        .Navigate(hostedContent)
                        .Run();
                    }
                });
                #if Windows
                thread.SetApartmentState(ApartmentState.STA);
                #endif
                thread.Start();
                thread.Join();
            }
            else
            {
                Console.WriteLine("This is a 3MF viewer. Please provide a '.3mf' file! Or use '-h' for help.");
            }
        }