05.net6的wpf使用Prism的对话窗口功能步骤

  1. 创建对话窗口
<UserControl x:Class="WpfApp1.Views.InputDialog"
        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:local="clr-namespace:WpfApp1.Views"
        mc:Ignorable="d"
        d:Height="387" d:Width="483">
    <Grid>
        <Button Command="{Binding CancelCommand}" Content="取消" HorizontalAlignment="Left" Margin="200,170,0,0" VerticalAlignment="Top"/>
        <Button Command="{Binding SaveCommand}" Content="保存" HorizontalAlignment="Left" Margin="275,169,0,0" VerticalAlignment="Top"/>
        <TextBox HorizontalAlignment="Left" Margin="189,118,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>

    </Grid>
</UserControl>

  1. 实现对话窗口的视图模型继承IDialogAware
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace WpfApp1.ViewModels
{
    

    public class InputDialogViewModel : BindableBase,IDialogAware
    {

        #region 私有字段区


        #endregion

        #region 属性区
        private string _title = "WelcomeViewModel";

        public event Action<IDialogResult> RequestClose;

        public string Title
        {
            get { return _title; }
            set
            {
                SetProperty(ref _title, value);
            }
        }

        #endregion

        #region 命令区
        public ICommand CancelCommand
        {
            get => new DelegateCommand(() =>
            {
                DialogParameters keyValuePairs = new DialogParameters();
                keyValuePairs.Add("Title", "回传cancel");
                RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel, keyValuePairs));
            });
        }

        public ICommand SaveCommand
        {
            get => new DelegateCommand(() =>
            {
                DialogParameters keyValuePairs = new DialogParameters();
                keyValuePairs.Add("Title", "回传ok");
                RequestClose?.Invoke(new DialogResult(ButtonResult.OK, keyValuePairs));
            });
        }
        #endregion

        public InputDialogViewModel(IRegionManager regionManager)
        {
            
           
        }
       
        // 在尝试关闭对话框时进行验证或确认,返回false则阻止关闭
        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            // 在对话框关闭时执行的逻辑
            // 处理对话框返回的结果
            //DialogParameters keyValuePairs = new DialogParameters();
            //keyValuePairs.Add("Title", "回传");
            //RequestClose?.Invoke(new DialogResult(ButtonResult.OK, keyValuePairs));
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            // 在对话框打开时执行的逻辑
            if (parameters.ContainsKey("Title"))
            {
                var title = parameters.GetValue<string>("Title");
            }
            // 执行相关操作
        }
    }
}

  1. 注册Prism对话窗口到对话窗口服务中
protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            //注册窗口服务中的窗口
            containerRegistry.RegisterDialog<InputDialog>();

            //注册区域视图
            containerRegistry.RegisterForNavigation<SettingView>();
            containerRegistry.RegisterForNavigation<WelcomeView>();
        }

4 使用对话窗口服务

 public ICommand OpenDialogCommand
        {
            get => new DelegateCommand(() =>
            {
                DialogParameters keyValuePairs = new DialogParameters();
                keyValuePairs.Add("Title", "传过去");
                _dialogService.ShowDialog("InputDialog", keyValuePairs,callback =>
                {
                    if (callback.Parameters.ContainsKey("Title"))
                    {
                        string title = callback.Parameters.GetValue<string>("Title");
                    }
                });
            });
        }
  1. IDialogAware介绍
    IDialogAware是Prism框架中的一个接口,用于定义视图模型(ViewModel)是否支持对话框弹出,并提供一些与对话框相关的方法。通过实现IDialogAware接口,可以让视图模型能够在对话框弹出时执行特定的操作。

IDialogAware接口定义了以下方法:

OnDialogOpened:当对话框打开时调用。可以在该方法中执行特定的逻辑和初始化操作。
OnDialogClosed:当对话框关闭时调用。可以在该方法中处理对话框返回的结果。
RequestClose:通知对话框关闭。视图模型可以通过调用RequestClose方法来关闭对话框,并传递一个表示对话框结果的参数。
下面是一个使用IDialogAware的示例:

public class MyViewModel : BindableBase, IDialogAware
{
    public event Action<IDialogResult> RequestClose;

    public string Title => "My Dialog";

    public void OnDialogOpened(IDialogParameters parameters)
    {
        // 在对话框打开时执行的逻辑
        var parameter = parameters.GetValue<string>("ParameterName");
        // 执行相关操作
    }

    public void OnDialogClosed()
    {
        // 在对话框关闭时执行的逻辑
        // 处理对话框返回的结果
    }

    public void CloseDialog(bool? dialogResult)
    {
        // 通知对话框关闭,并传递对话框结果
        RequestClose?.Invoke(new DialogResult(dialogResult));
    }

    // 可选方法
    public bool CanCloseDialog()
    {
        // 在尝试关闭对话框时进行验证或确认,返回false则阻止关闭
        return true;
    }
}

要使用IDialogAware,需要在视图模型中实现该接口,并根据需要实现相关方法。然后,在使用Prism框架弹出对话框时,Prism会按照一定的流程调用这些接口方法,以便执行特定的操作或处理对话框的返回结果。在视图模型中可以调用RequestClose方法来通知对话框关闭,并传递对话框结果。

请注意,使用IDialogAware需要与Prism框架中的对话框服务(如IInteractionRequestAware)一起使用,以实现对话框的弹出和关闭。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 `PdfiumViewer` 这个 NuGet 包将 PDF 文件转换为图片,它可以在 WPF 应用程序中使用。下面是一个简单的示例: 1. 首先,在 Visual Studio 中创建一个 WPF 应用程序。 2. 在项目中添加 `PdfiumViewer` NuGet 包。 3. 创建一个 `PdfToImageConverter` 类,该类包含一个 `ConvertPdfToImage` 方法,该方法将 PDF 文件转换为图片。代码如下: ```csharp using (var document = PdfiumViewer.PdfDocument.Load(pdfFilePath)) { var image = document.Render(0, 300, 300, true); image.Save(imageFilePath, System.Drawing.Imaging.ImageFormat.Jpeg); } ``` 在这个代码中,我们使用 `PdfiumViewer` 中的 `PdfDocument` 类加载 PDF 文件,然后将它渲染成一个 `System.Drawing.Image` 对象,最后保存为 JPEG 格式的图片文件。 4. 在 WPF 应用程序中,创建一个 `Image` 控件用于显示转换后的图片。 5. 在 WPF 应用程序中,使用 `OpenFileDialog` 对话框选择要转换的 PDF 文件,并将其路径传递给 `ConvertPdfToImage` 方法。 6. 将转换后的图片显示在 `Image` 控件中。 完整的代码示例如下: ```csharp public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void ConvertButton_Click(object sender, RoutedEventArgs e) { var openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "PDF Files (*.pdf)|*.pdf"; if (openFileDialog.ShowDialog() == true) { var pdfFilePath = openFileDialog.FileName; var imageFilePath = Path.Combine(Path.GetDirectoryName(pdfFilePath), Path.GetFileNameWithoutExtension(pdfFilePath) + ".jpg"); var converter = new PdfToImageConverter(); converter.ConvertPdfToImage(pdfFilePath, imageFilePath); var image = new BitmapImage(new Uri(imageFilePath)); ImageControl.Source = image; } } } public class PdfToImageConverter { public void ConvertPdfToImage(string pdfFilePath, string imageFilePath) { using (var document = PdfiumViewer.PdfDocument.Load(pdfFilePath)) { var image = document.Render(0, 300, 300, true); image.Save(imageFilePath, System.Drawing.Imaging.ImageFormat.Jpeg); } } } ``` 这个示例将 PDF 文件转换为 JPEG 格式的图片,但你也可以将其保存为其他格式。同时,你还需要添加一些错误处理和异常处理的代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值