Using NetCoreBlockly you can create custom blocks. As an alternative you could “outsource” some functionally to an ASP.NET Controller. In this example, a menu is sent as a CSV list of options to a controller method which returns a single formatted string with each item on a separate line and each item is proceeded by its index (numerical order from 1 in the CSV list) which is used in the menu selection. This can then be used with the NetCoreBlockly block prompt for number with message to prompt for a selection from the list.

To implement this, a new controller was added SoftataMisc with one Get method GetMenu() that takes a string as input and returns a processed string.

        [Route("GetMenu")]
        [HttpGet]
        public string GetMenu(string value)
        {
            var array = value.Split(',');
            string retMsg = "Please select from:\n";
            for (int i = 0; i < array.Length; i++)
            {
                retMsg += $"{i+1}. {array[i]}";
                if(i < array.Length - 1)
                {
                    retMsg += "\n";
                }
            }
            return retMsg;
        }

The string value sent is just a csv list of menu options. A blank string is created. An alternative is to have the first item as the menu title. Each item in the list is then added as a new line with preceded by its index.

This then manifests in the Blockly context as the following block:

The input csvString is attached to right connection point of the block. The returned string to be assigned to is attached on the left typically with aset <variable name> to block..

Using the custom block

This new block was then used in a reusable Blockly function that takes the CSV string as input and returns the user selected index. User inputs are validated against valid selections, must be a valid list index.

Custom Blockly block via API

A sample Blockly Menu test app was created as follows:

The displayed menu

The function was then added to the Categories Loaded file (see previous blog post) so that it can then be used in other Blockly programs.

Comment

This approach of hiving off some complex processing, that could have been done with a lot blocks though, to an ASP.NET controller is simple to implement and reuse. This way some complex computations can be coded in C# and presented in Blockly as a single block!


 TopicSubtopic
  Next: > Azure Pipelines
   
 This Category Links 
Category:Softata Index:Softata
<  Prev:   Softata