Thursday, September 11, 2008

ResourceLocator for Silverlight

Last year I've done a large project with WPF and so I couldn't wait to start with Silverlight. I know there's a lot of stuff "missing" so once in a while I'll be posting some stuff that I know could come in handy.

First thing I started with is a decent Resource Locator. Usually you would create your animations in the App.xaml and reference them in your UserControl. There are no triggers in Silverlight, in WPF triggers are able to connect to the target object for an animation. In Silverlight you should use the events to do this, they won't do this automatically so you have to do this yourself in code-behind.

So I've created a helper class that can find Resources for you (like in WPF):

public static class ResourceLocator
{
///
/// Helper method for finding resources located in app.xaml
///

public static object FindResource(string name)
{
if (App.Current.Resources.Contains(name))
{
return App.Current.Resources[name];
}
else
{
FrameworkElement root = App.Current.RootVisual as FrameworkElement;
return root.FindName(name);
}
}
}

No comments:

Post a Comment