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

本文关键字:添加 密码 对话框 输入 Mahapps Metro | 更新日期: 2023-09-27 18:04:02

我有一个输入对话框的示例代码,在Mahapps Metro中完美地工作,只有我需要将文本字段更改为密码字段。实际的对话框在这个cs文件和xaml文件中找到。

这听起来很简单,我所要做的只是修改xaml文件与密码框,但保持其他一切相同。唯一的问题是,要激活对话框,在DialogManager中调用了一个名为ShowInputAsync()的方法,该方法实例化了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的输入对话框中添加密码框

下面是在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));
        }
    }

它可以在MahApps Github中找到。如果你想简化它,可以这样调用

ShowLoginDialog(null,null);

因为它是内部的,所以如果一个类的构造函数位于同一个命名空间中,那么您总是可以访问它。尽管这通常是一个不好的编程实践,但是您可以在mahapps . metro . controls . dialogue:

中的新类中继承那个类。
namespace MahApps.Metro.Controls.Dialogs
{
    public class MyCustomDialog : InputDialog
    {
         public MyCustomDialog(MetroWindow parentWindow, MetroDialogSettings settings) : base(parentWindow, settings)
         {
         // Your custom code here
         }
    }
}

这只是一个想法。希望能有所帮助!

编辑:刚刚发现这个在这里:如何添加一个密码框在Mahapp对话框也许它会有所帮助。

我需要一个自定义输入对话框。所以我创建了一个继承自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;
            });
        }

您可以添加密码框或您选择显示的任何视图。您可以查看Mahapps中的代码。例如Metro的InputDialog

和Message一样,Title和Input是CustomInputDialog的依赖属性。