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

How do I add a button with action to ShowInputAsync from MahApps.Metro

我在 wpf 应用程序中使用 MahApps Metro。我正在使用他们的 ShowInputAsync() Dialog

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

所以,在我的 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 window 中。

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。

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

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

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

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

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