开发者

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

开发者 https://www.devze.com 2023-02-03 23:39 出处:网络
With Windows Phone 7, I\'m attempting to dynamically add controls to and object in codebehind, and apply a StaticResource to the new control.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消