Manually parse a string as an XAML attribute

How does XAML Parser convert the string "Red" to Foreground = "Red" in SolidColorBrush? Despite the fact that I know that types have System.ComponentModel.TypeConverter, I believe that the WPF XAML parser usually uses those that convert the string to a brush. Are there any XAML APIs besides XamlReader.Load (which requires a valid xml string) that I could use to parse a single string, as if it were in an attribute for a specific property?

+3
source share
2 answers

The XAML parser (for WPF) does use a type converter of the specified property or property. There are several hardcoded abbreviations, but they are intended for performance and do not change semantics. A parser, simply using attribute information, can duplicate the semantics of parsing (which, for example, does Blend and Cider).

There is no API that will convert a value in the same way that XAML will, mainly because many types of conversions work only in the context of XAML analysis. For example, type converters can refer to namespaces defined in the XAML file (which changes depending on where this value is located in the XML file), as well as the URI database of the external database database for this file. They really only apply when parsing the XAML file.

Converter. .

0

, . XamlReader ( , ). TypeConverter .

EDIT, , SolidColorBrush:

var colorString = ...;
var converter = new System.Windows.Media.BrushConverter();
var brush = (SolidColorBrush)converter.ConvertFromString(colorString);

SolidColorBrush .NET Reflector, , , XamlReader, API- . , , .

-1

Source: https://habr.com/ru/post/1714057/


All Articles