开发者

How to use a global Style in Windows Phone 7?

开发者 https://www.devze.com 2023-02-18 14:59 出处:网络
I want to use a global Style in WP7, something like: <Style TargetType=\"Button\"> //some code here

I want to use a global Style in WP7, something like:

<Style TargetType="Button">
//some code here
</Style>

The problem is that this code does not seem to work in WP7.

I know how to add x:Key to the 开发者_开发问答Style and after that how to reference it as a StaticResource, but this is not my case. I want a global Style.


If I create an application wide (global) style like this:

<Application.Resources>
    <Style x:Key="MyTextNormalStyle" TargetType="TextBlock">
        <Setter Property="Foreground" Value="White" />
        <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeNormal}" />
        <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilyNormal}" />
    </Style>
</Application.Resources>

Then I can refer to it like this:

<TextBlock Text="some text" Style="{StaticResource MyTextNormalStyle}" />


If I understand you correctly you want to use Implicit Styles in WP7.

If so then have in mind that: Implicit Styles are a feature of Silverlight 4 (and WPF): Windows Phone 7 is based on Silverlight 3+(with a few Silverlight 4 features added). Since there’s no Implicit Styles in Silverlight 3 this mean that there is no way to use them in Windows Phone 7 as well.

So, if you want to implement some kind of global Style in WP7, I would suggest that you try the approach with StaticResource as Matt Lacey suggested.


In WP7 Mango it supports the implicit styling feature. In mango MS upgraded silvelight 3 to silverlight 4. So it works perfectly

try this link : http://www.windowsphonegeek.com/articles/Windows-Phone-7-Mango-Implicit-Styles


If you want all your buttons to use the same style, you will have to create a base button class that implements the style then inherit all your buttons from that.

You can do this with a user control or a custom control. A user control is probably easier.

0

精彩评论

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