Pergunta

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.

Foi útil?

Solução

        Uri uri = new Uri("Themes/Generic.xaml", UriKind.RelativeOrAbsolute);
        var resDict = Application.LoadComponent(uri) as ResourceDictionary;
        this.Resources.MergedDictionaries.Add(resDict);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top