使用StaticResource给控件定义公共的样式和属性来写界面XAML

一:效果图

二:定义公共的样式和属性
在MainPage.xaml中
<phone:PhoneApplicationPage.Resources>
        <SolidColorBrush x:Key="MyBrush" Color="Brown"/>
        <Style TargetType="Button" x:Key="MyButtonBackground">
            <Setter Property="Background" Value="Blue"/>
        </Style>
    </phone:PhoneApplicationPage.Resources>
    
在App.xaml中,下划线部分
 <!--应用程序资源-->
    <Application.Resources>
        <local:LocalizedStrings xmlns:local="clr-namespace:XAMLResources" x:Key="LocalizedStrings"/>
        <Style x:Name="MyTitleText" 
               BasedOn="{StaticResource PhoneTextBlockBase}" 
               TargetType="TextBlock">
            <Setter Property="FontFamily"
                    Value="Verdana"/>
            <Setter Property="FontSize"
                    Value="64"/>
        </Style>
    </Application.Resources>

 

MainPage.xaml的全部代码
<phone:PhoneApplicationPage
    x:Class="XAMLResources.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <phone:PhoneApplicationPage.Resources>
        <SolidColorBrush x:Key="MyBrush" Color="Brown"/>
        <Style TargetType="Button" x:Key="MyButtonBackground">
            <Setter Property="Background" Value="Blue"/>
        </Style>
    </phone:PhoneApplicationPage.Resources>
    
    <!--LayoutRoot 是包含所有页面内容的根网格-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel 包含应用程序的名称和页标题-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock Text="红马車" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
            <TextBlock 
                Text="hongmaju" 
                Margin="9,-7,0,0" Style="{StaticResource MyTitleText}"/>--------------使用了App.xaml中定义公共样式
        </StackPanel>

        <!--ContentPanel - 在此处放置其他内容-->
        <StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Button
                Height="200"
                Width="150"
                Background="{ StaticResource MyBrush }" >
            </Button>
            <Button
                Height="200"
                Width="150"
                Style="{StaticResource MyButtonBackground}">
            </Button>
            <ListBox
                Height="150"
                Width="200"
                Background="{StaticResource MyBrush}">
                
            </ListBox>

        </StackPanel>
    </Grid>

</phone:PhoneApplicationPage>
 三:将定义的公共样式和属性直接使用在相应控件位置的快捷方式(如下图)

转载于:https://www.cnblogs.com/hongmaju/p/3993063.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值