Skip to content

Developing Microsoft .NET Applications for Visual Localization with Alchemy Catalyst

November 1, 2012

Alchemy Catalyst’s visual view regularly calls on the Microsoft .NET Framework to create instances of complex .NET Forms and User Controls. The .NET Framework is sometimes unable to create these objects. This results in Catalyst performing a manual parse of these .NET resources so they can be rendered in Visual View. In a small number of cases, this can result in Forms or User Controls that don’t appear exactly as they would at runtime in the parent application. While localizable content is made available, missing User Interface components can sometimes lead to loss of context during the translation process.

Often the Framework is unable to create a Form or User Control because the constructor for one of these U.I. containers fails. By ‘fails’ it is meant that the constructor for the object throws an exception and this exception is unhandled by the object that raised it.

This causes unhandled exceptions in Catalyst and normally prevents a .NET Form from being correctly rendered.  There are a number of suggestions for avoiding this situation.  These are outlined below.

Try-Catch Blocks

Avoid unhandled exceptions: Use a ‘try-catch’ block around code in the constructor that may throw an exception.

Sample Code Comment
MyClass::MyClass(){  InitializeComponents();  OpenFile(“c:\Info.dat”);}  If a constructor refers to a file or a database that may not exist in the localization environment.  The constructor may fail.  Catalyst will not be able to visually render this form if the constructor fails.
MyClass::MyClass(){  try  {           InitializeComponents();  OpenFile(“c:\Info.dat”);

}

catch(Exception e)

{

}

}

 

A simple try-catch block such as this will catch the exception.Even if there is no actual handling code in the catch block, the exception does not bubble up and cause an unhandled exception in Catalyst.In the situation where the file does not exist in the localization environment, Catalyst will still be able to visually construct this form.

Attach Event Handlers

Where appropriate: Attach event handlers to the events…

Application.ThreadException
AppDomain.CurrentDomain.UnhandledException

This will deal with these exceptions. These event handlers would not generally terminate the thread but would allow components of the user interface to continue functioning.  These user interface components may be required by Alchemy Catalyst.

Call InitializeComponents() Early

Wait until after the call to InitializeComponent() before adding more logic to the constructor. The InitializeComponent() method should be complete before any code that is liable to throw an exception is added.

Sample Code Comment
MyClass::MyClass(){  OpenFile(“c:\Info.dat”);  InitializeComponents();}

 

If the constructor fails before InitializeComponents() is called then no user interface components will be initialized.
MyClass::MyClass(){  try  {   InitializeComponents();

OpenFile(“c:\Info.dat”);

}

catch(Exception e)

{

}

}

 

Simply placing any complicated logic after the InitializeComponents() call will ensure that the UI components are initialized before any other code may fail.

Avoid resources that may not be present

If possible, try to avoid connecting to data sources or resources that may not exist in certain environments.  When an application is installed on a client’s machine, it may be that a certain file or a database is always present (the installer may look after this).  However, this may not be the case during localisation.  Try to avoid dependencies such as this in the constructor or InitializeComponent() calls.  This is not always possible and if not, that resource will need to be present during localization.

Public Constructors with no Arguments

For a given Form or User Control class, implement a public constructor, which takes no arguments, if one does not already exist.  Such a constructor is easier for Catalyst to call.

Handle Null Parameters

In certain circumstances Catalyst can ask the .NET Framework to create objects using a constructor which takes parameters; however, when the constructor is called by Catalyst the parameters passed can be null. Ideally, code should check for the possibility of null parameters and handle any resulting exceptions gracefully.

Sample Code Comment
MyClass::MyClass(myFile File){  OpenFile(File);  InitializeComponents();

}

 

When Catalyst calls a constructor that takes a parameter, it may pass in null as the parameter.  Code such as this will throw an exception. An uncaught exception will result in the form not being created.
MyClass::MyClass(myFile File){  try  {

InitializeComponents();

If(File)

OpenFile(File);

}

catch(Exception e)

}

Checking the parameter for validity before use is a good idea and allows the form to be created without difficulty.
Advertisement
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: