The main difference between WPF and Winrt for dealing with resources here is that you get FindResource()
and siblings in WPF objects, while in Winrt you just have the Resources
property.
The basic technique, where the object type is used as the key for TargetType
styles, still works though. Here's a simple helper extension method to do what you want:
public static object TryFindResource(this FrameworkElement element, object key)
{
if (element.Resources.ContainsKey(key))
{
return element.Resources[key];
}
return null;
}
Call just like you would in WPF:
control.Style = (Style)toplevelcontrol.TryFindResource(control.GetType());
(Note that your original example would not compile, as control
is a variable, and you can't use typeof
on a variable. I've fixed the bug in the above example call).