当前位置:网站首页 / WPF / 正文

Ay写给2022的纯xaml [wpf4net5] - Caliburn Micro-导航服务传参[9/16]

时间:2021年10月08日 | 作者 : aaronyang | 分类 : WPF | 浏览: 3365次 | 评论 1

相关文章

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

Ay写给2022的纯xaml [wpf4net5] - Caliburn Micro-导航服务传参[9/16]-WPF-aaronyang技术分享 (ayjs.net)   第9天CM.zip


下面的例子:从一个页面打开另一个页面, 并给第二个页面传递参数值

准备工作

添加 NavigationSourceViewModel

using Caliburn.Micro;

namespace cm1.ViewModels
{
    public class NavigationSourceViewModel : Screen
    {
        private readonly INavigationService navigationService;
        private string text;
        private bool isEnabled;

        public NavigationSourceViewModel(INavigationService navigationService)
        {
            this.navigationService = navigationService;
        }

        public string Text
        {
            get { return text; }
            set { Set(ref text, value); }
        }

        public bool IsEnabled
        {
            get { return isEnabled; }
            set { Set(ref isEnabled, value); }
        }

        public void Navigate()
        {
            navigationService.For<NavigationTargetViewModel>()
                .WithParam(v => v.Text, Text)
                .WithParam(v => v.IsEnabled, IsEnabled)
                .Navigate();
        }
    }



}

添加 NavigationTargetViewModel    

using Caliburn.Micro;

namespace cm1.ViewModels
{
    public class NavigationTargetViewModel : Screen
    {
        private string text;
        private bool isEnabled;

        public string Text
        {
            get { return text; }
            set { Set(ref text, value); }
        }

        public bool IsEnabled
        {
            get { return isEnabled; }
            set { Set(ref isEnabled, value); }
        }
    }



}



打开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>()
                .PerRequest<BubblingViewModel>()
                .PerRequest<NavigationSourceViewModel>()
                .PerRequest<NavigationTargetViewModel>();

        }

打开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)),
                new FeatureViewModel("Conductors", "将视图模型与生命周期事件组合在一起", typeof(ConductorViewModel)),
                 new FeatureViewModel("Bubbling Actions", "如何将动作从子视图模型冒泡到父视图", typeof(BubblingViewModel)),
                 new FeatureViewModel("Navigation", "Using a navigation service to switch between view models.", typeof(NavigationSourceViewModel)),
            };

添加 NavigationSourceView

<Page x:Class="cm1.Views.NavigationSourceView"
      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="NavigationSource">

    <Grid>
        <StackPanel Margin="20">
            <TextBox x:Name="Text" />
            <CheckBox x:Name="IsEnabled" Content="Is Enabled" Margin="0,10" />
            <Button x:Name="Navigate" Content="Navigate" />
        </StackPanel>
    </Grid>
</Page>

这个页面传递值给NavigationTargetView.xaml

<Page x:Class="cm1.Views.NavigationTargetView"
      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="NavigationTargetView">

    <Grid>
        <StackPanel Margin="20">
            <TextBlock x:Name="Text" />
            <TextBlock x:Name="IsEnabled" />
        </StackPanel>
    </Grid>
</Page>

运行

image.png

输入123   勾中 ,点击 Navigate

image.png

我们通过

        navigationService.For<NavigationTargetViewModel>()

                .WithParam(v => v.Text, Text)

                .WithParam(v => v.IsEnabled, IsEnabled)

                .Navigate();

给目标的VM传递参数值

image.png




本文由 安徽 合肥 杨洋 1991年的 英文名AY   ===来自网站 www.ayjs.net 编写

本文由 安徽 合肥 杨洋 1991年的 英文名AY   ===来自网站 www.ayjs.net 编写




本文代码下载:

第9天CM.zip

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>












推荐您阅读更多有关于“wpfcmWPF4.5,”的文章

猜你喜欢

已有1位网友发表了看法:

1#访客  2022-08-09 17:35:12 回复该评论

你好,我仿照你的Demo写的,我是我导航之后可以显示MenuView,但是里面没有任何东西,好像MenuViewModel没有运行,请问这是什么原因啊?

发表评论

必填

选填

选填

必填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

  查看权限

抖音:wpfui 工作wpf,兴趣学习flutter

目前在合肥市某公司上班,已经厌弃,如果你的公司看的上我,加我QQ私聊

AYUI8全源码 Github地址:前往获取

杨洋(AaronYang简称AY,安徽六安人)AY唯一QQ:875556003和AY交流

高中学历,2010年开始web开发,2015年1月17日开始学习WPF

声明:AYUI7个人与商用免费,源码可购买。部分DEMO不免费

不是从我处购买的ayui7源码,我不提供任何技术服务,如果你举报从哪里买的,我可以帮你转正为我的客户,并送demo

查看捐赠

AYUI7.X MVC教程 更新如下:

第一课 第二课 程序加密教程

标签列表