Use Blockly inside a WPF WebBrowser

前端 未结 1 530
眼角桃花
眼角桃花 2021-02-20 07:00

Is it possible to use the Blockly google javascript libraries inside a WPF WebBrowser?

In particular, Blockly needs several js scripts. How can we reference the js libra

1条回答
  •  太阳男子
    2021-02-20 07:19

    Short Answer

    You can use all blocky features including UI tools and API functions from WPF WebBrowser control. To do so you should:

    • Create an HTML content which contains script tags referencing Blocky js, or methods which you want to call from C#, and required HTML and XML contents based on your requirement (for example toolbox and workspace). You can load toolbox and workspace dynamically at runtime.
    • Load content to WebBrowser control using Navigate or NavigateToString
    • If you need to call a script use InvokeScript method of WebBrowser control.

    Also, to be able to use Blocky you should make the WebBrowser use latest document mode without compatibility mode and show modern content.


    Example

    This example shows:

    • How you can load a toolbox dynamically
    • How you can load workspace dynamically
    • How you can call Blocky API methods using javascript methods. In the example you can see showCode and runCode proxy methods which are independent from wprkspace and will work with any workspace. You can call javascript methods from C#.

    You can use either of Blocky Demos for example. I created an example which shows both using Blocky API methods and Blocky UI Tools. This example is based on Generating Javascript example which shows how to use Blocky API to generate a javascript from Blocky workspace.

    Download

    You can clone or download working example from:

    • GitHub repository
    • Download zip file

    Creating Example Step by Step

    The example contains a simple HTML file which in its head tag required javascript files are added. Also it contains two proxy methods which we created to call from C#.

    Also the example contains two xml files. On for Blocky workspace and one for toolbox.

    Note: creating those files is not compulsory and you can create workspace or toolbox dynamically at runtime. It's just to show you can load a workspace and toolbox at run-time and they don't need to be static.

    1) Create WPF Application

    Create a WPF project and name it WpfAppllicatin1.

    2) Create blockyWorkspace.xml File

    Create blockyWorkspace.xml file using below contents. This file will be used to create Blocky workspace.

    
    
        
        
        
            EQ
            
            
                ADD
                
                
                    6
                
                
                
                
                    7
                
                
            
            
            
            
                13
            
            
        
        
        
        
            
            
                Don't panic
            
            
        
        
        
        
            
            
                Panic
            
            
        
        
    
    
    

    3) Create blockyToolbox.xml File

    Create blockyToolbox.xml file using below contents. This file will be used to create Blocky toolbox.

    
        
        
        
        
        
        
        
    
    

    4) Create blockyHTML.html File

    Create blockyHTML.html file using below contents. This file just contains reference to Blocky scripts and also our javascript methods which will be called from our application using C# code:

    
    
      
      
      
      
      
    
    
        

    5) Write C# Code

    Put a WebBrowser control and name it browser and handle its LoadCompleted event. Also put two Button controls on windows and name them showCodeButton and runCodeButton and handle their Click events like this:

    public MainWindow()
    {
        InitializeComponent();
        showCodeButton.IsEnabled = false;
        runCodeButton.IsEnabled = false;
        browser.NavigateToString(System.IO.File.ReadAllText(@"d:\blockyHTML.html"));
    }
    private void browser_LoadCompleted(object sender, NavigationEventArgs e)
    {
        showCodeButton.IsEnabled = true;
        runCodeButton.IsEnabled = true;
        var toolboxXML = System.IO.File.ReadAllText(@"d:\blockyToolbox.xml");
        var workspaceXML = System.IO.File.ReadAllText(@"d:\blockyWorkspace.xml");
        //Initialize blocky using toolbox and workspace
        browser.InvokeScript("init", new object[] { toolboxXML, workspaceXML });
    }
    private void showCodeButton_Click(object sender, RoutedEventArgs e)
    {
        var result = browser.InvokeScript("showCode", new object[] { });
        MessageBox.Show(result.ToString());
    }
    private void runCodeButton_Click(object sender, RoutedEventArgs e)
    {
        browser.InvokeScript("runCode", new object[] { });
    }
    

    6) Run Application

    When you run the application, after the buttons got enabled, click on first button and then you can get the result of showCode method which uses blocky API to generate javascript code from blocky workspace.

    Also you can run the code which you created using blocky by click on the second button.

    0 讨论(0)
提交回复
热议问题