WPF(C#)学习日志6:Prism框架3-弹窗

1.为什么要使用弹窗

弹窗存在于许多的应用程序中,有多种应用场景

提高用户体验: 弹窗可以为用户提供更直观、友好的界面,避免用户迷失在复杂的页面结构中。

即时反馈: 弹窗可以立即向用户提供反馈,使用户了解其操作的结果或状态。

上下文切换: 弹窗可以在不离开当前界面的情况下展示新的信息,避免了用户在不同页面之间的频繁切换。

有效利用空间: 弹窗可以临时占用屏幕空间,而不需要为新页面分配额外的空间。

弹窗的分类:信息提示弹窗、确认弹窗、错误处理弹窗、输入表单弹窗、对话框-会话(通常会阻止用户与应用程序的其他部分交互,直到对话框被关闭。),下图便是一些常见的图像UI。

2.Prism中使用弹窗服务

(1)首先需要实现母窗体,注册弹窗服务 IDialogService dialogService,构造函数可以有多个参数,导航和弹窗的服务可以同时注册,因此仅注册弹窗时:

 public MainWindowViewModel(IDialogService dialogService)
        {
            OpenCommand_1 = new DelegateCommand<string>(Open_1);
            this.dialogService = dialogService;
            
        }

弹窗和导航都注册时:

       public MainWindowViewModel(IRegionManager regionManager, IDialogService dialogService)
        {
            OpenCommand = new DelegateCommand<string>(Open);
            OpenCommand_1 = new DelegateCommand<string>(Open_1);
            this.regionManager = regionManager;
            this.dialogService = dialogService;
            
        }

实现(绑定)弹出行为,回调函数。弹窗可以在多种情况下被调用,但是这里我们使用按键的方式使用弹窗。

视图

  <Button Content="打开窗口W" Style="{StaticResource MyButton}" Command="{Binding OpenCommand_1}" CommandParameter="ViewW"/>

视图模型

        private void Open_1(string obj)
        {
            //向开启的界面传参
            DialogParameters keys = new DialogParameters();
            keys.Add("Title", "测试弹窗");
            dialogService.ShowDialog(obj, keys, callback =>
            {
                if (callback.Result == ButtonResult.OK)
                {
                    //string result =callback.Parameters.GetValue<string>("Value");
                    Result = "返回结果为:确定";
                }
                else
                {
                    Result = "返回结果为:取消";
                }
            });
        }

(2)创建弹窗控件,先实现视图模型,识图模型可以通过继承的方式实现。

internal class ViewWViewModel : IDialogAware

这里主要Prism中的大多数的组件都是以用户控件实现的,之后会提示要实现一些函数,我们可以直接修复。

这里直接放上一个简单的确定取消弹窗

 internal class ViewWViewModel : IDialogAware
    {
        //确定和取消的方法
        public DelegateCommand CancelCommand { get; set; }
        public DelegateCommand SaveCommand { get; set; }
        public ViewWViewModel()
        {
            CancelCommand = new DelegateCommand(Cancel);
            SaveCommand = new  DelegateCommand (Save);
        }
        private void Save()
        {
            OnDialogClosed();
        }
        private void Cancel()
        {
            RequestClose?.Invoke(new DialogResult(ButtonResult.No));
        }
        public string Title { get; set; }

        //请求关闭
        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }
        public void OnDialogClosed()
        {
            DialogParameters keys = new DialogParameters();
            //字典
            keys.Add("Value", "Hello");

            RequestClose?.Invoke(new DialogResult(ButtonResult.OK, keys));
        }
        //启动时进入
        public void OnDialogOpened(IDialogParameters parameters)
        {
            Title = parameters.GetValue<string>("Title");
        }
    }

XAML代码

<UserControl x:Class="FullApp3.Modules.ModuleName.Views.ViewW"
             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:FullApp3.Modules.ModuleName.Views"
             
             mc:Ignorable="d" 
             Height="200" Width="400" >

    <Grid Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="60"/>
            <RowDefinition Height="*" />
            <RowDefinition Height="60"/>
        </Grid.RowDefinitions>

        <TextBlock Text="这是窗口" FontSize="20 "/>
        <StackPanel Grid.Row="2" Margin="10" HorizontalAlignment="Center" Orientation="Horizontal">
            <Button Width="100" Height="40" Content=" 取消"  Command="{Binding CancelCommand}"/>
            <Grid Width="50"> </Grid>
            <Button Width="100" Height="40" Content=" 确定"  Command="{Binding SaveCommand}"/>
        </StackPanel>
    </Grid>

</UserControl>

最终实现效果:

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值