当前位置: 首页 > news >正文

WPF viewmodel retrieve matched view /window

private Window? GetWindow()
{foreach (Window win in Application.Current.Windows){if (win.DataContext==this){return win;}}return null;
}

 

 

Install-Package CommunityToolkit.mvvm;
Install-Package Micorosoft.Extensions.DependencyInjection;

 

 

 

//app.xaml
<Application x:Class="WpfApp28.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:WpfApp28"><Application.Resources></Application.Resources>
</Application>//app.xaml.cs
using Microsoft.Extensions.DependencyInjection;
using System.Configuration;
using System.Data;
using System.Windows;namespace WpfApp28
{/// <summary>/// Interaction logic for App.xaml/// </summary>public partial class App : Application{ServiceProvider serviceProvider;protected override void OnStartup(StartupEventArgs e){base.OnStartup(e);var services = new ServiceCollection();ConfigureServices(services);serviceProvider = services.BuildServiceProvider();var mainWin = serviceProvider.GetRequiredService<MainWindow>();mainWin?.Show();}private static void ConfigureServices(ServiceCollection services){services.AddSingleton<IIDService, IDService>();services.AddSingleton<INameService, NameService>();services.AddSingleton<IISBNService, ISBNService>();services.AddSingleton<MainVM>();services.AddSingleton<MainWindow>();}protected override void OnExit(ExitEventArgs e){base.OnExit(e);serviceProvider?.Dispose();}}}//mainwindow.xaml
<Window x:Class="WpfApp28.MainWindow"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:WpfApp28"WindowState="Maximized"mc:Ignorable="d"Title="{Binding StatusMsg,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Height="450" Width="800"><Grid><ListBoxItemsSource="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"VirtualizingPanel.IsVirtualizing="True"VirtualizingPanel.VirtualizationMode="Recycling"ScrollViewer.IsDeferredScrollingEnabled="True"><ListBox.ItemTemplate><DataTemplate><Grid       Width="{Binding DataContext.GridWidth,RelativeSource={RelativeSource AncestorType=Window}}"Height="{Binding DataContext.GridHeight,RelativeSource={RelativeSource AncestorType=Window}}"><Grid.Background><ImageBrush ImageSource="{Binding ImgSource}"/></Grid.Background><Grid.Resources><Style TargetType="TextBlock"><Setter Property="FontSize" Value="30"/><Setter Property="HorizontalAlignment" Value="Center"/><Setter Property="VerticalAlignment" Value="Center"/><Style.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter Property="FontSize" Value="50"/><Setter Property="Foreground" Value="Red"/></Trigger></Style.Triggers></Style></Grid.Resources><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition/><RowDefinition/></Grid.RowDefinitions><TextBlock Text="{Binding Id}" Grid.Column="0" Grid.Row="0"/><TextBlock Text="{Binding Name}" Grid.Column="1" Grid.Row="0"/><TextBlock Text="{Binding ISBN}" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1"/></Grid></DataTemplate></ListBox.ItemTemplate></ListBox>         </Grid>
</Window>//window.xaml.cs
using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace WpfApp28
{/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}public MainWindow(MainVM vm){InitializeComponent();this.DataContext=vm;this.SizeChanged+=MainWindow_SizeChanged;this.Loaded+=async (s, e) =>{await LoadDataAsync();};}private async Task LoadDataAsync(){if (DataContext is MainVM vm){await vm.InitDataAsync();}}private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e){if (DataContext is MainVM vm){var fe = this.Content as FrameworkElement;if (fe==null){return;}vm.GridWidth=fe.ActualWidth;vm.GridHeight=fe.ActualHeight/2;}}}public partial class MainVM : ObservableObject{IIDService idService;INameService nameService;IISBNService isbnService;private Stopwatch watch;public MainVM(IIDService idServiceValue,INameService nameServiceValue,IISBNService isbnServiceValue){idService=idServiceValue;nameService=nameServiceValue;isbnService=isbnServiceValue;watch=Stopwatch.StartNew();}private void InitVisualTreesList(){VisualChildrenCollection=new ObservableCollection<string>();Window mainWin = GetWindow();if (mainWin!=null){var visualsList = GetVisualChildren<Visual>(mainWin);foreach (var visual in visualsList){VisualChildrenCollection.Add(visual.GetType().Name);}}string visualChildrenStr = string.Join("\n", VisualChildrenCollection.ToArray());Debug.WriteLine($"Visual Tree children:\n{visualChildrenStr}\n\n\n");}public async Task InitDataAsync(){string imgDir = @"../../../Images";if (!Directory.Exists(imgDir)){return;}var imgs = Directory.GetFiles(imgDir);if (imgs==null ||!imgs.Any()){return;}int imgsCount = imgs.Count();BooksCollection=new ObservableCollection<Book>();List<Book> booksList = new List<Book>();await Task.Run(async () =>{for (int i = 0; i<1000001; i++){booksList.Add(new Book(){Id=idService.GetID(),Name=nameService.GetName(),ISBN=isbnService.GetISBN(),ImgSource=GetImgSourceViaUrl(imgs[i%imgsCount])});if (i<1000&& i%100==0){await PopulateBooksCollectionAsync(booksList);}else if (i>=1000 && i%1000000==0){await PopulateBooksCollectionAsync(booksList);}}if (booksList.Any()){await PopulateBooksCollectionAsync(booksList);}});InitVisualTreesList();InitLogicalTreesList();}private void InitLogicalTreesList(){Window mainWin = GetWindow();var logicalChildren = GetAllLogicalChildren(mainWin);string logicalTreesStr = string.Join("\n", logicalChildren.Select(x => x.GetType().Name));Debug.WriteLine(logicalTreesStr);}private ImageSource GetImgSourceViaUrl(string imgUrl){BitmapImage bmi = new BitmapImage();bmi.BeginInit();bmi.UriSource=new Uri(imgUrl, UriKind.RelativeOrAbsolute);bmi.CacheOption=BitmapCacheOption.OnDemand;bmi.EndInit();bmi.Freeze();return bmi;}private async Task PopulateBooksCollectionAsync(List<Book> booksList){var tempList = booksList.ToList();booksList.Clear();await Application.Current.Dispatcher.InvokeAsync(() =>{foreach (var bk in tempList){BooksCollection.Add(bk);}StatusMsg=$"Loaded {BooksCollection.Count} items,{GetMemory()},{GetTimeCost()}";}, System.Windows.Threading.DispatcherPriority.Background);}private string GetMemory(){var procMemory = Process.GetCurrentProcess().PrivateMemorySize64/1024.0d/1024.0d;return $"Memory:{procMemory.ToString("#,##0.00")} M";}private string GetTimeCost(){return $"Time cost: {watch.Elapsed.TotalSeconds} seconds";}private static IEnumerable<T> GetVisualChildren<T>(DependencyObject parent) where T : DependencyObject{if (parent == null){yield break;}int childrenCount = VisualTreeHelper.GetChildrenCount(parent);for (int i = 0; i<childrenCount; i++){DependencyObject child = VisualTreeHelper.GetChild(parent, i);if (child is T tChild){yield return tChild;}foreach (var descedant in GetVisualChildren<T>(child)){yield return descedant;}}}public static IEnumerable<DependencyObject> GetAllLogicalChildren(DependencyObject parent){if (parent == null){yield break;}foreach (var child in LogicalTreeHelper.GetChildren(parent)){if (child is DependencyObject depChild){yield return depChild;// Recurse down into this child's childrenforeach (var descendant in GetAllLogicalChildren(depChild)){yield return descendant;}}}}private Window? GetWindow(){foreach (Window win in Application.Current.Windows){if (win.DataContext==this){return win;}}return null;}[ObservableProperty]private double gridWidth;[ObservableProperty]private double gridHeight;[ObservableProperty]private ObservableCollection<Book> booksCollection;[ObservableProperty]private string statusMsg;[ObservableProperty]private ObservableCollection<string> visualChildrenCollection;}public class Book{public int Id { get; set; }public string Name { get; set; }public string ISBN { get; set; }public ImageSource ImgSource { get; set; }}public interface IIDService{int GetID();}public class IDService : IIDService{int id = 0;public int GetID(){return Interlocked.Increment(ref id);}}public interface INameService{string GetName();}public class NameService : INameService{int idx = 0;public string GetName(){return $"Name_{Interlocked.Increment(ref idx)}";}}public interface IISBNService{string GetISBN();}public class ISBNService : IISBNService{int idx = 0;public string GetISBN(){return $"ISBN_{Interlocked.Increment(ref idx)}_{Guid.NewGuid():N}";}}
}

 

http://www.zskr.cn/news/8202.html

相关文章:

  • ctfshow web353
  • fxztgxj5.dll fxzrs4qj.dll fxztgxa5.dll fxzrs3qj.dll fxzpmc1.dll fxzrs2qj.dll fxzmpxa5.dll - 实践
  • 测试新手必学:10个让Bug无处遁形的黑盒测试技巧
  • 数据分类分级如何高效低成本落地?|高效智能的数据分类分级产品推荐(2025)
  • 文化课暂时计划
  • private void Form1_Load和 private void Form1_Activated 方法区别
  • BGP反射路由器
  • 完整教程:苹果WWDC25开发秘技揭秘:SwiftData3如何重新定义数据持久化
  • H5 页面与 Web 页面的制作方法 - 实践
  • Spring Cloud Gateway吞吐量优化
  • 完整教程:WinForms 项目里生成时选择“首选目标平台 32 位导致有些电脑在获取office word对象时获取不到
  • nginx学习笔记一:基础概念
  • AUTOSAR进阶图解==>AUTOSAR_SWS_PDURouter - 实践
  • getDefaultMidwayLoggerConfig报错;解决方法。
  • Python实现Elman RNN与混合RNN神经网络对航空客运量、啤酒产量、电力产量时间序列数据预测可视化对比
  • 解题报告-老逗找基友 (friends)
  • Python_occ 学习记录 | 细观建模(1) - 教程
  • 【小白也能懂】PyTorch 里的 0.5 到底是干啥的?——一次把 Normalize 讲透! - 教程
  • 051-Web攻防-文件安全目录安全测试源码等
  • error: xxxxx does not have a commit checked out
  • linux 命令语句
  • UM2003A 一款 200 ~ 960MHz ASK/OOK +18dBm 发射功率的单发射
  • 达芬奇(DaVinci Reslove)字体文件 bugb标签
  • 深入解析:上门按摩平台 “0 抽成 + 无底薪” 双模式拆解:如何让技师主动创收?
  • SUB-1G 无线收发芯片 DP10RF001 低功耗 (G) FSK/OOK 智能门锁,资产追踪、无线监控
  • 中电金信 :MCP在智能体应用中的挑战与对策
  • CSP 2025 复赛复习总目标与计划
  • WPF 调用 Windows 桌面右键新增文件菜单的实现方案
  • 重看P4211 [LNOI2014] LCA 以及 P5305 [GXOI/GZOI2019] 旧词 题解
  • 25.9.19随笔联考总结