How to Create Extensions for CSHTML5

 

Note: this feature requires the Professional Edition or higher

 

General Concepts

  • An "extension for CSHTML5" is a DLL that you can reference in your CSHTML5 project in order to extend the features of CSHTML5.
  • To create such a DLL with Visual Studio, all you have to do is click "File -> New -> Project" -> C#/XAML for HTML5", and choose "Empty Class Library (C#/XAML for HTML5)"
  • Your extension is written in C#/XAML just like any other CSHTML5 project. However, if you want, you can also use JavaScript and CSS code (read How to call JavaScript from C#).
  • When you compile that project, it produces a .NET assembly (DLL) that you can easily redistribute.
  • One of the places where you can distribute your extension is on the Extensions and Plugins section of the CSHTML5 forums. It is a good place to quickly find end-users to try out your extension.
  • The DLL is converted into pure JavaScript only at the very end, when the final application of the end-user is generated.

     

Examples of Extensions, Source Code, and How they work

Extension Source Code How it works
WebSockets extension Link It wraps the native JavaScript "WebSocket" class into a C#-based class that can be consumed by CSHTML5 applications.
Print extension Link It extracts the HTML DOM element that corresponds to the specified FrameworkElement, copies it into a new browser window, and then calls the browser Print command on the new window.
File Open Dialog extension Link It adds the <input type='file'> tag to the HTML DOM, and listens to the JavaScript "Change" event to return a JS blob or text.
File Save extension Link It wraps the open-source JavaScript "FileSaver.js" library into a C#/XAML class for consumption by CSHTML5-based apps.
Unofficial ArcGIS Mapping Control for CSHTML5 Link It wraps the JavaScript-based version of the ArcGIS mapping library into a C#/XAML-based control that can be consumed from pure C#/XAML code.
ZIP Compression extension Link It wraps the open-source JavaScript "JSZip.js" library into a C#/XAML class that mimics the method signatures of Ionic.Zip (DotNetZip).

Many more open-source extensions can be found on the Extensions Gallery page.

 

How to Call JavaScript from C#, or embed JS/CSS files in your C#-based extension

Read the page How to call JavaScript from C# to see the answer to those questions.

 

How to Publish Your Extension

To publish your extension, just distribute the DLL that is in the "bin/Debug" subfolder of your CSHTML5 class library folder (refer to the "General Concepts" section above for details). End-users will be able to use it by clicking "Add Reference" from the Solution Explorer.

You can distribute your extension by posting a message on the Extensions and Plugins section of the forums. It is a good place to quickly find end-users to try out your extension. To do so, just compress your DLL into a ZIP archive and send it as an attachment to a new forum post.

Please contact us if you need more information about how to publish an extension.

 

About "This feature is not yet supported" compilation errors

While creating an application or an extension with CSHTML5, you may encounter compilation errors that say that Feature X is not yet supported. This happens only with features that exist in MSCORLIB (.NET Core) and not in CSHTML5. It does not happen with missing features that exist elsewhere in the .NET Framework, because the error message is different in that case.

The JavaScript implementation of MSCORLIB is provided by the open-source project JSIL. Only a subset of MSCORLIB has been implemented. You can see the JavaScript files that contain the MSCORLIB implementation by opening the following folder on your computer:
C:\Program Files (x86)\MSBuild\CSharpXamlForHtml5\InternalStuff\Libraries

To implement missing MSCORLIB features, you have 2 possibilities: either implement the missing features by working directly on the JSIL solution, or implement the missing features by modifying the JSIL files that are contained inside CSHTML5. At the end, the result is the same, because any contribution you make to JSIL will be merged by us into CSHTML5.

  • If you decide to make the contribution by working with the JSIL solution (recommended), here is how it works: first, you create a fork of JSIL from the JSIL GitHub repository (you will find the instructions in the wiki section of the said repository). Then, you compile the solution, implement the missing MSCORLIB features by modifying the Library files contained in the solution, you test by writing simple C# test code (no XAML because JSIL does not use XAML), and then submit your contribution to the JSIL team.
  • Alternatively, you can modify the JSIL files directly from CSHTML5. To do so, please read How to implement mscorlib methods?

 

Related Topics

 

Contact Us

Please click here for contact information.