Migrate To MahApps.Metro 2.x

This article shows the migration steps from MahApps 1.3.0 to 2.4.3 for BurageSnap, a screen capture tool. BurageSnap uses MahApps to realize its small footprint, as shown below.

Adapt Theme

I first changed the theme specifications as explained in the official document Migration to v2.0.

Old New

Adapt Style

Then I had to fix four errors in the following style settings. MetroWindow in the 2.x doesn't have the properties.

<Style x:Key="WindowStyle" TargetType="mah:MetroWindow">
    <Setter Property="TitlebarHeight" Value="24"/>
    <Setter Property="TitleCaps" Value="False"/>
    <Setter Property="WindowMinButtonStyle" Value="{StaticResource WindowButtonStyle}"/>
    <Setter Property="WindowCloseButtonStyle" Value="{StaticResource WindowButtonStyle}"/>

Window Title

TitlebarHeight was just superseded by TitleBarHeight. TitleCaps was replaced by TitleCharacterCasing but having a CharcterCasing type value. I modified these settings as follows.

<Setter Property="TitleBarHeight"Value="24">
<Setter Property="TitleCharacterCasing" Value="Normal"/>

Window Buttons

I used the style properties WindowMinButtonStyle and WindowCloseButtonStyle to make the minimize and close buttons small. But these were eliminated in the 2.x.

Instead, WindowButtonCommands inside MetroWindow has the corresponding properties, LightMinButtonStyle and LightCloseButtonStyle in case of light themes. When defining a style inside another style, we can use Style.Resources as follows.

Button Labels

There were other errors in the following ButtonStyle to disable capitalization and boldness of the button labels in the default style. There aren't the MetroButton style and the ButtonHelper class in the 2.x.

<Style x:Key="ButtonStyle" TargetType="Button" BasedOn="{StaticResource MetroButton}">
    <Setter Property="mah:ButtonHelper.PreserveTextCase" Value="True"/>
    <Setter Property="FontWeight" Value="Normal"/>

These replacements are MahApps.Styles.Button and ControlsHelper respectively. ControlsHelper, however, have ContentCharacterCasing not PreserveTextCase. Those errors were fixed as follows.

Next Step

That's all of the migration. Next, I had to migrate Prism from 6.2 to 8.0. MahApps.Metro 2.x is incompatible with the older versions of Prism. Because XamlBehaviors.WPF, the 2.x depends on, conflicts with Windows.Interactivity, the older Prism depends on. I will show the migration steps in another article.

Quite Simple Memory Pool in C#

This article shows a quite simple memory pool to make a thread-unsafe library thread-safe without performance degradation in single-threaded programs. Same as the previous article, this article is about DynaJson.

Thread-safety requires overhead to allocate an instance by each invocation to isolate data being altered. Unless thread-safety is required, we can use a static class or a singleton to eliminate any additional allocation.

Continue reading "Quite Simple Memory Pool in C#"