Skip to content

Commit

Permalink
playing around with the dialog API
Browse files Browse the repository at this point in the history
  • Loading branch information
brianlagunas committed Jan 17, 2019
1 parent 457c725 commit 9acdecd
Show file tree
Hide file tree
Showing 20 changed files with 240 additions and 135 deletions.
9 changes: 2 additions & 7 deletions Sandbox/Wpf/HelloWorld/HelloWorld/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
using HelloWorld.Views;
using Prism.Ninject;
using Prism.Ioc;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using HelloWorld.Dialogs;

namespace HelloWorld
{
Expand All @@ -23,7 +18,7 @@ protected override Window CreateShell()

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{

containerRegistry.RegisterDialog<NotificationDialog, NotificationDialogViewModel>();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<UserControl x:Class="Prism.Services.Dialogs.DefaultDialogs.NotificationDialog"
<UserControl x:Class="HelloWorld.Dialogs.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">
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
Width="300" Height="150">
<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" />
<TextBlock Text="{Binding Message}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="0" TextWrapping="Wrap" />
<Button Command="{Binding CloseDialogCommand}" Content="OK" Width="75" Height="25" HorizontalAlignment="Right" Margin="0,10,0,0" Grid.Row="1" IsDefault="True" />
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HelloWorld.Dialogs
{
/// <summary>
/// Interaction logic for NotificationDialog.xaml
/// </summary>
public partial class NotificationDialog : UserControl
{
public NotificationDialog()
{
InitializeComponent();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Prism.Commands;
using Prism.Services.Dialogs;

namespace HelloWorld.Dialogs
{
public class NotificationDialogViewModel : DialogViewModelBase
{
private string _message;
public string Message
{
get { return _message; }
set { SetProperty(ref _message, value); }
}

public DelegateCommand CloseDialogCommand { get; set; }

public NotificationDialogViewModel()
{
Title = "Notification";
CloseDialogCommand = new DelegateCommand(CloseDialog);
}

private void CloseDialog()
{
RaiseRequestClose(new DialogResult(true));
}

public override void OnDialogOpened(IDialogParameters parameters)
{
Message = parameters.GetValue<string>("message");
}
}
}
8 changes: 8 additions & 0 deletions Sandbox/Wpf/HelloWorld/HelloWorld/HelloWorld.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Page Include="Dialogs\NotificationDialog.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand All @@ -68,6 +72,10 @@
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="Dialogs\NotificationDialog.xaml.cs">
<DependentUpon>NotificationDialog.xaml</DependentUpon>
</Compile>
<Compile Include="Dialogs\NotificationDialogViewModel.cs" />
<Compile Include="ViewModels\MainWindowViewModel.cs" />
<Compile Include="Views\MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,47 @@ public string Title

public MainWindowViewModel(IDialogService dialogService)
{
ShowDialogCommand = new DelegateCommand(ShowDialog);
_dialogService = dialogService;
ShowDialogCommand = new DelegateCommand(ShowDialog);
}

private void ShowDialog()
{
_dialogService.ShowNotification("This is a title", "This is a message!", r => Title = "CallBack");
var message = "This is a message that should be shown in the dialog.";

//using the dialog service as-is
_dialogService.ShowDialog("NotificationDialog", new DialogParameters($"message={message}"), r =>
{
if (!r.Result.HasValue)
Title = "Result is null";
else if (r.Result == true)
Title = "Result is True";
else if (r.Result == false)
Title = "Result is False";
else
Title = "What the hell did you do?";
});

//using custom extenions methods to simplify the app's dialogs
_dialogService.ShowNotification(message, r =>
{
if (!r.Result.HasValue)
Title = "Result is null";
else if (r.Result == true)
Title = "Result is True";
else if (r.Result == false)
Title = "Result is False";
else
Title = "What the hell did you do?";
});
}
}

public static class DialogServiceEstensions
{
public static void ShowNotification(this IDialogService dialogService, string message, Action<IDialogResult> callBack)
{
dialogService.ShowDialog("NotificationDialog", new DialogParameters($"message={message}"), callBack);
}
}
}
11 changes: 11 additions & 0 deletions Source/Wpf/Prism.Wpf/Ioc/IContainerRegistryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ namespace Prism.Ioc
{
public static class IContainerRegistryExtensions
{
public static void RegisterDialog<TView, TViewModel>(this IContainerRegistry containerRegistry, string name = null) where TViewModel : Services.Dialogs.IDialogAware
{
var viewType = typeof(TView);

if (string.IsNullOrWhiteSpace(name))
name = viewType.Name;

containerRegistry.Register(typeof(object), viewType, name);
ViewModelLocationProvider.Register<TView, TViewModel>();
}

/// <summary>
/// Registers an object for navigation
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
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>
WindowStartupLocation="CenterOwner" SizeToContent="WidthAndHeight">

</Window>
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ namespace Prism.Services.Dialogs.DefaultDialogs
/// </summary>
public partial class DialogWindow : Window, IDialogWindow
{
public IDialog ViewModel
public IDialogAware ViewModel
{
get { return (IDialog)DataContext; }
get { return (IDialogAware)DataContext; }
set { DataContext = value; }
}

public IDialogResult Result { get; set; }

public DialogWindow()
{
InitializeComponent();
Expand Down

This file was deleted.

This file was deleted.

8 changes: 4 additions & 4 deletions Source/Wpf/Prism.Wpf/Services/Dialogs/DialogParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

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
//TODO: should we reuse the NavigationParameters? 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)
{
}
public DialogParameters() : base() { }

public DialogParameters(string query) : base(query) { }
}
}
15 changes: 15 additions & 0 deletions Source/Wpf/Prism.Wpf/Services/Dialogs/DialogResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
{
public class DialogResult : IDialogResult
{
public IDialogParameters Parameters { get; private set; } = new DialogParameters();

public bool? Result { get; private set; }

public DialogResult() { }

public DialogResult(bool? result)
{
Result = result;
}

public DialogResult(bool? result, IDialogParameters parameters)
{
Result = result;
Parameters = parameters;
}
}
}
Loading

0 comments on commit 9acdecd

Please sign in to comment.