【发布时间】:2015-10-23 20:51:44
【问题描述】:
我在 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 窗口中。
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,您应该使用自定义对话框。
【问题讨论】:
标签: c# wpf xaml mahapps.metro