旅游网站建设策划书模板邓州网站建设
web/
2025/9/29 3:46:07/
文章来源:
旅游网站建设策划书模板,邓州网站建设,微信公众号运营大学,微信小程序对接广告联盟winform 由于不是数据驱动, 页面想刷新数据必须刷新控件, wpf则不用. 可以利用wpf 的数据绑定和IOC, 页面中的消息传递, itemscontrol 实现大量数据刷新, 上位机页面不卡顿
跨页面传值, 可以用两种方法: Toolkit.Mvvm中的Message和IOC. 下面是代码:
using Microsoft.Extensio…winform 由于不是数据驱动, 页面想刷新数据必须刷新控件, wpf则不用. 可以利用wpf 的数据绑定和IOC, 页面中的消息传递, itemscontrol 实现大量数据刷新, 上位机页面不卡顿
跨页面传值, 可以用两种方法: Toolkit.Mvvm中的Message和IOC. 下面是代码:
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/// /summarypublic 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.AddSingletonNewMainView();services.AddSingletonPage1();services.AddSingletonPage2();services.AddSingletonPage3();services.AddSingletonPage5();var viewModelTypes Assembly.GetExecutingAssembly().GetTypes().Where(t t.Name.EndsWith(ViewModel));foreach (var type in viewModelTypes){services.AddScoped(type);}//services.AddSingletonPage2(sp new Page2()//{// DataContext sp.GetServicePage2ViewModel()//});#endregion//PLC注入services.AddSingletonPLCModels();return services.BuildServiceProvider();}private void Application_Startup(object sender, StartupEventArgs e){NewMainView newMainView this.Services?.GetServiceNewMainView();newMainView.Show();//MainView? mainView this.Services?.GetServiceMainView();//mainView.DataContext this.Services?.GetServiceMainViewModel();//mainView.Show();}}
}
模型定义:
using CommunityToolkit.Mvvm.ComponentModel;
using System.ComponentModel;namespace NavTest.Eneities
{public partial class PLCModel : INotifyPropertyChanged{public int Id { get; set; }public string? Name { get; set; }public string? DataType { get; set; }//[ObservableProperty]//private int plcValue;private int plcValue;public event PropertyChangedEventHandler? PropertyChanged;public int PlcValue{get plcValue;set{if (plcValue ! value){plcValue value;NotifyPropertyChanged(nameof(PlcValue));}}}private void NotifyPropertyChanged(string propertyName){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}}
}
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace NavTest.Eneities
{public partial class PLCModels{public PLCModels(){for (int i 0; i 200; i){pLCModels.Add(new PLCModel(){Id i,PlcValue i,Name $名字{i},});}}public ObservableCollectionPLCModel pLCModels { get; set; } new();}
}
主页面产生数据:
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using NavTest.Eneities;
using NavTest.Views;
using NPOI.SS.Formula.Functions;
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, PLCModels pLCModelsIoc){this.page1 page1;this.page2 page2;this.page3 page3;this.page5 page5;this.pLCModelsIoc pLCModelsIoc;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;private PLCModels pLCModelsIoc;[ObservableProperty]private ObservableCollectionPLCModel 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);//用Message传递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);}}//用Ioc传递for (int j 0; j 200; j){var random new Random();pLCModelsIoc.pLCModels[j].PlcValue random.Next(1, 500);}}});}}
}
用ViewModel的Message 传值:
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;
using NavTest.Eneities;
using System.Collections.ObjectModel;namespace NavTest.ViewModels
{/// summary/// 用ViewModel 的 Message传递变化的值/// /summarypublic partial class Page2ViewModel : ObservableRecipient, IRecipientPropertyChangedMessageint{[ObservableProperty]private ObservableCollectionPLCModel pLCModels;public Page2ViewModel(){IsActive true;}public void Receive(PropertyChangedMessageint message){if (message.Sender is NewMainViewModel vm){this.PLCModels vm.PLCModels;}}}
}
UserControlx:ClassNavTest.Views.Page2xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:conclr-namespace:ValueConverters;assemblyValueConvertersxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:hchttps://handyorg.github.io/handycontrolxmlns:ihttp://schemas.microsoft.com/xaml/behaviorsxmlns:localclr-namespace:NavTest.Viewsxmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:mvclr-namespace:NavTest.ViewModelsxmlns:sysclr-namespace:System;assemblymscorlibxmlns:ttclr-namespace:NavTest.Eneitiesxmlns:vcclr-namespace:NavTest.Componentsd:DataContext{d:DesignInstance mv:Page2ViewModel}d:DesignHeight450d:DesignWidth800FontSize22mc:IgnorabledGridGrid.RowDefinitionsRowDefinition Height0.1* /RowDefinition //Grid.RowDefinitionsStackPanel HorizontalAlignmentCenter VerticalAlignmentCenterTextBlock ForegroundWhite Text用viewModel的消息传递 //StackPanelScrollViewer Grid.Row1 VerticalScrollBarVisibilityAutoItemsControl AlternationCount2 ItemsSource{Binding PLCModels}ItemsControl.ItemsPanelItemsPanelTemplateWrapPanel //ItemsPanelTemplate/ItemsControl.ItemsPanelItemsControl.ItemTemplateDataTemplateBorder x:Nameborder Padding2 BorderThickness2 BorderBrushCyanStackPanelTextBlock ForegroundWhite Text{Binding Id} /TextBlock ForegroundWhite Text{Binding Name} /TextBlock ForegroundWhite Text{Binding PlcValue} //StackPanel/BorderDataTemplate.TriggersTrigger PropertyItemsControl.AlternationIndex Value1Setter TargetNameborder PropertyBackground Valuegreen //Trigger/DataTemplate.Triggers/DataTemplate/ItemsControl.ItemTemplate/ItemsControl/ScrollViewer/Grid/UserControl
用IOC传值:
using CommunityToolkit.Mvvm.ComponentModel;
using NavTest.Eneities;
using System.Collections.ObjectModel;namespace NavTest.ViewModels
{/// summary/// 用Ioc传递变化的值/// /summarypublic partial class Page3ViewModel : ObservableObject{public Page3ViewModel(PLCModels pLCModelsIoc){pLCModels pLCModelsIoc.pLCModels;}[ObservableProperty]private ObservableCollectionPLCModel pLCModels;}
}
UserControlx:ClassNavTest.Views.Page3xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:hchttps://handyorg.github.io/handycontrolxmlns:ihttp://schemas.microsoft.com/xaml/behaviorsxmlns:localclr-namespace:NavTest.Viewsxmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:mvclr-namespace:NavTest.ViewModelsxmlns:sysclr-namespace:System;assemblymscorlibxmlns:ttclr-namespace:NavTest.Eneitiesxmlns:vcclr-namespace:NavTest.Componentsd:DataContext{d:DesignInstance mv:Page3ViewModel}d:DesignHeight450d:DesignWidth800FontSize24mc:IgnorabledGridGrid.RowDefinitionsRowDefinition Height0.1* /RowDefinition //Grid.RowDefinitionsStackPanel HorizontalAlignmentCenter VerticalAlignmentCenterTextBlock ForegroundWhite Text用Ioc传递 //StackPanelScrollViewer Grid.Row1 VerticalScrollBarVisibilityAutoItemsControl AlternationCount2 ItemsSource{Binding PLCModels}ItemsControl.ItemsPanelItemsPanelTemplateWrapPanel //ItemsPanelTemplate/ItemsControl.ItemsPanelItemsControl.ItemTemplateDataTemplateBorderx:NameborderPadding2BorderBrushYellowBorderThickness2StackPanelTextBlock ForegroundWhite Text{Binding Id} /TextBlock ForegroundWhite Text{Binding Name} /TextBlock ForegroundWhite Text{Binding PlcValue} //StackPanel/BorderDataTemplate.TriggersTrigger PropertyItemsControl.AlternationIndex Value1Setter TargetNameborder PropertyBackground ValueBlue //Trigger/DataTemplate.Triggers/DataTemplate/ItemsControl.ItemTemplate/ItemsControl/ScrollViewer/Grid
/UserControl
效果图:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/83679.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!