提交 e72c6c4c 编写于 作者: D Dan Siegel

chore: update WPF sample for updated Dialog API

上级 ec2c1b2b
using Prism.Services.Dialogs; using Prism.Dialogs;
using System; using System;
namespace HelloWorld.Core namespace HelloWorld.Core
......
using Prism.Services.Dialogs; using Prism.Dialogs;
using System.Windows; using System.Windows;
namespace HelloWorld.Dialogs namespace HelloWorld.Dialogs
......
using Prism.Services.Dialogs; using Prism.Dialogs;
using System.Windows; using System.Windows;
namespace HelloWorld.Dialogs namespace HelloWorld.Dialogs
......
using Prism.Commands; using Prism.Commands;
using Prism.Mvvm; using Prism.Mvvm;
using Prism.Services.Dialogs; using Prism.Dialogs;
using System; using System;
namespace HelloWorld.Dialogs namespace HelloWorld.Dialogs
...@@ -18,18 +18,19 @@ namespace HelloWorld.Dialogs ...@@ -18,18 +18,19 @@ namespace HelloWorld.Dialogs
set { SetProperty(ref _title, value); } set { SetProperty(ref _title, value); }
} }
public event Action<IDialogResult> RequestClose; public DialogCloseListener RequestClose { get; }
protected virtual void CloseDialog(string parameter) protected virtual void CloseDialog(string parameter)
{ {
ButtonResult result = ButtonResult.None; var result = parameter?.ToLower() switch
{
if (parameter?.ToLower() == "true") "true" => ButtonResult.OK,
result = ButtonResult.OK; "false" => ButtonResult.Cancel,
else if (parameter?.ToLower() == "false") _ => ButtonResult.None
result = ButtonResult.Cancel; };
RequestClose?.Invoke(new DialogResult(result)); //RequestClose.Invoke(new DialogResult(result));
RequestClose.Invoke(result);
} }
public virtual bool CanCloseDialog() public virtual bool CanCloseDialog()
......
using Prism.Services.Dialogs; using Prism.Dialogs;
namespace HelloWorld.Dialogs namespace HelloWorld.Dialogs
{ {
......
...@@ -3,7 +3,7 @@ using HelloWorld.Core; ...@@ -3,7 +3,7 @@ using HelloWorld.Core;
using Prism.Commands; using Prism.Commands;
using Prism.Mvvm; using Prism.Mvvm;
using Prism.Regions; using Prism.Regions;
using Prism.Services.Dialogs; using Prism.Dialogs;
namespace HelloWorld.ViewModels namespace HelloWorld.ViewModels
{ {
......
using Prism.Commands; using Prism.Commands;
using Prism.Mvvm; using Prism.Mvvm;
using Prism.Services.Dialogs; using Prism.Dialogs;
using HelloWorld.Core; using HelloWorld.Core;
namespace HelloWorld.Modules.ModuleA.ViewModels namespace HelloWorld.Modules.ModuleA.ViewModels
......
...@@ -35,6 +35,13 @@ public struct DialogCloseListener ...@@ -35,6 +35,13 @@ public struct DialogCloseListener
public void Invoke() => public void Invoke() =>
Invoke(new DialogResult()); Invoke(new DialogResult());
/// <summary>
/// Invokes the initialized delegate with the specified <see cref="ButtonResult"/>.
/// </summary>
/// <param name="result">The <see cref="ButtonResult"/>.</param>
public void Invoke(ButtonResult result) =>
Invoke(new DialogResult(result));
/// <summary> /// <summary>
/// Invokes the initialized delegate with the specified <see cref="IDialogParameters"/>. /// Invokes the initialized delegate with the specified <see cref="IDialogParameters"/>.
/// </summary> /// </summary>
......
...@@ -9,6 +9,23 @@ namespace Prism.Dialogs; ...@@ -9,6 +9,23 @@ namespace Prism.Dialogs;
/// </summary> /// </summary>
public class DialogResult : IDialogResult public class DialogResult : IDialogResult
{ {
/// <summary>
/// Creates a new <see cref="DialogResult"/>
/// </summary>
public DialogResult()
: this(ButtonResult.None)
{
}
/// <summary>
/// Creates a new <see cref="DialogResult"/> with a specified <see cref="ButtonResult"/>
/// </summary>
/// <param name="result"></param>
public DialogResult(ButtonResult result)
{
Result = result;
}
/// <summary> /// <summary>
/// An <see cref="System.Exception"/> that was thrown by the DialogService /// An <see cref="System.Exception"/> that was thrown by the DialogService
/// </summary> /// </summary>
......
...@@ -38,29 +38,35 @@ public static class DialogUtilities ...@@ -38,29 +38,35 @@ public static class DialogUtilities
private static void SetListener(IDialogAware dialogAware, DialogCloseListener listener) private static void SetListener(IDialogAware dialogAware, DialogCloseListener listener)
{ {
var type = dialogAware.GetType(); var setter = GetListenerSetter(dialogAware, dialogAware.GetType());
setter(listener);
}
private static Action<DialogCloseListener> GetListenerSetter(IDialogAware dialogAware, Type type)
{
var propInfo = type.GetProperty(nameof(IDialogAware.RequestClose)); var propInfo = type.GetProperty(nameof(IDialogAware.RequestClose));
if(propInfo is not null && propInfo.PropertyType == typeof(DialogCloseListener) && propInfo.SetMethod is not null) if (propInfo is not null && propInfo.PropertyType == typeof(DialogCloseListener) && propInfo.SetMethod is not null)
{ {
propInfo.SetValue(dialogAware, listener); return x => propInfo.SetValue(dialogAware, x);
return;
} }
var fields = type.GetRuntimeFields().Where(x => x.FieldType == typeof(DialogCloseListener)); var fields = type.GetRuntimeFields().Where(x => x.FieldType == typeof(DialogCloseListener));
var field = fields.FirstOrDefault(x => x.Name == $"<{nameof(IDialogAware.RequestClose)}>k__BackingField"); var field = fields.FirstOrDefault(x => x.Name == $"<{nameof(IDialogAware.RequestClose)}>k__BackingField");
if (field is not null) if (field is not null)
{ {
field.SetValue(dialogAware, listener); return x => field.SetValue(dialogAware, x);
} }
else if (fields.Any()) else if (fields.Any())
{ {
field = fields.First(); field = fields.First();
field.SetValue(dialogAware, listener); return x => field.SetValue(dialogAware, x);
} }
else
{ var baseType = type.BaseType;
if (baseType is null || baseType == typeof(object))
throw new DialogException(DialogException.UnableToSetTheDialogCloseListener); throw new DialogException(DialogException.UnableToSetTheDialogCloseListener);
}
return GetListenerSetter(dialogAware, baseType);
} }
} }
...@@ -21,6 +21,6 @@ using System.Windows.Markup; ...@@ -21,6 +21,6 @@ using System.Windows.Markup;
[assembly: XmlnsDefinition("http://prismlibrary.com/", "Prism.Regions.Behaviors")] [assembly: XmlnsDefinition("http://prismlibrary.com/", "Prism.Regions.Behaviors")]
[assembly: XmlnsDefinition("http://prismlibrary.com/", "Prism.Mvvm")] [assembly: XmlnsDefinition("http://prismlibrary.com/", "Prism.Mvvm")]
[assembly: XmlnsDefinition("http://prismlibrary.com/", "Prism.Interactivity")] [assembly: XmlnsDefinition("http://prismlibrary.com/", "Prism.Interactivity")]
[assembly: XmlnsDefinition("http://prismlibrary.com/", "Prism.Services.Dialogs")] [assembly: XmlnsDefinition("http://prismlibrary.com/", "Prism.Dialogs")]
[assembly: XmlnsDefinition("http://prismlibrary.com/", "Prism.Ioc")] [assembly: XmlnsDefinition("http://prismlibrary.com/", "Prism.Ioc")]
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册