[英]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.