2021-09-04 WPF上位机通用框架平台实战-设备编辑

Posted 微软MVP Eleven

tags:

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


窗体管理对象

public class WindowManager
{
    static Dictionary<string, WindowStruct> _regWindows = new Dictionary<string, WindowStruct>();
    public static void Register<T>(string name, Window owner = null)
    {
        if (!_regWindows.ContainsKey(name))
            _regWindows.Add(name, new WindowStruct { WindowType = typeof(T), Owner = owner });
    }
    public static bool ShowDialog(string name, object vm)
    {
        if (_regWindows.ContainsKey(name))
        {
            Type type = _regWindows[name].WindowType;

            var win = (Window)Activator.CreateInstance(type);
            win.Owner = _regWindows[name].Owner;
            win.DataContext = vm;
            return win.ShowDialog() == true;
        }
        return false;
    }
}

public class WindowStruct
{
    public Type WindowType { get; set; }
    public Window Owner { get; set; }
}

父窗体注册子窗体

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = new MainViewModel();

        WindowManager.Register<DeviceEditWindow>("DeviceEditWindow", this);
    }

    private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        this.DragMove();
    }
}

子窗体包含到父窗体

<Window x:Class="Zhaoxi.HostComputer.Views.DeviceEditWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Zhaoxi.HostComputer.Views"
        xmlns:sys="clr-namespace:System;assembly=System.Runtime"
        mc:Ignorable="d" UseLayoutRounding="True"
        FontFamily="Microsoft YaHei" FontWeight="ExtraLight" Foreground="#333"
        WindowStyle="None" AllowsTransparency="True"  WindowStartupLocation="CenterOwner" Background="Transparent"
        Title="设备信息维护" Height="550" Width="800">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Zhaoxi.HostComputer;component/Assets/Styles/ComboBoxStyle.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/Zhaoxi.HostComputer;component/Assets/Styles/LeftTabControlStyle.xaml"/>
                <ResourceDictionary>
                    <Style TargetType="Button" x:Key="ControlButtonStyle">
                        <Setter Property="FontSize" Value="12"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="Button">
                                    <Grid Background="Transparent" Height="30" Width="45" Name="back">
                                        <Border Name="hover" Background="{TemplateBinding Background}" Visibility="Collapsed"/>
                                        <TextBlock Text="{TemplateBinding Content}" FontFamily="{StaticResource iconfont}" 
                                       VerticalAlignment="Center" HorizontalAlignment="Center" Name="txt"
                                       Foreground="White"/>
                                    </Grid>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="IsMouseOver" Value="True">
                                            <Setter Property="Visibility" Value="Visible" TargetName="hover"/>

                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Foreground" Value="{Binding Tag,RelativeSource={RelativeSource Mode=Self}}"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>

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

                    <x:Array Type="sys:String" x:Key="datas">
                        <sys:String>1</sys:String>
                        <sys:String>2</sys:String>
                        <sys:String>3</sys:String>
                        <sys:String>4</sys:String>
                        <sys:String>5</sys:String>
                        <sys:String>6</sys:String>
                        <sys:String>7</sys:String>
                    </x:Array>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Border Margin="5" Background="#FFF6F7FC">
        <Border.Effect>
            <DropShadowEffect BlurRadius="8" Opacity="0.2" Direction="0" Color="Gray" ShadowDepth="0"/>
        </Border.Effect>
        <Grid MouseLeftButtonDown="Grid_MouseLeftButtonDown">
            <Grid.RowDefinitions>
                <RowDefinition Height="80"/>
                <!--<RowDefinition Height="40"/>
                <RowDefinition Height="40"/>
                <RowDefinition Height="40"/>
                <RowDefinition Height="40"/>
                <RowDefinition Height="10"/>
                <RowDefinition Height="40"/>-->
                <RowDefinition/>
                <!--<RowDefinition Height="50"/>-->
            </Grid.RowDefinitions>
            <Border Background="#FF3269DE"/>
            <StackPanel VerticalAlignment="Center" HorizontalAlignment="Left">
                <TextBlock Text="设备信息编辑" Foreground="White" VerticalAlignment="Center" Margin="15,0"/>
                <TextBlock Text="{Binding Name}" Foreground="White" VerticalAlignment="Center" Margin="15,20,0,5"
                           FontSize="18"/>
            </StackPanel>

            <Button Content="&#xe653;" Style="{StaticResource ControlButtonStyle}" Background="#11000000" Tag="#333" Height="33" VerticalAlignment="Top"
                    HorizontalAlignment="Right" Click="Button_Click"/>

            <TabControl Grid.Row="1" BorderThickness="0" Margin="0,20">
                <TabItem Header="通信配置">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="40"/>
                            <RowDefinition/>
                            <RowDefinition Height="50"/>
                        </Grid.RowDefinitions>
                        <StackPanel Orientation="Horizontal" Margin="30,0" VerticalAlignment="Center">
                            <TextBlock Text="通信方式" Margin="20,0"/>
                            <RadioButton Content="串口" IsChecked="True" Click="RadioButton_Checked"/>
                            <RadioButton Content="网口" Margin="15,0" Click="RadioButton_Checked"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Grid.Row="1" Margin="30,0" VerticalAlignment="Center">
                            <TextBlock Text="通信参数" Margin="20,0" VerticalAlignment="Center"/>
                            <ComboBox SelectedIndex="0" Width="80" x:Name="cb1">
                                <ComboBoxItem Content="COM1"/>
                                <ComboBoxItem Content="COM2"/>
                            </ComboBox>
                            <ComboBox SelectedIndex="1" Width="80" Margin="7,0" x:Name="cb2">
                                <ComboBoxItem Content="4800"/>
                                <ComboBoxItem Content="9600"/>
                                <ComboBoxItem Content="19200"/>
                            </ComboBox>
                            <ComboBox SelectedIndex="1" Width="50" x:Name="cb3">
                                <ComboBoxItem Content="7"/>
                                <ComboBoxItem Content="8"/>
                            </ComboBox>
                            <ComboBox SelectedIndex="0" Width="70" Margin="7,0" x:Name="cb4">
                                <ComboBoxItem Content="None"/>
                                <ComboBoxItem Content="Odd"/>
                                <ComboBoxItem Content="Even"/>
                            </ComboBox>
                            <ComboBox SelectedIndex="0" Width="70" x:Name="cb5">
                                <ComboBoxItem Content="One"/>
                                <ComboBoxItem Content="Two"/>
                            </ComboBox>
                            <TextBox Text="192.168.2.1" VerticalContentAlignment="Center" Width="100" x:Name="tb1"
                         Visibility="Collapsed" Height="26"/>
                            <TextBox Text="102" VerticalContentAlignment="Center" Width="50" Margin="7,0" x:Name="tb2"
                         Visibility="Collapsed"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Grid.Row="2" Margin="30,0" VerticalAlignment="Center">
                            <TextBlock Text="通信协议" Margin="20,0" VerticalAlignment="Center"/>
                            <ComboBox SelectedIndex="0" Width="167">
                                <ComboBoxItem Content="ModbusRTU"/>
                                <ComboBoxItem Content="ModbusASCII"/>
                                <ComboBoxItem Content="ModbusTCP"/>
                                <ComboBoxItem Content="S7-200SMART"/>
                                <ComboBoxItem Content="S7-1500"/>
                            </ComboBox>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Grid.Row="3" Margin="30,0" VerticalAlignment="Center">
                            <TextBlock Text="设备站号" Margin="20,0" VerticalAlignment="Center"/>
                            <TextBox Text="01" Height="23" VerticalContentAlignment="Center" Width="167"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Grid.Row="4" Margin="30,0" VerticalAlignment="Center">
                            <TextBlock Text="机  架  号" Margin="19,0" VerticalAlignment="Center"/>
                            <TextBox Text="00" Height="23" VerticalContentAlignment="Center" Width="167"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Grid.Row="5" Margin="30,0" VerticalAlignment="Center">
                            <TextBlock Text="插  槽  号" Margin="19,0" VerticalAlignment="Center"/>
                            <TextBox Text="01" Height="23" VerticalContentAlignment="Center" Width="167"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Grid.Row="6" Margin="30,0" VerticalAlignment="Center">
                            <TextBlock Text="刷新频率" Margin="20,0" VerticalAlignment="Center"/>
                            <ComboBox SelectedIndex="2" Width="167">
                                <ComboBoxItem Content="10ms"/>
                                <ComboBoxItem Content="50ms"/>
                                <ComboBoxItem Content="500ms"/>
                                <ComboBoxItem Content="1000ms"/>
                                <ComboBoxItem Content="2000ms"/>
                            </ComboBox>
                        </StackPanel>

                        <StackPanel Orientation="Horizontal" Grid.Row="8" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="50,0">
                            <Button Content="保存" Template="{StaticResource AddButtonTemplate}" Width="75" Height="28" Margin="5,0"
                        Background="#FF3269DE"/>
                            <Button Content="退出" Template="{StaticResource AddButtonTemplate}" Width="55" Height="28"
                        Background="#999" Visibility="Collapsed"/>
                        </StackPanel>
                    </Grid>
                </TabItem>
                <TabItem Header="监控点位">
                    <Grid Grid.Row="7">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="30"/>
                            <RowDefinition/>
                            <RowDefinition Height="50"/>
                        </Grid.RowDefinitions>
                        <Grid Background="#EEE" Margin="10,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="45"/>
                                <ColumnDefinition/>
                                <ColumnDefinition Width="80"/>
                                <ColumnDefinition Width="60"/>
                                <ColumnDefinition Width="60"/>
                                <ColumnDefinition Width="100"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="序号" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            <TextBlock Text="监控标签" VerticalAlignment="Center" HorizontalAlignment="Left" Grid.Column="1"/>
                            <TextBlock Text="寄存器地址"以上是关于2021-09-04 WPF上位机通用框架平台实战-设备编辑的主要内容,如果未能解决你的问题,请参考以下文章

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好?