C# WPF host computer development (software development of embedded virtual machine)

[Disclaimer: All rights reserved. Reprinting is welcome. Please do not use it for commercial purposes. Contact email: [email protected]]

        Students who have studied halcon know that it not only has many image operators that can be used, but also is very convenient to debug. The corresponding display effect can be seen for the debugging results of each step. In the meantime, developers can adjust parameters and write scripts by themselves, which is very convenient. After all configurations are ok, export it directly to c or c# code, turn it into the final software output, and deploy it to the customer's computer.

        In fact, it is not very complicated to do this. A relatively simple method is to embed a Lua virtual machine and convert the corresponding functions into the Lua version. After the configuration is completed, just package the Lua interpreter and the corresponding algorithm dll directly.

1. Install the Lua development package

        Currently, the corresponding lua package can be downloaded directly through NuGet. The current software is called NLua. Enter NLua on NuGet to search directly. The first displayed result is the object we need to download. Just download it directly.

2. Design interface

        The design of the interface part is relatively simple, with two main parts, one is the button and the other is the label. The main purpose of the label is to display the results of Lua operations. There are two more points here. One is that C# calls the Lua program, and the other is that Lua calls the C# program. These two parts are scenes that often appear later.

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button Content="Button" HorizontalAlignment="Left" Margin="235,178,0,0" VerticalAlignment="Top" Width="124" Click="Button_Click" Height="57"/>
        <Label x:Name="demoLabel" FontSize="20" Content="Test" HorizontalAlignment="Left" VerticalContentAlignment="Center" Margin="430,178,0,0" VerticalAlignment="Top" Height="57" Width="220"/>
    </Grid>
</Window>

3. The header file references NLua

        Header file application, this is the basic operation. The content is relatively simple, just add it using using.

using NLua;

4. c# calls ua

        When C# calls Lua, it usually creates a Lua virtual machine first, and then uses this virtual machine to execute Lua scripts or Lua strings. After the execution is completed, the execution results are returned to c# again.

        // init variable here
        private Lua lua;

        public MainWindow()
        {
            InitializeComponent();

            // initialize a lua translator
            lua = new Lua();

            //execute lua code
            lua.DoString("output = math.max(2,1)");
            int result = Convert.ToInt32(lua["output"]);
            
            demoLabel.Content = "Result from C#: " + result;
        }

5. Lua calls c#

        It’s not too complicated to call c# from lua. First, we prepare a class and matching functions.

    // third part class
    public class MyCSharpClass
    {
        public int Add(int a, int b)
        {
            return a + b;
        }
    }

        Next, if you need to execute this function, the first step is to register the corresponding class. After registration is completed, the second step is to call the corresponding function in the Lua script.

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // add class to lua
            lua["myCSharpObject"] = new MyCSharpClass();

            // execute lua code
            lua.DoString("result = myCSharpObject:Add(10, 5)");
            int result = Convert.ToInt32(lua["result"]);
            
            demoLabel.Content= "Result from C#: " + result;
        }

        In order to check whether the Add function is really called by lua.DoString, you can set a breakpoint at the location of the Add function so that you can double check it.

Guess you like

Origin blog.csdn.net/feixiaoxing/article/details/134967539