繁体   English   中英

在Mahapps Metro中将密码框添加到输入对话框?

[英]Adding password box to input dialog in Mahapps Metro?

我有一个输入对话框的示例代码,该代码在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中实现基本登录的简单功能:

 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.Dialogs中的新类中:

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中查看代码

像消息一样,标题和输入也是CustomInputDialog的依赖项属性。 这在我的工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM