用wpf替代winform 解决PLC数据量过大页面卡顿的问题

news2025/2/27 5:56:13

winform 由于不是数据驱动, 页面想刷新数据必须刷新控件, wpf则不用. 可以利用wpf 的数据绑定和IOC, 页面中的消息传递, itemscontrol 实现大量数据刷新, 上位机页面不卡顿

代码如下:

<Window
    x:Class="NavTest.Views.NewMainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:con="clr-namespace:ValueConverters;assembly=ValueConverters"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:hc="https://handyorg.github.io/handycontrol"
    xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
    xmlns:local="clr-namespace:NavTest.Views"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:mv="clr-namespace:NavTest.ViewModels"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:tt="clr-namespace:NavTest.Eneities"
    xmlns:vc="clr-namespace:NavTest.Components"
    Title="NewMainView"
    Width="1280"
    Height="800"
    d:DataContext="{d:DesignInstance mv:NewMainViewModel}"
    AllowsTransparency="True"
    Background="#2B2C31"
    FontFamily="Microsoft YaHei"
    FontSize="30"
    FontWeight="ExtraLight"
    ResizeMode="CanResizeWithGrip"
    WindowStartupLocation="CenterScreen"
    WindowStyle="None"
    mc:Ignorable="d">
    <Window.Resources>
        <Style x:Key="ControlButtonStyle" TargetType="{x:Type Button}">
            <Setter Property="Focusable" Value="False" />
            <Setter Property="Background" Value="#66FFFFFF" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="FontSize" Value="16" />
            <Setter Property="Width" Value="50" />
            <Setter Property="HorizontalContentAlignment" Value="Center" />
            <Setter Property="VerticalContentAlignment" Value="Center" />
            <Setter Property="WindowChrome.IsHitTestVisibleInChrome" Value="True" />
            <Setter Property="FontFamily" Value="{StaticResource iconfont}" />
            <Setter Property="IsTabStop" Value="False" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid x:Name="LayoutRoot" Background="Transparent">
                            <Rectangle
                                x:Name="ButtonBackground"
                                Fill="{TemplateBinding Background}"
                                Opacity="0" />
                            <Border x:Name="ButtonBorder" SnapsToDevicePixels="true">
                                <ContentPresenter
                                    x:Name="TitleBarButtonContentPresenter"
                                    Margin="{TemplateBinding Padding}"
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                    Focusable="False"
                                    RecognizesAccessKey="True"
                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                            </Border>
                        </Grid>

                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter TargetName="ButtonBackground" Property="Opacity" Value="1" />
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter TargetName="ButtonBackground" Property="Opacity" Value="0.6" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="5*" />
            <ColumnDefinition Width="2*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="0.3*" />
            <RowDefinition Height="5*" />
        </Grid.RowDefinitions>
        <!--  窗口控制按钮  -->
        <StackPanel
            Grid.Column="3"
            Height="33"
            Margin="0,0,5,0"
            HorizontalAlignment="Right"
            VerticalAlignment="Top"
            Orientation="Horizontal">
            <Button
                Background="#22FFFFFF"
                Content="&#xe7e6;"
                Style="{StaticResource ControlButtonStyle}"
                ToolTip="Minimize">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <i:ChangePropertyAction
                            PropertyName="WindowState"
                            TargetObject="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
                            Value="Minimized" />
                    </i:EventTrigger>
                    <!--<i:EventTrigger EventName="Click">
                        <i:ChangePropertyAction
                            PropertyName="WindowState"
                            TargetObject="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
                            Value="Minimized" />
                    </i:EventTrigger>-->
                </i:Interaction.Triggers>
            </Button>
            <Button
                Background="#22FFFFFF"
                Command="{Binding MaxNormorCommand}"
                CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
                Content="&#xe694;"
                FontSize="14"
                Style="{StaticResource ControlButtonStyle}"
                ToolTip="Maximize" />
            <Button
                Background="Red"
                Content="&#xe653;"
                Style="{StaticResource ControlButtonStyle}"
                ToolTip="Close">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <i:CallMethodAction MethodName="Close" TargetObject="{Binding RelativeSource={RelativeSource AncestorType=Window}}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Button>
        </StackPanel>
        <Border
            Grid.Row="2"
            Grid.RowSpan="2"
            Margin="5,5,5,58"
            Padding="10"
            Background="White"
            BorderBrush="Gray"
            BorderThickness="1"
            CornerRadius="5">
            <Border.Effect>
                <DropShadowEffect />
            </Border.Effect>
            <StackPanel HorizontalAlignment="Center" VerticalAlignment="Top">
                <TextBlock
                    Margin="5,25,5,5"
                    HorizontalAlignment="Center"
                    Text="Main">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseUp">
                            <i:InvokeCommandAction Command="{Binding SwitchPageCommand}" CommandParameter="main" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </TextBlock>
                <TextBlock
                    Margin="5,25,5,5"
                    HorizontalAlignment="Center"
                    Text="Page1">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseUp">
                            <i:InvokeCommandAction Command="{Binding SwitchPageCommand}" CommandParameter="page1" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </TextBlock>
                <TextBlock
                    Margin="5,25,5,5"
                    HorizontalAlignment="Center"
                    Text="Page2">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseUp">
                            <i:InvokeCommandAction Command="{Binding SwitchPageCommand}" CommandParameter="page2" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </TextBlock>
                <TextBlock
                    Margin="5,25,5,5"
                    HorizontalAlignment="Center"
                    Text="Page3">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseUp">
                            <i:InvokeCommandAction Command="{Binding SwitchPageCommand}" CommandParameter="page3" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </TextBlock>
                <TextBlock
                    Margin="5,25,5,5"
                    HorizontalAlignment="Center"
                    Text="Page5">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseUp">
                            <i:InvokeCommandAction Command="{Binding SwitchPageCommand}" CommandParameter="page5" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </TextBlock>
            </StackPanel>
        </Border>
        <ContentControl
            x:Name="container"
            Grid.Row="2"
            Grid.RowSpan="2"
            Grid.Column="1"
            Grid.ColumnSpan="2"
            Margin="5,5,5,5"
            Content="{Binding MyContent}" />
    </Grid>
</Window>

<UserControl
    x:Class="NavTest.Views.Page2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:con="clr-namespace:ValueConverters;assembly=ValueConverters"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:hc="https://handyorg.github.io/handycontrol"
    xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
    xmlns:local="clr-namespace:NavTest.Views"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:mv="clr-namespace:NavTest.ViewModels"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:tt="clr-namespace:NavTest.Eneities"
    xmlns:vc="clr-namespace:NavTest.Components"
    d:DataContext="{d:DesignInstance mv:Page2ViewModel}"
    d:DesignHeight="450"
    d:DesignWidth="800"
    FontSize="22"
    mc:Ignorable="d">

    <Grid>
        <ItemsControl AlternationCount="2" ItemsSource="{Binding PLCModels}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>

            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border x:Name="border" Padding="2">
                        <StackPanel>
                            <TextBlock Foreground="White" Text="{Binding Id}" />
                            <TextBlock Foreground="White" Text="{Binding Name}" />
                            <TextBlock Foreground="White" Text="{Binding PlcValue}" />
                        </StackPanel>
                    </Border>
                    <DataTemplate.Triggers>
                        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                            <Setter TargetName="border" Property="Background" Value="red" />
                        </Trigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>

</UserControl>




using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using NavTest.Eneities;
using NavTest.Views;
using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows;

namespace NavTest.ViewModels
{
    public partial class NewMainViewModel : ObservableRecipient
    {

        public NewMainViewModel(Page1 page1, Page2 page2, Page3 page3, Page5 page5, ObservableCollection<PLCModel> pLCModels)
        {
            this.page1 = page1;
            this.page2 = page2;
            this.page3 = page3;
            this.page5 = page5;
            this.pLCModels = pLCModels;

            IsActive = true;
            this.MyContent = page2;
            PlcGetValue();
        }

        [ObservableProperty]
        private object? myContent;
        private readonly Page1 page1;
        private readonly Page2 page2;
        private readonly Page3 page3;
        private readonly Page5 page5;
        [ObservableProperty]
        private ObservableCollection<PLCModel> pLCModels;


        private int myUshort1;

        public int MyUshort1
        {
            get => myUshort1;
            set => SetProperty(ref myUshort1, value, true);
        }

        [RelayCommand]
        public void MaxNormor(Window window)
        {
            window.WindowState =
                window.WindowState == WindowState.Maximized
                    ? WindowState.Normal
                    : WindowState.Maximized;
        }

        [RelayCommand]
        public void SwitchPage(string str)
        {

            switch (str)
            {
                case "main":
                    //this.MyContent;
                    break;
                case "page1":
                    this.MyContent = page1;
                    break;
                case "page2":
                    this.MyContent = page2;
                    break;
                case "page3":
                    this.MyContent = page3;
                    break;
                case "page5":
                    this.MyContent = page5;
                    break;
                default:
                    break;
            }
        }


        private void PlcGetValue()
        {
            Task.Run(async () =>
            {

                while (true)
                {
                    await Task.Delay(500);
                    PLCModels = new();
                    for (int i = 0; i < 90; i++)
                    {
                        var random = new Random();
                        PLCModels.Add(new()
                        {
                            Id = i,
                            Name = $"Name{i}",
                            PlcValue = random.Next(1, 500)
                        });
                        if (i == 10)
                        {
                            MyUshort1 = random.Next(1, 500);
                        }
                    }
                }
            });
        }
    }
}

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;
using NavTest.Eneities;
using System.Collections.ObjectModel;

namespace NavTest.ViewModels
{
    public partial class Page2ViewModel : ObservableRecipient, IRecipient<PropertyChangedMessage<int>>
    {
        [ObservableProperty]
        private ObservableCollection<PLCModel> pLCModels;

        public Page2ViewModel(ObservableCollection<PLCModel> pLCModels)
        {

            this.PLCModels = pLCModels;
            IsActive = true;

        }

        public void Receive(PropertyChangedMessage<int> message)
        {
            if (message.Sender is NewMainViewModel vm)
            {
                this.PLCModels = vm.PLCModels;
            }
        }
    }
}

using Microsoft.Extensions.DependencyInjection;
using NavTest.Eneities;
using NavTest.Views;
using System;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Windows;

namespace NavTest
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public App() => Services = ConfigureServices();

        public IServiceProvider? Services { get; }

        public new static App Current => (App)Application.Current;

        private IServiceProvider? ConfigureServices()
        {
            ServiceCollection services = new ServiceCollection();

            //View
            #region ViewModel,View 注入

            services.AddSingleton<NewMainView>();
            services.AddSingleton<Page1>();
            services.AddSingleton<Page2>();
            services.AddSingleton<Page3>();
            services.AddSingleton<Page5>();
            var viewModelTypes = Assembly.GetExecutingAssembly().GetTypes()
                .Where(t => t.Name.EndsWith("ViewModel"));

            foreach (var type in viewModelTypes)
            {
                services.AddScoped(type);
            }

            //services.AddSingleton<Page2>(sp => new Page2()
            //{
            //    DataContext = sp.GetService<Page2ViewModel>()
            //});


            #endregion

            //PLC注入
            services.AddSingleton<ObservableCollection<PLCModel>>();

            return services.BuildServiceProvider();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            NewMainView newMainView = this.Services?.GetService<NewMainView>();
            newMainView.Show();
            //MainView? mainView = this.Services?.GetService<MainView>();
            //mainView.DataContext = this.Services?.GetService<MainViewModel>();
            //mainView.Show();
        }
    }
}

效果图:

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1087458.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Linu:【Kafka四】集群介绍与单机搭建

目录 环境简介 一、搭建kafka集群 1.1、复制出两个kafka的配置文件 1.2、修改配置文件中的如下属性 二、启动kafka集群 三、可校验kafka三个节点是否均启动成功 四、查看集团中主题的分区和副本 4.1、新建一个包含了分区和副本的主题 4.2、查看该主题的详细信息 五、…

EDI入门讲解——一篇文章告诉你EDI是什么

知行软件从2008年开始做EDI解决方案&#xff0c;多年来帮助国内企业建立起与交易伙伴的EDI连接&#xff0c;在国内市场已有较高的市场份额。 我们在国内有自己的研发、实施和运维团队。如果您在产品使用过程中有任何问题、有任何功能上的需求、或者在我们产品中发现了错误&…

Talk | SIGGRAPH‘23 Best Paper 秦颖思:分罗曼三维显示器—各点独立变焦显示技术

本期为TechBeat人工智能社区第537期线上Talk。 北京时间10月12日&#xff08;周四&#xff09;20:00&#xff0c;卡耐基梅隆大学博士生—秦颖思的Talk已准时在TechBeat人工智能社区开播&#xff01; 她与大家分享的主题是: “分罗曼三维显示器—各点独立变焦显示技术”&#xf…

C++入门1

C入门1 1.前言2.命名空间1.C语言对于命名空间方面的缺陷2.命名空间的语法特性1.域作用限定符2.命名空间的可嵌套性 3.声明与定义分离的命名空间4.命名空间的展开5.多个命名空间中命名冲突6.对于命名空间的推荐写法 3.iostream1.cout和endl2.cin 3.缺省参数1.缺省参数的形式2.缺…

从零开始学习调用百度地图网页API:二、初始化地图,鼠标交互创建信息窗口

目录 代码结构headbodyscript 调试 代码 <!DOCTYPE html> <html> <head><meta http-equiv"Content-Type" content"text/html; charsetutf-8" /><meta name"viewport" content"initial-scale1.0, user-scalable…

进阶JAVA篇- Math 类和 System 类和 Runtime 类的常用API(三)

目录 API 1.0 Math 类的说明 1.1 Math 类中的 abs() 方法 1.2 Math 类中的 ceil () 方法 1.3 Math 类中的 floor () 方法 1.4 Math 类中的 round () 方法 1.5 Math 类中的 max() 和 min() 方法 1.6 Math 类中的 pow(double a , double b) 方法 1.7 Math 类中的 random&a…

案例|美创科技守护健康“一盘棋”,医共体整体数据安全建设实践

以医共体之“通”&#xff0c;破解看病之“痛”。在县域组建医疗共同体&#xff0c;逐步实现区域内医疗资源共享&#xff0c;推动形成基层首诊、双向转诊、急慢分治、上下联动的分级诊疗模式&#xff0c;是实现“首诊在基层、大病不出县”&#xff0c;更好地为全县人民群众服务…

生产环境中常用Linux命令

太简单的我就不讲解啦,浪费时间,直接将生产中常用的 文章目录 1.总纲2.整机 top3.CPU vmstat3. 内存 free4. 硬盘: df5. 磁盘IO iostat6. 网络IO ifstat7: 内存过高的情景排查 1.总纲 整机:topcpu:vmstat内存:free硬盘:df磁盘io: iostat网络io:ifstat 2.整机 top 首先们要查…

阿里云服务器10M带宽收费价格表

阿里云服务器10M公网带宽收费价格表&#xff0c;地域不同带宽价格不同&#xff0c;阿里云百科以华东2&#xff08;杭州&#xff09;地域为例&#xff0c;阿里云10M带宽一年85折优惠价格5355元&#xff0c;10M带一个月525元&#xff0c;阿里云百科aliyunbaike.com分享阿里云服务…

第二证券:全涨停,上市以来首次!

10月12日&#xff0c;国内产品商场大面积飘红&#xff0c;其间有色、化工板块上涨明显&#xff0c;碳酸锂期货自本年7月份上市以来初次呈现合约悉数触及涨停&#xff0c;到收盘时&#xff0c;主力合约LC2401上涨10750元/吨&#xff0c;涨幅6.97%&#xff0c;报164950元/吨。 碳…

pytorch学习第三篇:梯度

下面介绍了在pytorch中如何进行梯度运算,并介绍了在运行梯度计算时遇到的问题,通过解决一个个问题,来深入理解梯度计算。 梯度计算 import torch x = torch.rand(3,4,requires_grad=True) b = torch.rand(4,3,requires_grad=True) print(x,b)y = x@bt = y.sum()求导数 t.…

CrossOver2024下载安装详细图文教程

本文将集中为各位小伙伴们介绍一下类虚拟机软件CrossOver Mac是如何进行下载、安装教程。 1、双击下载好的文件&#xff08;若为压缩文件&#xff0c;请先解压&#xff09;&#xff0c;随后弹出“是否要把程序移动到应用程序文件夹中&#xff1f;”&#xff0c;一般我们选择移…

快排超详细,Leetcode排序数组题目带你升华掌握

大家好&#xff0c;这里是Dark FalmeMater。 这篇文章我将超级仔细地讲解快速排序&#xff0c;快排之所以叫快排&#xff0c;到底有多快&#xff0c;为什么这么快&#xff0c;还有快速排序的优化和改进&#xff0c;通过这篇文章你一定会对快排有进一步的掌握。 文章目录 Hoare版…

消息队列缓存,以蓝牙消息服务为例

前言 消息队列缓存&#xff0c;支持阻塞、非阻塞模式&#xff1b;支持协议、非协议模式 可自定义消息结构体数据内容 使用者只需设置一些宏定义、调用相应接口即可 这里我用蓝牙消息服务举例 有纰漏请指出&#xff0c;转载请说明。 学习交流请发邮件 1280253714qq.com 原…

4D视频数据集

RealGraph: A Multiview Dataset for 4D Real-world Context Graph Generation RealGraph是一个全新的4D视频数据集&#xff0c;旨在支持“Context Graph Generation (CGG)”任务。CGG任务的目标是从动态场景的校准多视图视频中恢复出物体的语义信息&#xff0c;包括坐标、轨迹…

Idea报错 java: 程序包org.springframework.boot不存在 解决方法

发现我的是因为更改了maven的主路径和本地仓库路径&#xff0c;但是新建了一个工程后&#xff0c;设置就恢复默认了。需要重新设置正确路径。 应用后会重新下载依赖项 之后虽然还会报错&#xff0c;但是已经不影响项目运行&#xff0c;配置成功

高速数字化仪和AWG在车辆总线(CAN/LIN/PSI5)测试中的应用(一)

引言 模块化仪器比传统仪器的尺寸大大减小&#xff0c;适合安装在电路卡上&#xff0c;同时也可以将多个卡插入具有通用计算机接口、电源和互连的框架中。模块化仪器框架包括使用标准PCIe接口的计算机、PXI测试框架或基于LXI的盒子&#xff0c;工程师通常会使用多个卡并将其配…

基于Docker-compose搭建LNMP

1、配置nginx 创建nginx目录上传所需压缩包将wordress解压到指定路径下 配置Dockerfile文本文件 vim DockerfileFROM centos:7 MAINTAINER this is nginx image <zgc> RUN yum -y install pcre-devel zlib-devel gcc gcc-c make RUN useradd -M -s /sbin/nologin nginx…

【RcoketMQ】RcoketMQ 5.0新特性(二)- Pop消费模式

Pop模式消费和消息粒度负载均衡 在RocketMQ 5.0之前&#xff0c;消费有两种方式可以从Broker获取消息&#xff0c;分别为Pull模式和Push模式。 Pull模式&#xff1a;消费需要不断的从阻塞队列中获取数据&#xff0c;如果没有数据就等待&#xff0c;这个阻塞队列中的数据由消息…

springBoot 自动配置机制

springBoot 自动配置机制 自动配置的tomcat、springmvc等默认包扫描规则自定义扫描路径配置默认值按需加载自动配置总结 自动配置的tomcat、springmvc等 导入场景&#xff0c;容器中就会自动配置好这个场景的核心组件 以前&#xff1a;dispatcherservlet、viewresolver、chara…