gpt4 book ai didi

wpf - 如何在Wpf中的用户控件中调用MahApps Metro对话框

转载 作者:行者123 更新时间:2023-12-01 21:43:58 26 4
gpt4 key购买 nike

当我尝试调用 MahApps Metro 对话框时,我在传递值时遇到错误

在传递参数时调用Dialog Control时我需要传递Metrowindow参数

但我需要在用户控件中调用它

下面是我需要对话框控制时调用的方法

public async void ShowMessageDialog(object sender, RoutedEventArgs e)
{
// This demo runs on .Net 4.0, but we're using the Microsoft.Bcl.Async package so we have async/await support
// The package is only used by the demo and not a dependency of the library!
var mySettings = new MetroDialogSettings()
{
AffirmativeButtonText = "Hi",
NegativeButtonText = "Go away!",
FirstAuxiliaryButtonText = "Cancel",
// ColorScheme = MetroDialogOptions.ColorScheme
};

MessageDialogResult result = await this.ShowMessageAsync("Hello!", "Welcome to the world of metro!",
MessageDialogStyle.AffirmativeAndNegativeAndSingleAuxiliary, mySettings);

if (result != MessageDialogResult.FirstAuxiliary)
await this.ShowMessageAsync("Result", "You said: " + (result == MessageDialogResult.Affirmative ? mySettings.AffirmativeButtonText : mySettings.NegativeButtonText +
Environment.NewLine + Environment.NewLine + "This dialog will follow the Use Accent setting."));
}

public static Task<MessageDialogResult> ShowMessageAsync(this MetroWindow window, string title, string message, MessageDialogStyle style = MessageDialogStyle.Affirmative, MetroDialogSettings settings = null)
{
window.Dispatcher.VerifyAccess();
return HandleOverlayOnShow(settings, window).ContinueWith(z =>
{
return (Task<MessageDialogResult>)window.Dispatcher.Invoke(new Func<Task<MessageDialogResult>>(() =>
{
if (settings == null)
{
settings = window.MetroDialogOptions;
}

//create the dialog control
var dialog = new MessageDialog(window, settings)
{
Message = message,
Title = title,
ButtonStyle = style
};

SizeChangedEventHandler sizeHandler = SetupAndOpenDialog(window, dialog);
dialog.SizeChangedHandler = sizeHandler;

return dialog.WaitForLoadAsync().ContinueWith(x =>
{
if (DialogOpened != null)
{
window.Dispatcher.BeginInvoke(new Action(() => DialogOpened(window, new DialogStateChangedEventArgs())));
}

return dialog.WaitForButtonPressAsync().ContinueWith(y =>
{
//once a button as been clicked, begin removing the dialog.

dialog.OnClose();

if (DialogClosed != null)
{
window.Dispatcher.BeginInvoke(new Action(() => DialogClosed(window, new DialogStateChangedEventArgs())));
}

Task closingTask = (Task)window.Dispatcher.Invoke(new Func<Task>(() => dialog._WaitForCloseAsync()));
return closingTask.ContinueWith(a =>
{
return ((Task)window.Dispatcher.Invoke(new Func<Task>(() =>
{
window.SizeChanged -= sizeHandler;

window.RemoveDialog(dialog);

return HandleOverlayOnHide(settings, window);
}))).ContinueWith(y3 => y).Unwrap();
});
}).Unwrap();
}).Unwrap().Unwrap();
}));
}).Unwrap();
}

最佳答案

ShowMessageAsync 是 MetroWindow 的扩展方法,因此此代码应该可以工作:

var metroWindow = (Application.Current.MainWindow as MetroWindow); 
await metroWindow.ShowMessageAsync(title, message);

关于wpf - 如何在Wpf中的用户控件中调用MahApps Metro对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33951004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com