简体   繁体   中英

XamlReader throws when loading generic.xaml to merge resource dictionaries

Trying to load generic.xaml in code but it throws a XamlParseException. Code as follows:

Uri uri = new Uri("Themes/Generic.xaml", UriKind.Relative);
StreamResourceInfo info = Application.GetResourceStream(uri);
System.Windows.Markup.XamlReader reader = new System.Windows.Markup.XamlReader();

ResourceDictionary resdict = (ResourceDictionary)reader.LoadAsync(info.Stream);

this.Resources.MergedDictionaries.Add(resdict);

The idea is to merge a resource dictionary in a basepage. Then derived pages can use styles, colors, brushes, etc from their base class by using {StaticResource DarkBrush} for example.

But the above code throws:

'', hexadecimal value 0x0C, is an invalid character. Line 1, position 1.

The generic.xaml file was created in VS2010 the standard way. Tried to set Build Action to Resource but that didn't work either...

I got the code sample from Microsoft. There it was used to load a page. Any help would be greatly appreciated.

        Uri uri = new Uri("Themes/Generic.xaml", UriKind.RelativeOrAbsolute);
        var resDict = Application.LoadComponent(uri) as ResourceDictionary;
        this.Resources.MergedDictionaries.Add(resDict);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM