WP7 - attempt to select and use application static resource in codebehind not working (no resources found)
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? H开发者_开发百科ow do I correctly access the resource?
If the resource is defined within the page you need to the resources in the page and any defined at applicaiton level.
You can do this simply by refering to the Resources
object within the page:
this.Resources["PushpinControlTemplateBlue"];
Application.Current.Resources is define in App.xaml not on a page. Place your template in
<Application.Resources>
Example
精彩评论