Prism for WPF 搭建一個簡單的模塊化開發框架(二)

原文: Prism for WPF 搭建一個簡單的模塊化開發框架(二)

今天又有時間了,再改改,加了一些控件全局的樣式css

樣式代碼html

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <!--TextBlock-->
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="Foreground" Value="{DynamicResource Sys.Foreground}"/>
        <Setter Property="FontFamily" Value="{DynamicResource Sys.FontFamily}"/>
        <Setter Property="FontSize" Value="{DynamicResource Sys.FontSize}"/>
        <Setter Property="Padding" Value="0,3,0,0"/>
    </Style>
    <!--下拉框-->
    <Style TargetType="ComboBox">
        <Setter Property="Foreground" Value="Black"/>
    </Style>
    <Style TargetType="ComboBoxItem">
        <Setter Property="Foreground" Value="Black"/>
    </Style>
    <!--文本框-->
    <Style TargetType="TextBox">
        <Setter Property="Foreground" Value="{DynamicResource Sys.Input.Foreground}"/>
        <Setter Property="Background" Value="{DynamicResource Sys.Input.Background}"/>
        <Setter Property="BorderBrush" Value="{DynamicResource Sys.Input.BorderBrush}"/>
        <Setter Property="FontSize" Value="{DynamicResource Sys.Input.FontSize}"/>
        <Setter Property="BorderThickness" Value="{DynamicResource Sys.Input.BorderThickness}"/>
        <Setter Property="Height" Value="20"/>
    </Style>
    <Style TargetType="PasswordBox">
        <Setter Property="Foreground" Value="{DynamicResource Sys.Input.Foreground}"/>
        <Setter Property="Background" Value="{DynamicResource Sys.Input.Background}"/>
        <Setter Property="BorderBrush" Value="{DynamicResource Sys.Input.BorderBrush}"/>
        <Setter Property="FontSize" Value="{DynamicResource Sys.Input.FontSize}"/>
        <Setter Property="BorderThickness" Value="{DynamicResource Sys.Input.BorderThickness}"/>
        <Setter Property="Height" Value="20"/>
    </Style>
    <!--複選框-->
    <Style TargetType="CheckBox">
        <Setter Property="Foreground" Value="{DynamicResource Sys.Input.Foreground}"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="4,0,0,0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="CheckBox">
                    <Grid VerticalAlignment="Top">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition Width="5*"/>
                        </Grid.ColumnDefinitions>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <ColorAnimation Duration="0" To="{DynamicResource Sys.Input.MouseOver.BorderBrush}" Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)" Storyboard.TargetName="hotbg_20" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="Disabled"/>
                                <VisualState x:Name="Pressed"/>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="CheckStates">
                                <VisualState x:Name="Checked">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="CheckIcon">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ColorAnimation Duration="0" To="{DynamicResource Sys.Input.Checked.BorderBrush}" Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)" Storyboard.TargetName="hotbg_20" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Unchecked"/>
                                <VisualState x:Name="Indeterminate">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="IndeterminateIcon">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Viewbox HorizontalAlignment="Center" VerticalAlignment="Center" Height="23">
                            <Grid>
                                <Path x:Name="hotbg_20" Stretch="Fill" StrokeThickness="1.33333" StrokeLineJoin="Round" Fill="{DynamicResource Sys.Input.Background}" Stroke="{DynamicResource Sys.Input.BorderBrush}" Data="F1 M 746.405,724.995L 725.944,724.995C 725.703,724.995 725.508,724.534 725.508,723.966L 725.508,704.689C 725.508,704.122 725.703,703.661 725.944,703.661L 746.405,703.661C 746.646,703.661 746.841,704.122 746.841,704.689L 746.841,723.966C 746.841,724.534 746.646,724.995 746.405,724.995 Z " UseLayoutRounding="False">
                                    <!--<Path.Fill>
                                        <RadialGradientBrush RadiusX="1.96789" RadiusY="1.89722" Center="0.0848637,0.291189" GradientOrigin="0.0848637,0.291189">
                                            <RadialGradientBrush.RelativeTransform>
                                                <TransformGroup>
                                                    <SkewTransform CenterX="0.0848637" CenterY="0.291189" AngleX="-13.2747" AngleY="0"/>
                                                    <RotateTransform CenterX="0.0848637" CenterY="0.291189" Angle="37.211"/>
                                                </TransformGroup>
                                            </RadialGradientBrush.RelativeTransform>
                                            <GradientStop Color="#303EAFFF" Offset="0.0858586"/>
                                            <GradientStop Color="#302463FF" Offset="0.301316"/>
                                            <GradientStop Color="#300A17FF" Offset="0.555556"/>
                                            <GradientStop Color="#302474FF" Offset="0.828512"/>
                                            <GradientStop Color="#303ED1FF" Offset="0.969697"/>
                                        </RadialGradientBrush>
                                    </Path.Fill>-->
                                </Path>
                                <Path x:Name="CheckIcon" Data="M102.03442,598.79645 L105.22962,597.78918 L106.78825,600.42358 C106.78825,600.42358 108.51028,595.74304 110.21724,593.60419 C112.00967,591.35822 114.89314,591.42316 114.89314,591.42316 C114.89314,591.42316 112.67844,593.42645 111.93174,594.44464 C110.7449,596.06293 107.15683,604.13837 107.15683,604.13837 z" Fill="{DynamicResource Sys.Input.Foreground}" FlowDirection="LeftToRight" Stretch="Fill" HorizontalAlignment="Center" UseLayoutRounding="False" VerticalAlignment="Center" Visibility="Collapsed"/>
                                <Border x:Name="IndeterminateIcon" Background="{DynamicResource Sys.Input.Foreground}" Margin="4,9" Visibility="Collapsed"/>
                            </Grid>
                        </Viewbox>
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Grid.Column="1" Margin="{TemplateBinding Padding}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <!--單選框-->
    <Style TargetType="RadioButton">
        <Setter Property="Foreground" Value="{DynamicResource Sys.Input.Foreground}"/>
    </Style>

    <!--Button-->
    <Style TargetType="Button">
        <Setter Property="FontSize" Value="{DynamicResource Sys.FontSize}"/>
        <Setter Property="Foreground" Value="{DynamicResource Sys.Foreground}"/>
        <Setter Property="FontFamily" Value="{DynamicResource Sys.FontFamily}"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="Background" Value="{DynamicResource Sys.Button.Background}"/>
        <Setter Property="BorderBrush" Value="{DynamicResource Sys.Button.BorderBrush}"/>
        <Setter Property="BorderThickness" Value="{DynamicResource Sys.Button.BorderThickness}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Name="border" CornerRadius="1" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}">
                        <ContentPresenter Name="content" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                              RecognizesAccessKey="True" 
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="{DynamicResource Sys.Button.MouseOver.Background}">
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" Value="{DynamicResource Sys.Button.Pressed.Background}">
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="border" Property="Opacity" Value="0.6" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

而後又作了個配置系統的名稱等信息的xamlexpress

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:PW="clr-namespace:PW.Desktop">
    <sys:String x:Key="System.Title">這裏是系統名稱</sys:String>
    <sys:String x:Key="System.TitleEN">this is system name</sys:String>
    <sys:String x:Key="System.Company">xxxxx有限公司</sys:String>
    <sys:String x:Key="System.Version">1.0.0</sys:String>
    <sys:String x:Key="System.Copyright">Copyright © 2018 xxxxx有限公司</sys:String>
</ResourceDictionary>

作了個兩個字體的文本樣式,一個是用來作圖標的,一個是液晶字體,用在特殊狀況下顯示數字app

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <!--FIcon-->
    <Style x:Key="FIcon" TargetType="TextBlock">
        <Setter Property="FontFamily" Value="pack://application:,,,/PW.Desktop;component/Resources/#SF2015"></Setter>
        <!--<Setter Property="FontFamily" Value="../Resources/iconfont.ttf#SF2015"></Setter>-->
        <Setter Property="Foreground" Value="{StaticResource ModuleControl.TextBlock.Foreground}"/>
        <Setter Property="TextAlignment" Value="Center"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="FontSize" Value="{StaticResource Sys.IconFontSize}"/>
        <Setter Property="Width" Value="20"/>
        <Setter Property="Height" Value="20"/>
        <Setter Property="Margin" Value="4,2,4,2"/>
    </Style>
    <Style x:Key="QuartzRegular">
        <Setter Property="TextElement.FontFamily" Value="pack://application:,,,/PW.Desktop;component/Resources/#Quartz Regular" />
    </Style>
</ResourceDictionary>

字體文件我就不貼了,網上都有框架

用法是這樣的模塊化

<TextBlock x:Name="navIco" Text="&#xe63c;" Style="{DynamicResource FIcon}"></TextBlock>字體

這裏注意這裏的寫法是&#xe63c;this

而後若是是用後臺代碼給賦值就要寫成這樣的spa

具體是在哪裏給轉義了還不清楚.net

 

最後把這些文件都引入到主工程的App.xaml下面

兩種引用方法都行,ModularityStyles.xaml是系統的一些配色

看一下效果

這裏不一樣的模塊內設置按鈕的背景色和名稱,而後在登錄模塊去加載並顯示到頁面,包括下面的導航按鈕圖標

這裏右上角的導航條的文本和圖標就是在各模塊設置的,這樣寫

 public void Initialize()
        {
            GlobalData.NavModules.Add(new NavModuleInfo() { region = RegionNames.Main, module = ModuleNames.Map, title = ModuleTitle.Map, icon = "\xe63c", img= Images.CreateImageSourceFromImage(Properties.Resources.avtar) });
            this.moduleTracker.RecordModuleInitialized(ModuleNames.Map);
            regionManager.RegisterViewWithRegion(RegionNames.Main, typeof(MapModule_MainView));
        }

就兩個模塊而後就兩個按鈕看起來好像有些小氣,,,,

下次準備把左側的導航作出來

相關文章
相關標籤/搜索