简体   繁体   English

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

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

I have a sample code for an input dialog that works perfectly in Mahapps Metro and only I need to change the text field to a password field. 我有一个输入对话框的示例代码,该代码在Mahapps Metro中可以正常使用,仅我需要将文本字段更改为密码字段。 The actual dialog is found in this cs file and xaml file. 在此cs文件和xaml文件中找到实际的对话框。

That sounds simple enough, all I have to do is pretty much just modify the xaml file With a password box, but keep everything else the same. 这听起来很简单,我所要做的几乎就是仅使用密码框修改xaml文件,而保持其他所有内容不变。 Only problem is, to activate the dialog box a method is called in DialogManager called ShowInputAsync() that instantiates the InputDialog. 唯一的问题是,要激活对话框,将在DialogManager中调用名为ShowInputAsync()的方法来实例化InputDialog。 Problem is, the constructor is internal. 问题是,构造函数是内部的。

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

The code from the DialogManager 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)

Is there a way to re-use the code, or do I have to just Write all this plumbing from scratch? 有没有办法重新使用代码,还是我必须从头开始编写所有这些代码?

Here is a simple function for achiving a basic login in Mahapps: 这是在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));
        }
    }

It can be found in the MahApps Github .It can be called like this if you want it simplified 可以在MahApps Github中找到它,如果要简化它可以这样称呼

ShowLoginDialog(null,null);

Since it is internal, you can always access the constructor in a class if it is located in the same namespace. 由于它是内部的,因此如果位于同一命名空间中,则始终可以在类中访问构造函数。 Although this is usually a bad programming practice, you can inherit from that class in a new class located in MahApps.Metro.Controls.Dialogs: 尽管这通常是不好的编程习惯,但是您可以从该类中继承一个位于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
         }
    }
}

This is just an idea. 这只是一个想法。 Hope it helps! 希望能帮助到你!

Edit: Just found this here: How to add a passwordbox to dialog in Mahapp Maybe it will help. 编辑:刚刚在这里找到: 如何在Mahapp中向对话框添加密码框也许会有所帮助。

I needed a custom Input Dialog. 我需要一个自定义的输入对话框。 So I Created a CustomInputDialog class inherited from BaseMetroDialog. 因此,我创建了一个从BaseMetroDialog继承的CustomInputDialog类。

I used this code to call the method: 我使用以下代码来调用该方法:

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;
            });
        }

You can add a password box or whichever view you choose to display. 您可以添加密码框或选择显示的任何视图。 You can look at the code in Mahapps.Metro's InputDialog for example 例如,您可以在Mahapps.Metro的InputDialog中查看代码

Like Message, Title and Input are dependency properties of CustomInputDialog. 像消息一样,标题和输入也是CustomInputDialog的依赖项属性。 This is working at my end. 这在我的工作。

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

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