时间:2021年09月28日 | 作者 : aaronyang | 分类 : WPF | 浏览: 1321次 | 评论 0 人
相关文章
Ay写给2022的纯xaml [wpf4net5] - Caliburn-Micro[1/16],MyGet-WPF-aaronyang技术分享 (ayjs.net) Download
Ay写给2022的纯xaml [wpf4net5] - Caliburn-Micro-绑定[2/16]-WPF-aaronyang技术分享 (ayjs.net) Download
Ay写给2022的纯xaml [wpf4net5] - Caliburn-Micro-调用vm的方法[3/16]-WPF-aaronyang技术分享 (ayjs.net) Download
Ay写给2022的纯xaml [wpf4net5] - Caliburn-Micro-Coroutine协程[4/16]-WPF-aaronyang技术分享 (ayjs.net) Download
Ay写给2022的纯xaml [wpf4net5] - Caliburn-Micro-Execute 异步里面更新界面UI[5/16]-WPF-aaronyang技术分享 (ayjs.net)
第5天CM.zip
Ay写给2022的纯xaml [wpf4net5] - Caliburn-Micro-EventAggregation[6/16]-WPF-aaronyang技术分享 (ayjs.net)
第6天CM.zip
Ay写给2022的纯xaml [wpf4net5] - Caliburn Micro-Conductor[7/16]-WPF-aaronyang技术分享 (ayjs.net)
第7天CM.zip
Ay写给2022的纯xaml [wpf4net5] - Caliburn Micro-Bubbling[8/16]-WPF-aaronyang技术分享 (ayjs.net)
第8天CM.zip
通过发布订阅的方式 在VM之间 传递界面与界面的操作
在多个需要传递操作的vm中也需要加
添加 ConductorViewModel
using Caliburn.Micro; namespace cm1.ViewModels { public class ConductorViewModel: Screen { public ConductorViewModel() { } } }
打开Bootstrapper.cs 修改
protected override void Configure() { container = new SimpleContainer(); container.Instance(container); container .Singleton<IWindowManager, WindowManager>() .Singleton<IEventAggregator, EventAggregator>(); container .PerRequest<ShellViewModel>() .PerRequest<MenuViewModel>() .PerRequest<BindingsViewModel>() .PerRequest<ActionsViewModel>() .PerRequest<CoroutineViewModel>() .PerRequest<ExecuteViewModel>() .PerRequest<EventAggregationViewModel>() .PerRequest<ConductorViewModel>(); }
打开MenuViewModel.cs添加菜单
Features = new BindableCollection<FeatureViewModel> { new FeatureViewModel("Binding Conventions", "Binding view model properties to your view.", typeof(BindingsViewModel)), new FeatureViewModel("Action Conventions", "Wiring view events to view model methods.", typeof(ActionsViewModel)), new FeatureViewModel("Coroutines", "Using IEnumerable<IResult>", typeof(CoroutineViewModel)), new FeatureViewModel("Execute", "Using Execute to execute code on the UI thread.", typeof(ExecuteViewModel)), new FeatureViewModel("Event Aggregator", "在非耦合视图模型之间发送事件.", typeof(EventAggregationViewModel)), };
添加 ConductorView
<Page x:Class="cm1.Views.ConductorView" 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" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" Title="ConductorView"> <Grid Background="Red"> </Grid> </Page>
Conductors 管理一个或多个Screen的生命周期状态。他们负责基于不同的场景管理Screen的激活,禁用和关闭。
关闭操作的一部分,包括查询Screen,看它是否可以关闭。如果Screen存在未保存的数据,它会停止关闭操作,使未保存的工作不会丢失。
Conductor<T> ——管理一个单一Screen。一旦激活一个新的屏幕,任何先前的Screen会被关闭,会通过 conductor 进行管理。用于非常简单的导航/显示(navigation/display)方案。
Conductor<T>.Collection.OneActive ——管理多个Screen,并允许在同一时间里存在一个激活的Screen,很像一个选项卡控件。当Screen被激活,以前的活动Screen被停用,他不会关闭,也不会通过conductor管理遗留。Screens可以显式地关闭和移除。这种类型的conductor还负责激活Screens中的某一个,如果当前活动Screen关闭。还有简单的逻辑,你可以重写,如果你需要。
Conductor<T>.Collection.AllActive ——和上一个Conductor非常类似,但允许多个屏幕处于活动状态。
F12 查看 Conductor
Conductor的父类ConductorBaseWithActiveItem
这里有个很重要的属性 ActiveItem
调用Conductor的ActivateItem时候会改变ActiveItem
前台需要使用ContentControl来显示激活的Screen的子类,很明显是个 XXXViewModel类
<ContentControl cal:View.Model="{Binding ActiveItem}" Grid.Row="2" Margin="40,20"/>
我们添加一个TabViewModel,成为Conductor<TabViewModel> 也就是激活的类型
using System; using System.Threading; using System.Threading.Tasks; using Caliburn.Micro; namespace cm1.ViewModels { public class TabViewModel : Screen { private readonly Random random = new Random(); public TabViewModel() { Messages = new BindableCollection<string>(); } protected override Task OnInitializeAsync(CancellationToken cancellationToken) { Messages.Add("Initialized"); return Task.CompletedTask; } protected override Task OnActivateAsync(CancellationToken cancellationToken) { Messages.Add("Activated"); return Task.CompletedTask; } protected override Task OnDeactivateAsync(bool close, CancellationToken cancellationToken) { Messages.Add($"Deactivated, close: {close}"); return Task.CompletedTask; } public override async Task<bool> CanCloseAsync(CancellationToken cancellationToken) { var canClose = true; Messages.Add($"canClose"); await Task.Delay(500); return canClose; } public BindableCollection<string> Messages { get; } } }
这个类,在Screen的生命周期触发时候,往Messages集合 加字符串
添加对应的界面TabView.xaml
显示那些字符串
<UserControl x:Class="cm1.Views.TabView" 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" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <ItemsControl x:Name="Messages"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding}"/> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </Grid> </UserControl>
每次只激活一个screen,所以继承
using System; using System.Threading; using System.Threading.Tasks; using Caliburn.Micro; namespace cm1.ViewModels { public class ConductorViewModel : Conductor<TabViewModel>.Collection.OneActive { private int count; public ConductorViewModel() { Items.CollectionChanged += (s, e) => NotifyOfPropertyChange(() => CanCloseTab); } protected override async Task OnInitializeAsync(CancellationToken cancellationToken) { await AddTabAsync(); await AddTabAsync(); } public Task AddTabAsync() { return ActivateItemAsync(new TabViewModel { DisplayName = $"Tab {count++}" }, CancellationToken.None); } public bool CanCloseTab => Items.Count > 1; public Task CloseTab() { return DeactivateItemAsync(ActiveItem, close: true, cancellationToken: CancellationToken.None); } } }
集合改变的时候,也就是数量改变了,改变了 回调一下 CanCloseTab,只有当选项卡个数大于1,时候才能关闭选项卡
OnInitializeAsync初始化时候,增加2个选项卡
添加界面代码
<Page x:Class="Features.CrossPlatform.Views.ConductorView" 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:Features.CrossPlatform.Views" xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro.Platform" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" Title="ConductorView"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <StackPanel Orientation="Horizontal" Margin="40,20"> <Button x:Name="AddTab" Content="增加一个选项卡" Margin="0,0,10,0" /> <Button x:Name="CloseTab" Content="关闭当前选项卡" /> </StackPanel> <ListBox x:Name="Items" Grid.Row="1" DisplayMemberPath="DisplayName" Margin="40,0"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </ListBox.ItemsPanel> </ListBox> <ContentControl cal:View.Model="{Binding ActiveItem}" Grid.Row="2" Margin="40,20"/> </Grid> </Page>
运行效果,单击添加,会增加一个,单击关闭会关闭一个
单击listbox的item时候,会显示当前激活的item的 messages
这个ListBox的名字叫 Items
第一反应找 vm里面的Items集合
默认Listbox的selectionchanged会自动调用ActivateItem事件
具体看 CM的源码
关于 判断设计时
public string Text => Execute.InDesignMode ? "Design Time" : "Run Time";
本文由 安徽 合肥 杨洋 1991年的 英文名AY ===来自网站 www.ayjs.net 编写
本文由 安徽 合肥 杨洋 1991年的 英文名AY ===来自网站 www.ayjs.net 编写
本文代码下载:
net framework 类库 | net5 中的替代类库 |
Microsoft.Expression.Interactions System.Windows.Interactivity | Microsoft.Xaml.Behaviors.Wpf |
其他Ay准备升级到net5的纯xaml库
【ay wpf markup】AY XAML应该这样玩【1/18】-WPF-aaronyang技术分享 (ayjs.net)
【ay wpf markup】AY XAML应该这样玩【2/18】-WPF-aaronyang技术分享 (ayjs.net)
【ay wpf markup】AY XAML应该这样玩-写个计算器【3/18】-WPF-aaronyang技术分享 (ayjs.net)
【ay wpf markup】AY XAML应该这样玩-for循环【4/18】-WPF-aaronyang技术分享 (ayjs.net)
【ay wpf markup】AY XAML应该这样玩-ChangedHandler【5/18】-WPF-aaronyang技术分享 (ayjs.net)
【ay wpf markup】AY XAML应该这样玩-ResourceCollection和ScriptHandler【6/18】-WPF-aaronyang技术分享 (ayjs.net)
【ay wpf markup】AY XAML应该这样玩-ResourceObject【7/18】-WPF-aaronyang技术分享 (ayjs.net)
【ay wpf markup】AY XAML应该这样玩-ScriptHandler组合,也是最常用的【8/18】-WPF-aaronyang技术分享 (ayjs.net)
【ay wpf markup】AY XAML应该这样玩-语法集合【9/18】-WPF-aaronyang技术分享 (ayjs.net)
【ay wpf markup】AY XAML应该这样玩-Tower of Hanoi【10/18】-WPF-aaronyang技术分享 (ayjs.net)
【ay wpf markup】AY XAML应该这样玩-写个简单的移动【11/18】-WPF-aaronyang技术分享 (ayjs.net)
【ay wpf markup】AY XAML应该这样玩-写个复杂的绘图【12/18】-WPF-aaronyang技术分享 (ayjs.net)
【ay wpf markup】AY XAML应该这样玩-写个复杂的颜色环【13/18】-WPF-aaronyang技术分享 (ayjs.net)
【ay wpf markup】AY XAML应该这样玩-获得焦点全选【14/18】-WPF-aaronyang技术分享 (ayjs.net)
【ay wpf markup】AY XAML应该这样玩-把Additional.Operations当资源【15/18】-WPF-aaronyang技术分享 (ayjs.net)
【ay wpf markup】AY XAML应该这样玩-给ListView创建数据【16/18】-WPF-aaronyang技术分享 (ayjs.net)
【ay wpf markup】AY XAML应该这样玩-和后台ViewModel交互,操作方法【17/18】-WPF-aaronyang技术分享 (ayjs.net)
Window
<Window x:Class="Features.CrossPlatform.Views.ShellView" 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:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro.Platform" mc:Ignorable="d" Title="ShellView" Height="450" Width="800"> <Grid> </Grid> </Window>
Page
<Page x:Class="Features.CrossPlatform.Views.MenuView" 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" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" Title=""> <Grid> </Grid> </Page>
UserControl
<UserControl x:Class="cm1.Views.FeatureView" 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:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro.Platform" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> </Grid> </UserControl>
抖音:wpfui 工作wpf
目前在合肥企迈科技公司上班,加我QQ私聊
2023年11月网站停运,将搬到CSDN上
AYUI8全源码 Github地址:前往获取
杨洋(AaronYang简称AY,安徽六安人)和AY交流
高中学历,2010年开始web开发,2015年1月17日开始学习WPF
声明:AYUI7个人与商用免费,源码可购买。部分DEMO不免费
查看捐赠AYUI7.X MVC教程 更新如下:
第一课 第二课 程序加密教程
额 本文暂时没人评论 来添加一个吧
发表评论