0x07交互

Notification

xaml:

<Window x:Class="UsingPopupWindowAction.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="{Binding Title}" Height="350" Width="525">
    <i:Interaction.Triggers>
        <prism:InteractionRequestTrigger SourceObject="{Binding NotificationRequest}">
            <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True" />
        </prism:InteractionRequestTrigger>
    </i:Interaction.Triggers>
    <StackPanel>
        <Button Margin="5" Content="Raise Default Notification" Command="{Binding NotificationCommand}" />

        <TextBlock Text="{Binding Title}" Margin="25" HorizontalAlignment="Center" FontSize="24" />
    </StackPanel>
</Window>

code-behind:

using Prism.Commands;
using Prism.Interactivity.InteractionRequest;
using Prism.Mvvm;

namespace UsingPopupWindowAction.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private string _title = "Prism Unity Application";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        public InteractionRequest<INotification> NotificationRequest { get; set; }
        public DelegateCommand NotificationCommand { get; set; }

        public MainWindowViewModel()
        {
            NotificationRequest = new InteractionRequest<INotification>();
            NotificationCommand = new DelegateCommand(RaiseNotification);
        }

        void RaiseNotification()
        {
            NotificationRequest.Raise(new Notification { Content = "Notification Message", Title = "Notification" }, r => Title = "Notified");
        }
    }
}

Prism通过InteractionRequest 来实现Pop Up window。

xaml中需要注册一个Trigger:

    <i:Interaction.Triggers>
        <prism:InteractionRequestTrigger SourceObject="{Binding NotificationRequest}">
            <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True" />
        </prism:InteractionRequestTrigger>
    </i:Interaction.Triggers>

code-behind中声明

        public InteractionRequest<INotification> NotificationRequest { get; set; }

在command的回调函数中就可以使用NotificationRequest:

            NotificationRequest.Raise(new Notification { Content = "Notification Message", Title = "Notification" }, r => Title = "Notified");

Confirmation

跟Notification的使用方法一样,先注册:

        <prism:InteractionRequestTrigger SourceObject="{Binding ConfirmationRequest}">
            <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True" />
        </prism:InteractionRequestTrigger>

然后在使用InteractionRequest的时候:

        public InteractionRequest<IConfirmation> ConfirmationRequest { get; set; }

callback:

            ConfirmationRequest.Raise(new Confirmation {
                Title = "Confirmation",
                Content = "Confirmation Message" },
                r => Title = r.Confirmed ? "Confirmed" : "Not Confirmed");

CustomPopupRequest

简单客制化弹窗(这里我们并没有用到viewmodel)

首先绘制一个弹窗:

<UserControl x:Class="UsingPopupWindowAction.Views.CustomPopupView"
             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:UsingPopupWindowAction.Views"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <StackPanel>
            <TextBlock Text="{Binding Title}" FontSize="24" HorizontalAlignment="Center" />
            <TextBlock Text="{Binding Content}" Margin="10"/>
            <Button Margin="25" Click="Button_Click">Accept</Button>
        </StackPanel>
    </Grid>
</UserControl>

code-behind:

using Prism.Interactivity.InteractionRequest;
using System;
using System.Windows;
using System.Windows.Controls;

namespace UsingPopupWindowAction.Views
{
    /// <summary>
    /// Interaction logic for CustomPopupView.xaml
    /// </summary>
    public partial class CustomPopupView : UserControl, IInteractionRequestAware
    {
        public CustomPopupView()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            FinishInteraction?.Invoke();
        }

        public Action FinishInteraction { get; set; }
        public INotification Notification { get; set; }
    }
}

然后在调用者的xaml中注册:

        <prism:InteractionRequestTrigger SourceObject="{Binding CustomPopupRequest}">
            <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True">
                <prism:PopupWindowAction.WindowContent>
                    <views:CustomPopupView />
                </prism:PopupWindowAction.WindowContent>
            </prism:PopupWindowAction>
        </prism:InteractionRequestTrigger>

code-behind:

        public InteractionRequest<INotification> CustomPopupRequest { get; set; }
        public DelegateCommand CustomPopupCommand { get; set; }

call-back:

            CustomPopupRequest.Raise(new Notification { Title = "Custom Popup", Content = "Custom Popup Message " }, r => Title = "Good to go");

复杂的客制化弹框:

上面的弹窗中,只有一个 accept按钮,在prism内置的confirmation都可以实现是和否,那客制化的弹窗中如何实现更为复杂的情况呢?

示例中实现了一个弹窗,然后选择一个类型为String的Item确认后并将他返回给调用者,如没有确认,则提示没有选择。

定义一个接口(为了IoC):

using Prism.Interactivity.InteractionRequest;

namespace UsingPopupWindowAction.Notifications
{
    public interface ICustomNotification : IConfirmation
    {
        string SelectedItem { get; set; }
    }
}

一个类(真正干活的类):

using Prism.Interactivity.InteractionRequest;
using System.Collections.Generic;

namespace UsingPopupWindowAction.Notifications
{
    public class CustomNotification : Confirmation, ICustomNotification
    {
        public IList<string> Items { get; private set; }

        public string SelectedItem { get; set; }

        public CustomNotification()
        {
            this.Items = new List<string>();
            this.SelectedItem = null;

            CreateItems();
        }

        private void CreateItems()
        {
            Items.Add("Item1");
            Items.Add("Item2");
            Items.Add("Item3");
            Items.Add("Item4");
            Items.Add("Item5");
            Items.Add("Item6");
        }
    }
}

调用者xaml中注册:

        <prism:InteractionRequestTrigger SourceObject="{Binding CustomNotificationRequest}">
            <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True">
                <prism:PopupWindowAction.WindowContent>
                    <views:ItemSelectionView />
                </prism:PopupWindowAction.WindowContent>
            </prism:PopupWindowAction>
        </prism:InteractionRequestTrigger>

调用者Code-behind:

        public InteractionRequest<ICustomNotification> CustomNotificationRequest { get; set; }
        public DelegateCommand CustomNotificationCommand { get; set; }
            CustomNotificationRequest = new InteractionRequest<ICustomNotification>();
            CustomNotificationCommand = new DelegateCommand(RaiseCustomInteraction);
        private void RaiseCustomInteraction()
        {
            CustomNotificationRequest.Raise(new CustomNotification { Title = "Custom Notification" }, r =>
                {
                    if (r.Confirmed && r.SelectedItem != null)
                        Title = $"User selected: { r.SelectedItem}";
                    else
                        Title = "User cancelled or didn't select an item";
                });
        }

接下来是我们的弹窗:

<UserControl x:Class="UsingPopupWindowAction.Views.ItemSelectionView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:prism="http://prismlibrary.com/"
             prism:ViewModelLocator.AutoWireViewModel="True"
             Width="400">
    <StackPanel>

        <TextBlock FontSize="24" Foreground="DarkBlue" Margin="10">Item Selection</TextBlock>

        <TextBlock Margin="10" TextWrapping="Wrap">
                This view has its own view model that implements the <Bold>IInteractionRequestAware</Bold> interface.
                Thanks to this, the view model is automatically populated with the corresponding "notification"
                and an action to finish the interaction, which in this case closes the window.
        </TextBlock>

        <TextBlock Margin="10" TextWrapping="Wrap" FontWeight="Bold">Please select an item:</TextBlock>
        <ListBox SelectionMode="Single" Margin="10,0" Height="100" ItemsSource="{Binding Notification.Items}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}"></ListBox>

        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>

            <Button AutomationProperties.AutomationId="ItemsSelectButton" Grid.Column="0" Margin="10" Command="{Binding SelectItemCommand}">Select Item</Button>
            <Button AutomationProperties.AutomationId="ItemsCancelButton" Grid.Column="1" Margin="10" Command="{Binding CancelCommand}">Cancel</Button>
        </Grid>
    </StackPanel>
</UserControl>

code-behind:

using Prism.Commands;
using Prism.Interactivity.InteractionRequest;
using Prism.Mvvm;
using System;
using UsingPopupWindowAction.Notifications;

namespace UsingPopupWindowAction.ViewModels
{
    public class ItemSelectionViewModel : BindableBase, IInteractionRequestAware
    {
        public string SelectedItem { get; set; }

        public DelegateCommand SelectItemCommand { get; private set; }

        public DelegateCommand CancelCommand { get; private set; }

        public ItemSelectionViewModel()
        {
            SelectItemCommand = new DelegateCommand(AcceptSelectedItem);
            CancelCommand = new DelegateCommand(CancelInteraction);
        }

        private void CancelInteraction()
        {
            _notification.SelectedItem = null;
            _notification.Confirmed = false;
            FinishInteraction?.Invoke();
        }

        private void AcceptSelectedItem()
        {
            _notification.SelectedItem = SelectedItem;
            _notification.Confirmed = true;
            FinishInteraction?.Invoke();
        }

        public Action FinishInteraction { get; set; }

        private ICustomNotification _notification;

        public INotification Notification
        {
            get { return _notification; }
            set { SetProperty(ref _notification, (ICustomNotification)value); }
        }

    }
}

最新文章

  1. [codevs3729]飞扬的小鸟
  2. struts2文件上传和下载
  3. Leetcode 160 Intersection of Two Linked Lists 单向链表
  4. 字母排序问题(c++实现)
  5. Centos7 安装 Nginx
  6. windows下做react native官方例子遇到的问题
  7. DBA应该知道的一些SQL Server跟踪标记
  8. javaweb 乱码总结
  9. mvc 解决StyleBundle中 图片绝对路径 装换成相对路径的问题 CssRewriteUrlTransform
  10. db2服务器端授权
  11. java 深入理解内部类以及之间的调用关系
  12. COSO企业风险管理框架2017版发布!看看有哪些变化?
  13. 为Jupyter只安装目录的扩展包
  14. Eclipse控制台输出log日志中文乱码
  15. LeetCode算法题-Sum of Two Integers(Java实现)
  16. 文件管理 - Ring3创建目录
  17. Python开发【模块】:aiohttp(二)
  18. 使用vue脚手架(vue-cli)快速搭建项目
  19. Codeforces 429B Working out(递推DP)
  20. Android C/C++ 开发

热门文章

  1. Spring AOP梳理
  2. jstl的表达式不能解析
  3. SQL添加表字段
  4. oc 与 js交互之vue.js
  5. Java中动态代理工作流程
  6. 特殊权限chattr的用法
  7. MongoDB3.6之Replica Set初步体验
  8. 快速搭建CentOS+ASP.NET Core环境支持WebSocket
  9. poj-1146 ID codes
  10. MySQL 中添加列、修改列以及删除列