Click here to Skip to main content
15,886,258 members
Articles / Desktop Programming / WPF
Tip/Trick

VB.NET, NeoLua and External XAML Implementation

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
17 Nov 2015CPOL2 min read 9K   168   1   2
Example of loading external scripting and external XAML with VB.NET and NeoLua

Introduction

This is an example of combining VB.NET, NeoLua as external scripting and external XAML. It solve these problems:

  • Loading XAML externally
  • Assigning loaded XAML's DataContext from lua
  • Binding loaded XAML's Events to function in lua
  • Extend the content of a Window

Background

At the time of this writing, I was just starting to use .NET and WPF. Migrating from the web world, I was forced to leave all the dynamicity of the web world behind. At the same time, I was working on a project that took several minutes for each build to complete. It was from there I came up with the idea of combining VB.NET, NeoLua and XamlLoader in order to generate content dynamically.

The idea is actually to create an extendable VB.NET more than scripting the entire system externally although this example does exactly that.

Why Not IronPython or Awesomium or ...?

For IronPython, I chose NeoLua because the implementation is super easy and from what I heard, IronPython requires you to have the Python's Library while NeoLua, you just need the NeoLua libraries. (I might be wrong?)

For Awesomium, the requirement was to use XAML and not HTML.

Drawbacks

  • Lua and XAML have no intellisense.
  • NeoLua Exception is not 100% accurate. (ex, const error will show line and column of [0, 0])
  • Not for strong-typed dependant environment because of the dynamicity.

Using the Code

This code was meant for you to study on it. To give you some hints:

  • The entire NeoLua extension was put under a folder called Engine
  • Adding event from lua requires a help from a function called LuaAddEvent check LuaWindow
  • To make a Window Scriptable, you must inherit from LuaWindow check MainWindow.xaml
  • XAML Content's Root for LuaWindow is Page while for LuaContent is UserControl check Main.xaml and Content1.xaml
  • LuaWindow and LuaContent both load from Scripts and Contents of the same name. (ex: Main.xaml, Main.lua)
  • Both Scripts and Contents folder was copied from the solution upon run
  • External XAML cannot have an x:Class
  • At the time of writing, NeoLua const for generic does not work properly (other than List) Fixed
  • Event uses a Dictionary with Key<Parent, Element, LuaResult> and Value<Delegate>
  • Event's Key.Parent is needed in order to prevent clearing events incorrectly.

History

  • Revision 2 - NeoLua const problem is now fixed.
  • Revision 1 - First release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Malaysia Malaysia
not much... just have a bunch of code in my mind.. and my website http://pyrostrex.blogspot.com though...

Comments and Discussions

 
QuestionIs this a issue Pin
neolithos27-Oct-15 13:40
neolithos27-Oct-15 13:40 
AnswerRe: Is this a issue Pin
PyroStrex27-Oct-15 16:29
PyroStrex27-Oct-15 16:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.