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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23728130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:44:52  来源:igfitidea点击:

Adding password box to input dialog in Mahapps Metro?

c#wpfmahapps.metro

提问by ryseagain

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. The actual dialog is found in this csfile and xamlfile.

我有一个在 Mahapps Metro 中完美运行的输入对话框的示例代码,我只需要将文本字段更改为密码字段。实际对话框位于此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. Only problem is, to activate the dialog box a method is called in DialogManager called ShowInputAsync() that instantiates the InputDialog. Problem is, the constructor is internal.

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

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?

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

回答by Sondre

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

回答by joazlazer

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 MahappMaybe it will help.

编辑:刚刚在这里找到这个:How to add a passwordbox to dialog in Mahapp也许它会有所帮助。

回答by Akanksha Gaur

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. This is working at my end.

和 Message 一样,Title 和 Input 是 CustomInputDialog 的依赖属性。这在我的最后工作。