zoukankan      html  css  js  c++  java
  • 使用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>

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

  • 相关阅读:
    右键菜单:锁到任务栏丢失 修复
    检测到鼠标时自动禁用触摸板 (注:仅适用于个别笔记本)
    清除右键菜单CMD入口
    VBS编辑字段
    VBS创建数据表
    VBS创建数据库
    QTP操作论坛回复编辑框----webelement
    数字排序
    对应键盘的ASCII码(备忘)
    将字符串打乱输出
  • 原文地址:https://www.cnblogs.com/hongmaju/p/3993063.html
Copyright © 2011-2022 走看看