WP7 - attempt to select and use application static resource in codebehind not working (no resources found).

Posted by pearcewg on Stack Overflow See other posts from Stack Overflow or by pearcewg
Published on 2011-01-10T16:50:28Z Indexed on 2011/01/10 16:53 UTC
Read the original article Hit count: 152

Filed under:

With Windows Phone 7, I'm attempting to dynamically add controls to and object in codebehind, and apply a StaticResource to the new control.

Xaml file sample:

<phone:PhoneApplicationPage.Resources>
  <ControlTemplate x:Key="PushpinControlTemplateBlue" TargetType="my2:Pushpin">
  ...
  </ControlTemplate>
</phone:PhoneApplicationPage.Resources>

Codebehind sample:

>Pushpin myPush = new Pushpin();
>myPush.Location = new GeoCoordinate(52.569593, -0.9261151403188705);
>myPush.Content = "";
>myPush.Template = (ControlTemplate)Application.Current.Resources["PushpinControlTemplateBlue"];
>mapMain.Children.Add(myPush);

When I debug, and look at "Application.Current.Resources", there are no items in the collection, so the item is added to the controls list, but doesn't show up because it has no content.

Is there something simple I'm doing wrong? How do I correctly access the resource?

© Stack Overflow or respective owner

Related posts about windows-phone-7