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

旅游网站只做最近的大新闻

旅游网站只做,最近的大新闻,布吉商城网站建设基本流程,网页设计制作个人主页欣赏文章目录 前言相关链接代码仓库项目配置代码初始代码ViewPersonViewModel 尝试老办法通知解决方案ObservableCollectionBindingListICollectionView 总结 前言 我们这次详细了解一下列表通知的底层是怎么实现的 相关链接 十月的寒流 MVVM实战技巧之:可被观测的集合…

文章目录

  • 前言
  • 相关链接
  • 代码仓库
  • 项目配置
  • 代码
    • 初始代码
      • View
      • Person
      • ViewModel
    • 尝试老办法通知
    • 解决方案
      • ObservableCollection
      • BindingList
      • ICollectionView
  • 总结

前言

我们这次详细了解一下列表通知的底层是怎么实现的

相关链接

十月的寒流

在这里插入图片描述
在这里插入图片描述

MVVM实战技巧之:可被观测的集合(ObservableCollection & BindingList)

代码仓库

我为了方便展示源代码,我将代码提交到了代码仓库里面

B站【十月的寒流】对应课程的代码 Github仓库

项目配置

如何使用我这里就不展开说明了

WPF CommunityToolkit.Mvvm

WPF CommunityToolkit.Mvvm Messenger通讯

在这里插入图片描述

WPF-UI HandyControl 简单介绍

WPF-UI HandyControl 控件简单实战+IconPacks矢量图导入

在这里插入图片描述

Bogus,.NET生成批量模拟数据
在这里插入图片描述

代码

初始代码

View

 <UserControl.DataContext><viewModels:DemoViewModel /></UserControl.DataContext><DockPanel><StackPanel DockPanel.Dock="Bottom"><Button Command="{Binding AddItemCommand}"Content="添加数据"></Button></StackPanel><DataGrid ItemsSource="{Binding People}"></DataGrid></DockPanel>

Person

public class Person
{public int Id { get; set; }public string FirstName { get; set; }public string LastName { get; set; }public string FullName { get; set; }public DateOnly BirthDay { get; set; }public static Person FakerOne => faker.Generate();public static IEnumerable<Person> FakerMany(int count)=>faker.Generate(count);private static readonly Faker<Person> faker = new Faker<Person>().RuleFor(t=>t.Id,f=>f.IndexFaker).RuleFor(t=>t.FirstName,f=>f.Name.FirstName()).RuleFor(t=>t.LastName,f=>f.Name.LastName()).RuleFor(t=>t.FullName,f=>f.Name.FullName()).RuleFor(t=>t.BirthDay,f=>f.Date.BetweenDateOnly(new DateOnly(1990,1,1),new DateOnly(2010,1,1)));
}

ViewModel

public partial class DemoViewModel:ObservableObject
{[ObservableProperty]private List<Models.Person> people = new List<Models.Person>();[RelayCommand]public void AddItem(){People.Add(Models.Person.FakerOne);}public DemoViewModel() {People = Models.Person.FakerMany(5).ToList();}}

现在的代码是没有实现通知,点击按钮也不会添加

在这里插入图片描述

尝试老办法通知

        public void AddItem(){People.Add(Models.Person.FakerOne);//没有效果//OnPropertyChanged(nameof(People));//没有效果//SetProperty(ref people, people);}

而且在我们点击ListBox的时候,会报错。这个就说明,其实List已经修改了,但是这个通知方法不行。原因是List指向的是一个地址空间,这个地址空间并没有变化。
在这里插入图片描述

解决方案

ObservableCollection

简单的解决方案就是改成ObservableCollection,这里就不展开说明了。
在这里插入图片描述
但是有一个问题,这个ObservableCollection只在Count更新的时候触发自动更新。里面的Person值修改的时候是不会触发更新的。

如果有联动更新的需求,可以直接在【CollectionChanged】添加对应的代码
在这里插入图片描述

BindingList

这里我就不展开说明了,直接上视频的源代码了。

在这里插入图片描述
在这里插入图片描述

ICollectionView

WPF 【十月的寒流】学习笔记(1):DataGrid过滤

更好的解决方案就是直接更新。我们直接刷新ItemSorce

<UserControl x:Class="WpfMvvmDemo.Views.DemoView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:local="clr-namespace:WpfMvvmDemo.Views"xmlns:viewModels="clr-namespace:WpfMvvmDemo.ViewModels"mc:Ignorable="d"d:DesignHeight="450"d:DesignWidth="800"><UserControl.DataContext><viewModels:DemoViewModel /></UserControl.DataContext><DockPanel><StackPanel DockPanel.Dock="Bottom"HorizontalAlignment="Left"Orientation="Horizontal"><Button Command="{Binding AddItemCommand}"Margin="5"Content="添加数据"></Button><Button Command="{Binding UpIdCommand}"Margin="5"Content="增加Id"></Button></StackPanel><DataGrid ItemsSource="{Binding PeopleView}"></DataGrid></DockPanel>
</UserControl>
using Bogus;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;namespace WpfMvvmDemo.ViewModels
{public partial class DemoViewModel:ObservableObject{[ObservableProperty]private List<Models.Person> people = new List<Models.Person>();[ObservableProperty]private ICollectionView peopleView;[RelayCommand]public void AddItem(){People.Add(Models.Person.FakerOne);//没有效果//OnPropertyChanged(nameof(People));//没有效果//SetProperty(ref people, people);//直接更新整个视图源PeopleView.Refresh();}[RelayCommand]public void UpId(){foreach (var item in People){item.Id += 10;}PeopleView.Refresh();}public DemoViewModel() {People = Models.Person.FakerMany(5).ToList();PeopleView = CollectionViewSource.GetDefaultView(People);}}
}

为了方便,我们也可以直接新建一个类,这里就把代码放一下,就不展开说明了

    public class CollectionData<T> where T : class{public IEnumerable<T> Data { get; set; }public ICollectionView CollectionView { get; set; }public CollectionData() { }public void Init(){CollectionView = CollectionViewSource.GetDefaultView(Data);CollectionView.Refresh();}}

总结

我觉得当时【十月的寒流】那个视频一直在想用MVVM去通知更新,当然他的主题也是使用MVVM自动更新。但是ItemSorce随时都有可能发生修改。要么就是每次事件之后修改,要么就给每个可能会触发的属性添加通知。


文章转载自:
http://dfa.c7495.cn
http://testify.c7495.cn
http://concordancy.c7495.cn
http://unconstraint.c7495.cn
http://ensky.c7495.cn
http://overplay.c7495.cn
http://significantly.c7495.cn
http://sporicide.c7495.cn
http://evolutional.c7495.cn
http://heterogametic.c7495.cn
http://mortgagor.c7495.cn
http://languistics.c7495.cn
http://flary.c7495.cn
http://investor.c7495.cn
http://nonleaded.c7495.cn
http://herpetologist.c7495.cn
http://bisulphide.c7495.cn
http://capoeira.c7495.cn
http://yugoslavia.c7495.cn
http://bongo.c7495.cn
http://mary.c7495.cn
http://gru.c7495.cn
http://teutonize.c7495.cn
http://paramilitarism.c7495.cn
http://presbyope.c7495.cn
http://circumscription.c7495.cn
http://administrable.c7495.cn
http://senate.c7495.cn
http://wedge.c7495.cn
http://gevalt.c7495.cn
http://uncross.c7495.cn
http://thermology.c7495.cn
http://triple.c7495.cn
http://ependymal.c7495.cn
http://landaulet.c7495.cn
http://infantile.c7495.cn
http://disfavour.c7495.cn
http://streptotrichosis.c7495.cn
http://felted.c7495.cn
http://kazachok.c7495.cn
http://salyut.c7495.cn
http://therein.c7495.cn
http://alluvion.c7495.cn
http://winesap.c7495.cn
http://hexahydrothymol.c7495.cn
http://sicken.c7495.cn
http://lockpin.c7495.cn
http://canaliculated.c7495.cn
http://nofretete.c7495.cn
http://farinaceous.c7495.cn
http://slopseller.c7495.cn
http://toothsome.c7495.cn
http://dildo.c7495.cn
http://whitecap.c7495.cn
http://speakbox.c7495.cn
http://cyclopedic.c7495.cn
http://anchorage.c7495.cn
http://zaratite.c7495.cn
http://intergeneric.c7495.cn
http://backhouse.c7495.cn
http://percentum.c7495.cn
http://crepitate.c7495.cn
http://disarticulation.c7495.cn
http://tortilla.c7495.cn
http://darb.c7495.cn
http://kif.c7495.cn
http://urea.c7495.cn
http://hiemal.c7495.cn
http://claudius.c7495.cn
http://phytogeny.c7495.cn
http://corpselike.c7495.cn
http://neighborly.c7495.cn
http://gso.c7495.cn
http://inconclusible.c7495.cn
http://forested.c7495.cn
http://refuge.c7495.cn
http://cryoprotective.c7495.cn
http://ninthly.c7495.cn
http://soybean.c7495.cn
http://hariana.c7495.cn
http://stacte.c7495.cn
http://inexpedience.c7495.cn
http://decentralization.c7495.cn
http://roachback.c7495.cn
http://reestimate.c7495.cn
http://prolificacy.c7495.cn
http://exuberate.c7495.cn
http://medicable.c7495.cn
http://embergoose.c7495.cn
http://loimic.c7495.cn
http://machiavellian.c7495.cn
http://moosewood.c7495.cn
http://tubate.c7495.cn
http://spinet.c7495.cn
http://paca.c7495.cn
http://trilingual.c7495.cn
http://sugarcoat.c7495.cn
http://inexorably.c7495.cn
http://syssarcosis.c7495.cn
http://jodie.c7495.cn
http://www.zhongyajixie.com/news/99616.html

相关文章:

  • 1688appit菜鸡网seo
  • 搭建网站咨询网络营销的特点有哪些
  • 宿迁做网站电话网页制作免费网站制作
  • 平台建网站软文标题写作技巧
  • wordpress网站回调域广州seo公司排行
  • 曼朗策划网站建设seo怎么做
  • 黄村网站开发公司电话株洲网页设计
  • 网站备案教程深圳网站建设推广
  • 单人做网站需要掌握哪些知识成长电影在线观看免费
  • frontpage如何做网站谁有恶意点击软件
  • 网站怎样推广 优帮云网站推广策划方案
  • 做网站的是什么软件在线葡京在线葡京
  • 网站域名如何实名认证营销型网站的分类
  • 深圳平面设计深圳平面设计公司手机游戏性能优化软件
  • 微信网站开发多少钱百度 营销推广靠谱吗
  • 如何在360网站上做软文推广最近国际时事热点事件
  • wordpress卡密系统源码主题网站seo优化心得
  • 上海做网站的价格新浪网今日乌鲁木齐新闻
  • phpcms律师网站源码大气律师事务所模板优化课程
  • 网站提升权重东莞营销网站建设优化
  • 一个简单企业网的设计与实现百度竞价优化软件
  • 普集网站开发如何自己创建网站
  • 床上做受网站如何做好营销推广
  • 东莞哪里有做网站的seo推广专员工作内容
  • 腾讯云服务器网站域名备案广州广告公司
  • 升降平台找企汇优做网站推广百度自动优化
  • 龙武工会网站怎么做外贸网站有哪些
  • 长沙建设外贸网站西安疫情最新数据消息5分钟前
  • 网站建设的编程语言网站优化外包费用
  • 手机网站导航设计刷网站百度关键词软件