-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial attempt at a new dialog service
- Loading branch information
1 parent
7baeb32
commit 457c725
Showing
16 changed files
with
354 additions
and
3 deletions.
There are no files selected for viewing
17 changes: 15 additions & 2 deletions
17
Sandbox/Wpf/HelloWorld/HelloWorld/ViewModels/MainWindowViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,32 @@ | ||
using Prism.Mvvm; | ||
using System; | ||
using Prism.Commands; | ||
using Prism.Mvvm; | ||
using Prism.Services.Dialogs; | ||
|
||
namespace HelloWorld.ViewModels | ||
{ | ||
public class MainWindowViewModel : BindableBase | ||
{ | ||
private string _title = "Prism Application"; | ||
private readonly IDialogService _dialogService; | ||
|
||
public string Title | ||
{ | ||
get { return _title; } | ||
set { SetProperty(ref _title, value); } | ||
} | ||
|
||
public MainWindowViewModel() | ||
public DelegateCommand ShowDialogCommand { get; private set; } | ||
|
||
public MainWindowViewModel(IDialogService dialogService) | ||
{ | ||
ShowDialogCommand = new DelegateCommand(ShowDialog); | ||
_dialogService = dialogService; | ||
} | ||
|
||
private void ShowDialog() | ||
{ | ||
_dialogService.ShowNotification("This is a title", "This is a message!", r => Title = "CallBack"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
Source/Wpf/Prism.Wpf/Services/Dialogs/DefaultDialogs/DialogWindow.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Window x:Class="Prism.Services.Dialogs.DefaultDialogs.DialogWindow" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
Title="{Binding Title}" Icon="{Binding IconSource}" | ||
WindowStartupLocation="CenterOwner" MinWidth="300" MinHeight="150"> | ||
<Window.Style> | ||
<Style TargetType="{x:Type Window}" > | ||
<Setter Property="SizeToContent" Value="WidthAndHeight" /> | ||
</Style> | ||
</Window.Style> | ||
</Window> |
21 changes: 21 additions & 0 deletions
21
Source/Wpf/Prism.Wpf/Services/Dialogs/DefaultDialogs/DialogWindow.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System.Windows; | ||
|
||
namespace Prism.Services.Dialogs.DefaultDialogs | ||
{ | ||
/// <summary> | ||
/// Interaction logic for DialogWindow.xaml | ||
/// </summary> | ||
public partial class DialogWindow : Window, IDialogWindow | ||
{ | ||
public IDialog ViewModel | ||
{ | ||
get { return (IDialog)DataContext; } | ||
set { DataContext = value; } | ||
} | ||
|
||
public DialogWindow() | ||
{ | ||
InitializeComponent(); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
Source/Wpf/Prism.Wpf/Services/Dialogs/DefaultDialogs/NotificationDialog.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<UserControl x:Class="Prism.Services.Dialogs.DefaultDialogs.NotificationDialog" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:mvvm="clr-namespace:Prism.Mvvm" | ||
mvvm:ViewModelLocator.AutoWireViewModel="True" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> | ||
<Grid x:Name="LayoutRoot" Margin="5"> | ||
<Grid.RowDefinitions> | ||
<RowDefinition /> | ||
<RowDefinition Height="Auto" /> | ||
</Grid.RowDefinitions> | ||
|
||
<TextBlock Text="{Binding Message}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="0" /> | ||
<Button Command="{Binding CloseDialogCommand}" Content="OK" Width="75" Height="25" HorizontalAlignment="Right" Margin="0,10,0,0" Grid.Row="1" IsDefault="True" /> | ||
</Grid> | ||
</UserControl> |
15 changes: 15 additions & 0 deletions
15
Source/Wpf/Prism.Wpf/Services/Dialogs/DefaultDialogs/NotificationDialog.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.Windows.Controls; | ||
|
||
namespace Prism.Services.Dialogs.DefaultDialogs | ||
{ | ||
/// <summary> | ||
/// Interaction logic for NotificationDialog.xaml | ||
/// </summary> | ||
public partial class NotificationDialog : UserControl | ||
{ | ||
public NotificationDialog() | ||
{ | ||
InitializeComponent(); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
Source/Wpf/Prism.Wpf/Services/Dialogs/DefaultDialogs/NotificationDialogViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Prism.Commands; | ||
using Prism.Mvvm; | ||
using System; | ||
|
||
namespace Prism.Services.Dialogs.DefaultDialogs | ||
{ | ||
public class NotificationDialogViewModel : BindableBase, IDialog | ||
{ | ||
private string _title; | ||
public string Title | ||
{ | ||
get { return _title; } | ||
set { SetProperty(ref _title, value); } | ||
} | ||
|
||
public event Action RequestClose; | ||
|
||
private string _message; | ||
public string Message | ||
{ | ||
get { return _message; } | ||
set { SetProperty(ref _message, value); } | ||
} | ||
|
||
public DelegateCommand CloseDialogCommand { get; set; } | ||
|
||
private string _iconSource; | ||
public string IconSource | ||
{ | ||
get { return _iconSource; } | ||
set { SetProperty(ref _iconSource, value); } | ||
} | ||
|
||
public NotificationDialogViewModel() | ||
{ | ||
CloseDialogCommand = new DelegateCommand(CloseDialog); | ||
} | ||
|
||
private void CloseDialog() | ||
{ | ||
RequestClose?.Invoke(); | ||
} | ||
|
||
public bool CanCloseDialog() | ||
{ | ||
return true; | ||
} | ||
|
||
public void ProcessDialogParameters(IDialogParameters parameters) | ||
{ | ||
Title = parameters.GetValue<string>("title"); | ||
Message = parameters.GetValue<string>("message"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Prism.Regions; | ||
|
||
namespace Prism.Services.Dialogs | ||
{ | ||
//TODO: should we reuse the NavigatrionParameters? I'm not sure I want to add the regions namespace requirement for using dialogs | ||
public class DialogParameters : NavigationParameters, IDialogParameters | ||
{ | ||
public DialogParameters(string query) : base(query) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Prism.Services.Dialogs | ||
{ | ||
public class DialogResult : IDialogResult | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using Prism.Common; | ||
using Prism.Ioc; | ||
using Prism.Mvvm; | ||
using Prism.Services.Dialogs.DefaultDialogs; | ||
using System; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using System.Windows; | ||
|
||
namespace Prism.Services.Dialogs | ||
{ | ||
public class DialogService : IDialogService | ||
{ | ||
private readonly IContainerExtension _containerExtension; | ||
|
||
public DialogService(IContainerExtension containerExtension) | ||
{ | ||
_containerExtension = containerExtension; | ||
|
||
RegisterDialog<NotificationDialog, NotificationDialogViewModel>(); | ||
} | ||
|
||
public void ShowNotification(string title, string message, Action<IDialogResult> callback) | ||
{ | ||
var parameters = new DialogParameters($"?title={title}&message={message}"); | ||
|
||
IDialogWindow dialogWindow = CreateDialogWindow(); | ||
ConfigureDialogWindowEvents(dialogWindow, callback); | ||
ConfigureDialogWindowContent<NotificationDialog>(dialogWindow, parameters); | ||
|
||
//TODO: figure out how to control the parent (is this even neccessary? Should we assume the parent should be the active window? | ||
dialogWindow.Owner = Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.IsActive); | ||
//TODO: figure out how to control various properties of the window, maybe a WindowSettings object? | ||
//TODO: handle modal/non-modal dialogs | ||
dialogWindow.ShowDialog(); | ||
} | ||
|
||
//TODO: create extension point to provide a custom Window | ||
IDialogWindow CreateDialogWindow() | ||
{ | ||
return new DialogWindow(); | ||
} | ||
|
||
void ConfigureDialogWindowContent<T>(IDialogWindow window, IDialogParameters parameters) | ||
{ | ||
var content = _containerExtension.Resolve<T>(); | ||
var dialogContent = content as FrameworkElement; | ||
if (dialogContent == null) | ||
throw new NullReferenceException("A dialog's content must be a FrameworkElement"); | ||
|
||
var viewModel = dialogContent.DataContext as IDialog; | ||
if (viewModel == null) | ||
throw new NullReferenceException("A dialog's ViewModel must implement the IDialog interface"); | ||
|
||
MvvmHelpers.ViewAndViewModelAction<IDialog>(viewModel, d => d.ProcessDialogParameters(parameters)); | ||
|
||
window.Content = dialogContent; | ||
window.ViewModel = viewModel; | ||
} | ||
|
||
void ConfigureDialogWindowEvents(IDialogWindow dialogWindow, Action<IDialogResult> callback) | ||
{ | ||
Action requestCloseHandler = null; | ||
requestCloseHandler = () => | ||
{ | ||
dialogWindow.Close(); | ||
}; | ||
|
||
RoutedEventHandler loadedHandler = null; | ||
loadedHandler = (o, e) => | ||
{ | ||
dialogWindow.Loaded -= loadedHandler; | ||
dialogWindow.ViewModel.RequestClose += requestCloseHandler; | ||
}; | ||
dialogWindow.Loaded += loadedHandler; | ||
|
||
CancelEventHandler closingHandler = null; | ||
closingHandler = (o, e) => | ||
{ | ||
if (!dialogWindow.ViewModel.CanCloseDialog()) | ||
e.Cancel = true; | ||
}; | ||
dialogWindow.Closing += closingHandler; | ||
|
||
EventHandler closedHandler = null; | ||
closedHandler = (o, e) => | ||
{ | ||
dialogWindow.Closed -= closedHandler; | ||
dialogWindow.Closing -= closingHandler; | ||
dialogWindow.ViewModel.RequestClose -= requestCloseHandler; | ||
//TODO: get dialog result from ViewModel | ||
callback?.Invoke(new DialogResult()); | ||
dialogWindow.ViewModel = null; | ||
dialogWindow.Content = null; | ||
}; | ||
dialogWindow.Closed += closedHandler; | ||
} | ||
|
||
public void RegisterDialog<TView, TViewModel>() where TViewModel : IDialog | ||
{ | ||
_containerExtension.Register<TView>(); | ||
ViewModelLocationProvider.Register<TView, TViewModel>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
|
||
namespace Prism.Services.Dialogs | ||
{ | ||
public interface IDialog | ||
{ | ||
bool CanCloseDialog(); | ||
|
||
string IconSource { get; set; } | ||
|
||
string Title { get; set; } | ||
|
||
event Action RequestClose; | ||
|
||
void ProcessDialogParameters(IDialogParameters parameters); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
Source/Wpf/Prism.Wpf/Services/Dialogs/IDialogParameters.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Prism.Services.Dialogs | ||
{ | ||
public interface IDialogParameters | ||
{ | ||
void Add(string key, object value); | ||
|
||
bool ContainsKey(string key); | ||
|
||
int Count { get; } | ||
|
||
IEnumerable<string> Keys { get; } | ||
|
||
T GetValue<T>(string key); | ||
|
||
IEnumerable<T> GetValues<T>(string key); | ||
|
||
bool TryGetValue<T>(string key, out T value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Prism.Services.Dialogs | ||
{ | ||
public interface IDialogResult | ||
{ | ||
//TODO: what should we return? | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
|
||
namespace Prism.Services.Dialogs | ||
{ | ||
public interface IDialogService | ||
{ | ||
void ShowNotification(string title, string message, Action<IDialogResult> callback); | ||
|
||
//ShowConfirmation | ||
|
||
//ShowDialog - this is for custom dialogs | ||
|
||
//RegisterDialog | ||
|
||
//RegisterDialogWindow | ||
} | ||
} |
Oops, something went wrong.