当前位置: 首页 > 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.jsqmd.com/news/160/

相关文章:

  • 实用指南:目标检测如何将同时有方形框和旋转框的json/xml标注转为txt格式
  • ctfshow web351
  • ctfshow web353
  • fxztgxj5.dll fxzrs4qj.dll fxztgxa5.dll fxzrs3qj.dll fxzpmc1.dll fxzrs2qj.dll fxzmpxa5.dll - 实践
  • Linux虚拟机常用命令与Hadoop生态组件启动大全
  • 测试新手必学:10个让Bug无处遁形的黑盒测试技巧
  • private void Form1_Load与构造方法前执行顺序
  • 数据分类分级如何高效低成本落地?|高效智能的数据分类分级产品推荐(2025)
  • 文化课暂时计划
  • BGP路由属性与选路-1
  • private void Form1_Load和 private void Form1_Activated 方法区别
  • BGP反射路由器
  • HarmonyOS Stage模型与ArkTS:现代应用开发的核心架构与最佳实践 - 详解
  • 【CV】图像超分辨率的一些基础概念
  • 完整教程:苹果WWDC25开发秘技揭秘:SwiftData3如何重新定义数据持久化
  • H5 页面与 Web 页面的制作方法 - 实践
  • Python面试题及详细答案150道(116-125) -- 性能优化与调试篇 - 实践
  • 完整教程:构建基石:Transformer架构
  • Spring Cloud Gateway吞吐量优化
  • 【先记录一下】windows下使用的lazarus/fpc安装到中文的目录时出错的问题
  • 物联网摄像头硬件设计秘籍:低成本与低功耗的平衡之道
  • CF182C Optimal Sum
  • 完整教程:WinForms 项目里生成时选择“首选目标平台 32 位导致有些电脑在获取office word对象时获取不到
  • 关于网络社交
  • nginx学习笔记一:基础概念
  • HTB UNIV CTF 24 Armaxix靶场漏洞链:命令注入与账户接管实战
  • 【c++进阶系列】:万字详解AVL树(附源码实现) - 教程
  • 【JAVA接口自动化】JAVA如何读取Yaml文档
  • 完整教程:uni-app 常用钩子函数:从场景到实战,掌握开发核心
  • PyTorch Weight Decay 技术指南