Current Conversion and required manual changes to enable the control templates

This section describes the actions required to activate the extracted control templates

Introduction

Control Templates are a difficult topic to move to Angular. It seems that there is no direct equivalent for it. This Silverlight feature allows the developer to replace the way to render any control without modifying its implementation.

Source code

Here you can download the source code that is used across the example.

The Silverlight example

Given the following example definition:

<Grid x:Name="LayoutRoot" Background="White">
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <TextBlock HorizontalAlignment="Center" Grid.Column="0">Using Control Template</TextBlock>
        <Button Name="MyTemplatedButton" Grid.Column="0" Style="{StaticResource MyCustomButtonStyle}" Click="MyTemplatedButton_Click" ></Button>
        <TextBlock HorizontalAlignment="Center" Grid.Column="1">Not Using Control Template</TextBlock>
        <Button Name="MyNormalButton" Grid.Column="1" Width="80" Height="30" Content="Click Me!" Click="MyNormalButton_Click"></Button>
    </Grid>
</UserControl>

In the previous Xaml code, you can find the definition of two buttons, however, one of the button's appearance is being changed by using the Template property (System.Windows.Controls.Control.Template property) defined inside a custom style. This property is usually (but not always) specified in a Setter of a Style in a resource dictionary.

The following code creates a custom style and defines the template by setting an instance of a Control Template.

The previous code Renders:

Render of the application by using a Control Template to change the appearance of the left button

Migrated Application without the Control Template Activation

If you run the migrated application before activating the Control Template, it will render:

Migrated applicaiton without activating the Control Template

Activating the control template

By default, the control templates are not active in the migrated code. When the migration tool converts the code, the Control Templates elements found in Xaml are extracted into new Angular components. These components are not registered in the module of its library. The reason for not registering them is that trying to make them work is not trivial and several different/specific cases have been found.

In the previous Silverlight example, there is a Control Template defined inside a custom style, and the style is being used in the first button definition of the MainPage. In the Angular/HTML template of the MainPage, you can see that the Resource dictionary that contains the custom style, and the control template are not included.

As with the control template, the Resource Dictionary is extracted. In this case, the dictionary is moved to the model of the MainPage component while the control template is extracted as a different component. You can see the files created for the extracted control template in the following image:

Files generated for the extracet contro template

And here is the resulting Angular/HTML template of the template found in the "MainPage"

And the component definition:

The first step to enabling the control template is to register the component in the library or application module where it is defined (app.module.ts or lib.module.ts). Following the example included in this guide, you need to add MysCustomButtonStyleCT to the app.module.ts file.

The entire file after adding the MyCustomButtonStyleCT will look as follows:

Now that the component is registered, you can go to the converted resource dictionary where the control template was defined (in this case, the resource dictionary was extracted in the MainPage component model).

You can see in the following style definition (defined in MainPage.ts model class), that the setter for the Template property was generated but the value it's initialized to null.

And to enable the control template, you need to add the following initialization instead of the initialization to null:

The control template initialization from the previous line of code must be added to the setter for the "Template" property.

It's important to add the import for ControlTemplate and MyCustomButtonStyleCTModel when adding the control template initialization. The final version of the component model after adding the Control Template initialization will be:

Now you can rebuild and run the application again to see how the application renders the buttons with the control template enabled:

Migrated application after activating the control template

Resources

Silverlight Application

32KB
archive
Open
Silverlight application code

Angular Migrated Application

Angular migrated application without the control template changes
Angular migrated application with the control template changes (manual changes to enable the control template)

Last updated

Was this helpful?