문제

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top