如何从MahApps.Metro将带有操作的按钮添加到ShowInputAsync

本文关键字:操作 按钮 添加 ShowInputAsync MahApps Metro | 更新日期: 2023-09-27 18:18:58

我在 wpf 应用程序中使用 MahApps Metro。 我正在使用他们的ShowInputAsync((对话框。

我想保存一个目录,我想使用对话框来设置该目录。

所以,在我的MainWindow.xaml.cs中,我有一些类似的东西;

if(string.IsNullOrEmpty(userInputDirectory)) 
{
    userInputDirectory = await this.ShowInputAsync("Your Directory", "Set Your Directory");
}

这很好用,我喜欢对话框的外观,但我想添加一个浏览按钮,这样他们就不必记住目录位置,并且可以使用System.Windows.Forms.FolderBrowserDialog();浏览到所需的目录

就像我说的,我喜欢它现在的样子,我不想删除其他两个按钮,或者替换一个按钮,我只想添加一个。 任何帮助,不胜感激。

编辑1

我创建了一个新的用户控件,通过右键单击项目 -> 添加... -> 用户控件 -> 用户控件 (WPF(,并将其从 UserControl 更改为 Dialogs:BaseMetroDialog 。 我还为控件添加了 xmlns。 我收到一个错误Exception: Cannot create an instance of "BaseMetroDialog". 代码如下。

<Dialogs:BaseMetroDialog x:Class="testApp.CustomDialog"
         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:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
         xmlns:Dialogs="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"          
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
    </Grid>
</Dialogs:BaseMetroDialog>

我的.cs文件

namespace testApp
{
/// <summary>
/// Interaction logic for CustomDialog.xaml
/// </summary>
    public partial class CustomDialog : CustomDialog
    {
        public CustomDialog()
        {
            InitializeComponent();
        }
    }
}

在我尝试运行程序之前,我在 .xaml 文件中收到的错误显示在 xaml 窗口中。

InnerException: Exception has been thrown by the target of an invocation.
    StackTrace
         at System.RuntimeTypeHandle.CreateInstance(RuntimeType type,     Boolean publicOnly, Boolean noCheck, Boolean& canBeCached,     RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
InnerException: Object reference not set to an instance of an object
    NullReferenceException: Object reference not set to an instance of an object.
        StackTrace
            at MahApps.Metro.Controls.Dialogs.BaseMetroDialog.HandleTheme()
            at MahApps.Metro.Controls.Dialogs.BaseMetroDialog.Initialize()
            at MahApps.Metro.Controls.Dialogs.BaseMetroDialog..ctor()

编辑2

如果其他人发现这一点,并且遇到同样的问题,MahApps.Metro团队(这很棒,而且非常有用(直到1.1.3 APHA才添加CustomDialog修复程序,而不是BaseMetroDialog,你应该使用CustomDialog。

如何从MahApps.Metro将带有操作的按钮添加到ShowInputAsync

据我所知,metro没有浏览器对话框。为了具有这种行为,您需要创建自己的自定义地铁对话框。

为此,您必须创建一个类型为 CustomDialog 的新用户控件(位于 MahApps.Metro.Controls.Dialogs 中(,并自行实现预期行为。

在自定义对话框中,添加一个浏览器按钮,该按钮调用System.Windows.Forms.FolderBrowserDialog((;

获得自定义控件后,可以使用以下代码显示它:

var browserDialog = new MyCustomDialog();
await this.ShowMetroDialogAsync(browserDialog);