1

I always thought that the generic.xaml styles are merged into the Application.Current.Resources, but they aren't.

Where are the generic.xaml styles stored?

How are they accessable in code? (not xaml)

UPDATE: I know the general c# syntax to access explicit or implicit styles in a ResourceDictionary. This question is only about the styles of the /Themes/Generic.xaml for templated controls.

0

3 Answers 3

4

You can not access the resource in generic.xaml in your app directly from the (Program Files)\Windows Kits\10\ DesignTime\CommonConfiguration\Neutral\UAP\\Generic folder, they aren't copied into your apps so they are not part of your app and your app can not use the resource in the generic.xaml directly as the general resource dictionaries.

The Windows Runtime doesn't use these physical files(including the generic.xaml) for runtime lookup. That's why they are specifically in a DesignTime folder, and they aren't copied into apps by default. Instead, these resource dictionaries exist in memory as part of the Windows Runtime itself, and your app's XAML resource references to theme resources (or system resources) resolve there at runtime. See the Theme resources in the resource dictionary structure part.

So if you want to use the resource in the generic.xaml. You should add it as a general ResourceDictionary in your app then you can access it from Page.Resources or Application.Current.Resources.

Sign up to request clarification or add additional context in comments.

Comments

0
Application.Current.FindResource(key)

5 Comments

Could not find this method for UWP, does only exist for WPF
@Henk try this: Application.Current.Resources["the_resource_key"]
implicit styles can't be accessed by x:Key like you suggested. I would have to use .Resources[typeof(Class)] which doesn't work
@Henk What do you mean implicit styles? Are they targeting CustomControls/TemplatedControls?
0
    public T GetVisualChild<T>(System.Windows.DependencyObject parent, System.Func<T, bool> predicate) where T : System.Windows.Media.Visual
    {
        int numVisuals = System.Windows.Media.VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
            System.Windows.DependencyObject v = (DependencyObject)System.Windows.Media.VisualTreeHelper.GetChild(parent, i);
            T child = v as T;
            if (child == null)
            {
                child = GetVisualChild<T>(v, predicate);
                if (child != null)
                {
                    return child;
                }
            }
            else
            {
                if (predicate(child))
                {
                    return child;
                }
            }
        }
        return null;
    }

Button btnTopMost= GetVisualChild(this, v => v.Name == "btnTopMost");

"this" is your MainWindow class instance. "btnTopMost" in Generic.xaml defined like this x:Name="btnTopMost". In WPF project, use this code, you can access Generic.xaml's control that in style. I think it may help you, good luck.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.