C# 在Mahapps Metro的输入对话框中添加密码框?

C# 在Mahapps Metro的输入对话框中添加密码框?,c#,wpf,mahapps.metro,C#,Wpf,Mahapps.metro,我有一个输入对话框的示例代码,它在Mahapps Metro中运行得非常好,只需要将文本字段更改为密码字段。实际对话框位于此文件和文件中 这听起来很简单,我所要做的就是用一个密码框修改xaml文件,但其他一切保持不变。唯一的问题是,要激活对话框,在DialogManager中调用一个名为showInputSync()的方法,该方法实例化InputDialog。问题是,构造函数是内部的 namespace MahApps.Metro.Controls.Dialogs { public pa

我有一个输入对话框的示例代码,它在Mahapps Metro中运行得非常好,只需要将文本字段更改为密码字段。实际对话框位于此文件和文件中

这听起来很简单,我所要做的就是用一个密码框修改xaml文件,但其他一切保持不变。唯一的问题是,要激活对话框,在DialogManager中调用一个名为showInputSync()的方法,该方法实例化InputDialog。问题是,构造函数是内部的

namespace MahApps.Metro.Controls.Dialogs
{
    public partial class InputDialog : BaseMetroDialog
    {
        internal InputDialog(MetroWindow parentWindow, MetroDialogSettings settings)
            : base(parentWindow, settings)
        {
            InitializeComponent();
        }
来自DialogManager的代码

using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;

namespace order
{
    public static class DialogManager
    {
        public static Task<string> ShowInputAsync(this MetroWindow window, string title, string message, MetroDialogSettings settings = null)
        {
            window.Dispatcher.VerifyAccess();
            return HandleOverlayOnShow(settings, window).ContinueWith(z =>
            {
                return (Task<string>)window.Dispatcher.Invoke(new Func<Task<string>>(() =>
                {
                    if (settings == null)
                        settings = window.MetroDialogOptions;

                    //create the dialog control
                    InputDialog dialog = new InputDialog(window, settings); // this is where I need my own dialog created (xaml/cs files)
使用MahApps.Metro.Controls;
使用MahApps.Metro.Controls.Dialogs;
名称空间顺序
{
公共静态类对话框管理器
{
公共静态任务ShowInputAsync(此MetroWindow窗口、字符串标题、字符串消息、MetroDialogSettings设置=null)
{
window.Dispatcher.VerifyAccess();
返回HandleOverlyOnShow(设置,窗口)。继续(z=>
{
return(Task)window.Dispatcher.Invoke(new Func)(()=>
{
如果(设置==null)
设置=window.MetroDialogOptions;
//创建对话框控件
InputDialog=新建InputDialog(窗口,设置);//这是我需要创建自己的对话框(xaml/cs文件)的地方

有没有办法重复使用代码,或者我必须从头开始编写所有这些管道?

因为它是内部的,如果类位于同一命名空间中,您始终可以访问该类中的构造函数。尽管这通常是一种不好的编程实践,但您可以在MahApps.Metro.Controls.D中的新类中继承该类iLogs:

namespace MahApps.Metro.Controls.Dialogs
{
    public class MyCustomDialog : InputDialog
    {
         public MyCustomDialog(MetroWindow parentWindow, MetroDialogSettings settings) : base(parentWindow, settings)
         {
         // Your custom code here
         }
    }
}
这只是一个想法,希望能有所帮助


编辑:刚刚在这里找到这个:可能会有帮助。

我需要一个自定义输入对话框。所以我创建了一个从BaseMetroDialog继承的CustomInputDialog类

我使用以下代码调用该方法:

public async Task<string> ShowCustomDialog(string message, string title)
        {
            var metroDialogSettings = new MetroDialogSettings()
            {
                AffirmativeButtonText = "OK",
                NegativeButtonText = "CANCEL",
                AnimateHide = true,
                AnimateShow = true,
                ColorScheme = MetroDialogColorScheme.Accented,
            };

            var dialog = new CustomInputDialog(View, metroDialogSettings)
            {
                Message = message,
                Title = title,
                Input = metroDialogSettings.DefaultText
            };

            return await InvokeOnCurrentDispatcher(async () =>
            {
                await View.ShowMetroDialogAsync(dialog, metroDialogSettings);

                await dialog.WaitForButtonPressAsync().ContinueWith((m) =>
                    {
                        InvokeOnCurrentDispatcher(() => View.HideMetroDialogAsync(dialog));
                    });

                return dialog.Input;
            });
        }
公共异步任务ShowCustomDialog(字符串消息、字符串标题)
{
var metroDialogSettings=新metroDialogSettings()
{
确认按钮text=“确定”,
NegativeButtonText=“取消”,
AnimateHide=true,
AnimateShow=true,
配色方案=MetroDialogColorScheme.重音,
};
var对话框=新建CustomInputDialog(视图、metroDialogSettings)
{
消息=消息,
头衔,
输入=metroDialogSettings.DefaultText
};
return wait wait InvokeOnCurrentDispatcher(异步()=>
{
等待查看。ShowMetroDialogAsync(对话框,metroDialogSettings);
等待对话框。WaitForButtonPressSync()。继续((m)=>
{
InvokeOnCurrentDispatcher(()=>View.HideMetroDialogAsync(dialog));
});
返回对话框。输入;
});
}
您可以添加密码框或选择显示的任何视图。 例如,您可以查看Mahapps.Metro的InputDialog中的代码


与消息一样,标题和输入也是CustomInputDialog的依赖属性。这在我这方面起作用。

这里有一个简单的函数,用于在Mahapps中实现基本登录:

 private async void ShowLoginDialog(object sender, RoutedEventArgs e)
    {
        LoginDialogData result = await this.ShowLoginAsync("Authentication", "Enter your credentials", new LoginDialogSettings { ColorScheme = this.MetroDialogOptions.ColorScheme, InitialUsername = "MahApps"});
        if (result == null)
        {
            //User pressed cancel
        }
        else
        {
            MessageDialogResult messageResult = await this.ShowMessageAsync("Authentication Information", String.Format("Username: {0}\nPassword: {1}", result.Username, result.Password));
        }
    }
它可以在中找到。如果你想简化它,可以这样叫它

ShowLoginDialog(null,null);

你能继承XAML文件吗?事实上,我只需要将文本框更改为密码框,这需要很多代码……如果不能,这是迄今为止最好的解决方案。谢谢。