четверг, 27 марта 2008 г.

Xaml Viewer

Here I am again trying to translate my thoughts.

In my previous post I've mentioned the complex case in localization caused by the WPF document API formatted text in TextBlock. For example:






-, that in Russian looks like this:






There is no possibilities to use markup extension in document objects (such as Run or Bold etc.), because it will raise exceptions in XamlParser. The reason is simple - these document objects are not parts of a visual tree. Therefore I need another way to localize and render this text, so I've decided to write custom control for rendering loose-xaml in run-time and keep this localizable xaml same way as I keep string resources - in resx file and obtain them with the CultureResource markup extension.

Well, there is absolutely nothing complex in this control, and I am mentioning it only in the scope of localizing formatted text scenario.

It works in the following way:

  • control has 2 states: Host (this is our case - static rendering of bounded xaml) and Edit (in this state you can edit xaml and see render output changes in runtime, there will be an edit box in the upper half for xaml editing);
  • it has a string property Xaml;
  • control parses xaml (by means of XamlReader) and render it in its Content property;
  • Content is binded to internal control part - ScrollViewer, so in case your render output is bigger then the control area, you'll see a scrollbar;
The edit mode control template:



The Host mode control template:

In case of exceptions in xaml parsing process (the most propable is XamlParseException) control will catch them and show the exception message in its content area. This process is governed by a trigger in default style.

The error content style:



This is the way Xaml looks in the resources table:



And this is the way our scenario looks like an assembled one:



Disadvantage is lack of xaml formating (since it is just a string property, it can be nonformatted xml - very annoying!).

Good luck!

Комментариев нет: