2021-09-04 WPF上位机通用框架平台实战-Device

Posted 微软MVP Eleven

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021-09-04 WPF上位机通用框架平台实战-Device相关的知识,希望对你有一定的参考价值。

一:Device窗口布局和功能实现

<UserControl x:Class="Zhaoxi.HostComputer.Views.DeviceView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Zhaoxi.HostComputer.Views"
             xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
             xmlns:sys="clr-namespace:System;assembly=System.Runtime"
             xmlns:base="clr-namespace:Zhaoxi.HostComputer.Base"
             mc:Ignorable="d" UseLayoutRounding="True"
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.Resources>
        <ControlTemplate TargetType="ToggleButton" x:Key="StateButtonTemplate">
            <Border BorderBrush="#99EEEEEE" BorderThickness="0" CornerRadius="3" 
                    Background="#EEE" Height="20" Width="50" Name="back">
                <Grid>
                    <TextBlock Text="停机" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="4,0" FontSize="9" Foreground="#888"/>
                    <TextBlock Text="运行" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="4,0" FontSize="9" Foreground="White"/>
                    <Border Margin="2,2" HorizontalAlignment="Left" Width="23" Background="#DDD" CornerRadius="2">
                        <TextBlock Text="||" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="8"/>
                        <Border.RenderTransform>
                            <TranslateTransform X="22" x:Name="tt"/>
                        </Border.RenderTransform>
                    </Border>
                </Grid>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Duration="0:0:0.2" To="0" Storyboard.TargetName="tt" Storyboard.TargetProperty="X"/>
                                <ColorAnimation Duration="0:0:0.2" To="#38baec" Storyboard.TargetName="back" Storyboard.TargetProperty="Background.(SolidColorBrush.Color)"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetName="tt" Storyboard.TargetProperty="X"/>
                                <ColorAnimation Duration="0:0:0.2" Storyboard.TargetName="back" Storyboard.TargetProperty="Background.(SolidColorBrush.Color)"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.ExitActions>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
        <Style TargetType="RadioButton" x:Key="DeviceItemButtonStyle">
            <Setter Property="Margin" Value="5"/>
            <Setter Property="Background" Value="#DDD"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="RadioButton">
                        <Border Background="Transparent" BorderBrush="{TemplateBinding Background}" BorderThickness="1" CornerRadius="5">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="auto"/>
                                    <RowDefinition/>
                                </Grid.RowDefinitions>
                                <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,15">
                                    <TextBlock Text="&#xe661;" FontFamily="{StaticResource iconfont}"
                                                   VerticalAlignment="Center" HorizontalAlignment="Center"
                                                   FontSize="40" Foreground="#888" Name="device_icon"/>
                                    <StackPanel VerticalAlignment="Center" Margin="20,0">
                                        <TextBlock Text="{Binding Name}" FontSize="13" HorizontalAlignment="Center"/>
                                        <TextBlock Text="8937-45834659" FontSize="10" Margin="0,5,0,0" HorizontalAlignment="Center"/>
                                    </StackPanel>
                                </StackPanel>

                                <Grid Grid.Row="1">
                                    <UniformGrid Columns="4" Grid.Column="1" TextBlock.FontSize="10" TextBlock.Foreground="#888">
                                        <TextBlock Text="正在运行" VerticalAlignment="Top" HorizontalAlignment="Center" Grid.Row="1" Foreground="#333" FontSize="11"/>
                                        <TextBlock>
                                            <Run Text="电压:"/>
                                            <Run Text="220V"/>
                                        </TextBlock>
                                        <TextBlock>
                                            <Run Text="电流:"/>
                                            <Run Text="220A"/>
                                        </TextBlock>
                                        <TextBlock>
                                            <Run Text="温度:"/>
                                            <Run Text="220℃"/>
                                        </TextBlock>
                                        <Border/>
                                        <TextBlock Text="流量:220"/>
                                        <TextBlock Text="温度:220"/>
                                        <TextBlock Text="时长:220"/>
                                    </UniformGrid>
                                </Grid>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter Property="Foreground" Value="#14AAE6" TargetName="device_icon"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Background" Value="#14AAE6"/>
                </Trigger>
            </Style.Triggers>
        </Style>

        <ControlTemplate TargetType="Button" x:Key="FlipButtonTemplate">
            <Grid Background="Transparent" Name="back">
                <TextBlock Text="{Binding Content,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Button}}" VerticalAlignment="Center" HorizontalAlignment="Center"
                           FontSize="20" FontFamily="{StaticResource iconfont}"/>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="#11000000" TargetName="back"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

        <ControlTemplate TargetType="Button" x:Key="AddButtonTemplate">
            <Border Background="#FF3269DE" Name="back" CornerRadius="3">
                <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"
                                  Content="{TemplateBinding Content}" TextBlock.Foreground="White"/>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="#CC3269DE" TargetName="back"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>


        <x:Array Type="sys:String" x:Key="datas">
            <sys:String>#1 Master device info</sys:String>
            <sys:String>#2 Master device info</sys:String>
            <sys:String>#3 Master device info</sys:String>
            <sys:String>#4 Master device info</sys:String>
        </x:Array>
        <Style TargetType="ListViewItem">
            <Setter Property="Margin" Value="0"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="VerticalContentAlignment" Value="Stretch"/>
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="60"/>
            <RowDefinition/>
            <RowDefinition Height="180"/>
        </Grid.RowDefinitions>
        <Button Content="注册新设备" VerticalAlignment="Center" Height="30" Width="90" HorizontalAlignment="Right"
                Template="{StaticResource AddButtonTemplate}" Margin="10,0"/>

        <Grid Grid.Row="1" Margin="20,0" DataContext="{Binding ElementName=lvDeviceList,Path=SelectedItem}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition Width="2*"/>
                <ColumnDefinition Width="0.7*"/>
            </Grid.ColumnDefinitions>
            <Border BorderBrush="#EEE" BorderThickness="0,0,1,0"/>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="30"/>
                    <RowDefinition/>
                    <RowDefinition Height="40"/>
                </Grid.RowDefinitions>
                <TextBlock Text="设备详情" VerticalAlignment="Center" FontSize="14"/>

                <Grid Grid.Row="1">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="40"/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <TextBlock Text="[设备基本参数]" VerticalAlignment="Center" Foreground="#888"/>
                    <Border Background="#FFF" Grid.Row="1" Margin="30,10" BorderThickness="0,2,0,2" CornerRadius="5">
                        <Border.BorderBrush>
                            <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
                                <GradientStop Color="#FFF" Offset="0"/>
                                <GradientStop Color="#14AAE6" Offset="0.4"/>
                                <GradientStop Color="#14AAE6" Offset="0.6"/>
                                <GradientStop Color="#FFF" Offset="1"/>
                            </LinearGradientBrush>
                        </Border.BorderBrush>
                        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                            <TextBlock Text="&#xe661;" FontFamily="{StaticResource iconfont}" FontSize="50" Foreground="#14AAE6" 
                                       HorizontalAlignment="Center"/>
                            <TextBlock Text="{Binding Name}" Margin="0,10" FontWeight="Bold"/>
                            <TextBlock Text="8937-45834659" HorizontalAlignment="Center"/>
                        </StackPanel>
                    </Border>

                    <Grid Grid.Row="2" Margin="10,20,0,20">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="1.5*"/>
                            <ColumnDefinition Width="2*"/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <TextBlock VerticalAlignment="Center">
                            <Run Text="P:" Foreground="#888"/>
                            <Run Text="{Binding MonitorValueList[0].Value,StringFormat=\\{0\\}kW}" FontWeight="Bold"/>
                        </TextBlock>
                        <TextBlock VerticalAlignment="Center" Grid.Column="1">
                            <Run Text="Va:" Foreground="#888"/>
                            <Run Text="668,126.2kV" FontWeight="Bold"/>
                        </TextBlock>
                        <TextBlock VerticalAlignment="Center" Grid.Column="2">
                            <Run Text="La:" Foreground="#888"/>
                            <Run Text="188A" FontWeight="Bold"/>
                        </TextBlock>
                        <TextBlock VerticalAlignment="Center" Grid.Row="1">
                            <Run Text="Q:" Foreground="#888"/>
                            <Run Text="56.3kVar" FontWeight="Bold"/>
                        </TextBlock>
                        <TextBlock VerticalAlignment="Center" Grid.Row="1" Grid.Column="1">
                            <Run Text="Vb:" Foreground="#888"/>
                            <Run Text="768,126.2kV" FontWeight="Bold"/>
                        </TextBlock>
                        <TextBlock VerticalAlignment="Center" Grid.Row="1" Grid.Column="2">
                            <Run Text="Lb:" Foreground="#888"/>
                            <Run Text="89A" FontWeight="Bold"/>
                        </TextBlock>
                        <TextBlock VerticalAlignment="Center" Grid.Row="2">
                            <Run Text="PF:" Foreground="#888"/>
                            <Run Text="0.8" FontWeight="Bold"/>
                        </TextBlock>
                        <TextBlock VerticalAlignment="Center" Grid.Row="2" Grid.Column="1">
                            <Run Text="Vc:" Foreground="#888"/>
                            <Run Text="253,212.8kV" FontWeight="Bold"/>
                        </TextBlock>
                        <TextBlock VerticalAlignment="Center" Grid.Row="2" Grid.Column="2">
                            <Run Text="Lc:" Foreground="#888"/>
                            <Run Text="956A" FontWeight="Bold"/>
                        </TextBlock>
                        <TextBlock VerticalAlignment="Center" Grid.Row="3">
                            <Run Text="PF:" Foreground="#888"/>
                            <Run Text="0.8" FontWeight="Bold"/>
                        </TextBlock>
                        <TextBlock VerticalAlignment="Center" Grid.Row="3" Grid.Column="1">
                            <Run Text="Vc:" Foreground="#888"/>
                            &l

以上是关于2021-09-04 WPF上位机通用框架平台实战-Device的主要内容,如果未能解决你的问题,请参考以下文章

2021-09-04 WPF上位机通用框架平台实战-Device

2021-09-04 WPF上位机通用框架平台实战-主窗口

2021-09-02 WPF上位机通用框架平台实战-项目架构图

2021-09-03 WPF上位机通用框架平台实战-Dashboard

2021-09-02 WPF上位机通用框架平台实战-登录窗口

有小伙伴问:上位机用QT还是winform/wpf好?