wpf prism 《3》 弹窗 IOC

打印 上一主题 下一主题

主题 926|帖子 926|积分 2778

传统的弹窗 这种耦合度高

new 窗体() . Show();
new 窗体() . ShowDialog();
使用Prism 主动的 IOC 弹窗的 必须 必须 必须 页面控件


弹窗的 必须 必须 必须 页面控件
弹窗的 必须 必须 必须 页面控件
弹窗的 必须 必须 必须 页面控件
弹窗的 必须 必须 必须 页面控件
》》否则 报上面的错误


》》主步伐
  1. <Window x:Class="BlankApp2.Views.MainView"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:prism="http://prismlibrary.com/"
  5.         prism:ViewModelLocator.AutoWireViewModel="True"
  6.         Title="{Binding Title}" Height="350" Width="525" >
  7.     <Grid>
  8.         <Grid.RowDefinitions>
  9.             <RowDefinition Height="50"></RowDefinition>
  10.             <RowDefinition></RowDefinition>
  11.         </Grid.RowDefinitions>
  12.         <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="10">
  13.             <Button Content="弹窗1" Command="{Binding OpenCommand}" CommandParameter="Popup"></Button>
  14.             <Button Content="弹窗2" Command="{Binding OpenCommand}" CommandParameter="UCPopup"></Button>
  15.             <!--<Button Content="模块Student" Command="{Binding OpenCommand}" CommandParameter="ViewXX"></Button>
  16.             <Button Content="模块C" Command="{Binding OpenCommand}" CommandParameter="ViewC"></Button>
  17.             <Button Content="回退" Command="{Binding BackCommand}"></Button>-->
  18.         </StackPanel>
  19.         <ContentControl Grid.Row="1" prism:RegionManager.RegionName="ContentRegion" />
  20.     </Grid>
  21. </Window>
复制代码
》》主步伐对应的 ViewModel
  1. using Prism.Commands;
  2. using Prism.Dialogs;
  3. using Prism.Mvvm;
  4. using Prism.Navigation;
  5. using Prism.Navigation.Regions;
  6. using System;
  7. namespace BlankApp2.ViewModels
  8. {
  9.     public class MainViewModel : BindableBase
  10.     {
  11.         private string _title = "Prism Application";
  12.         public string Title
  13.         {
  14.             get { return _title; }
  15.             set { SetProperty(ref _title, value); }
  16.         }
  17.         public DelegateCommand<string> OpenCommand { get; private set; }
  18.         public IDialogService DialogService { get; }
  19.         public MainViewModel(IDialogService dialogService)
  20.         {
  21.             this.DialogService = dialogService;
  22.             this.OpenCommand = new DelegateCommand<string>(Open);
  23.         }
  24.         private void Open(string obj)
  25.         {
  26.             //传递给弹窗的参数信息
  27.             DialogParameters keys = new DialogParameters();
  28.             keys.Add("zen", "============zen============");
  29.             DialogService.ShowDialog(obj, keys, callback =>
  30.             {
  31.                 if (callback.Result == ButtonResult.OK)
  32.                 {   
  33.                    //弹窗传递的参数信息
  34.                     string ss = callback.Parameters.GetValue<string>("Info");
  35.                 }
  36.             });
  37.         }
  38.     }
  39. }
复制代码
》》》弹窗用户控件 、弹窗的ViewModel

  1. <UserControl x:Class="BlankApp2.Views.UCPopup"
  2.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6.              xmlns:local="clr-namespace:BlankApp2.Views"
  7.              mc:Ignorable="d"
  8.              d:DesignHeight="450" d:DesignWidth="800">
  9.     <Grid>
  10.         <Grid.RowDefinitions>
  11.             <RowDefinition Height="20"></RowDefinition>
  12.             <RowDefinition></RowDefinition>
  13.             <RowDefinition Height="80"></RowDefinition>
  14.         </Grid.RowDefinitions>
  15.         <TextBlock Text="{Binding Title}"></TextBlock>
  16.         <TextBlock Text="弹窗信息" FontSize="40" Grid.Row="1"></TextBlock>
  17.         <StackPanel Orientation="Horizontal" Margin="10" HorizontalAlignment="Right" Grid.Row="2">
  18.             <Button Content="确     定" Margin="10" Command="{Binding OKCommand}"></Button>
  19.             <Button Content="取     消" Margin="10" Command="{Binding CancelCommand}"></Button>
  20.         </StackPanel>
  21.     </Grid>
  22. </UserControl>
复制代码
  1. using Prism.Commands;
  2. using Prism.Dialogs;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace BlankApp2.ViewModels
  9. {
  10.     public class UCPopupViewModel : IDialogAware
  11.     {
  12.         public string Title { get; set; }
  13.         public DialogCloseListener RequestClose { get; set; }
  14.         public DelegateCommand CancelCommand { get; set; }
  15.         public DelegateCommand OKCommand { get; set; }
  16.         public UCPopupViewModel()
  17.         {
  18.             CancelCommand = new DelegateCommand(Cancel);
  19.             OKCommand = new DelegateCommand(OKcmd);
  20.         }
  21.         private void OKcmd()
  22.         {
  23.             DialogParameters keys = new DialogParameters();
  24.             keys.Add("Info", "======INDO==========");
  25.             RequestClose.Invoke(keys, ButtonResult.OK);
  26.         }
  27.         private void Cancel()
  28.         {
  29.             RequestClose.Invoke(ButtonResult.Cancel);
  30.         }
  31.         //是否准许关闭弹窗
  32.         public bool CanCloseDialog()
  33.         {
  34.             return true;
  35.         }
  36.         //弹窗关闭时【窗体哪个 X】
  37.         public void OnDialogClosed()
  38.         {
  39.             DialogParameters keys = new DialogParameters();
  40.             keys.Add("Info", "======INDO==========");
  41.             RequestClose.Invoke(keys, ButtonResult.OK);
  42.             //throw new NotImplementedException();
  43.         }
  44.         //弹窗弹出时触发
  45.         public void OnDialogOpened(IDialogParameters parameters)
  46.         {
  47.             if (parameters.ContainsKey("zen"))
  48.             {
  49.                 this.Title = parameters.GetValue<string>("zen");
  50.             }
  51.             //throw new NotImplementedException();
  52.         }
  53.     }
  54. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

商道如狼道

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表