Skip to content

Commit

Permalink
Merge pull request #142 from AvaloniaCommunity/feature/Prism9x-v9.0.2…
Browse files Browse the repository at this point in the history
…71-Samples-New-Navigation

Prism v9.0.271-pre - Update End-to-End Samples
  • Loading branch information
DamianSuess authored Apr 13, 2024
2 parents 72d16e5 + 6147816 commit 560667c
Show file tree
Hide file tree
Showing 27 changed files with 172 additions and 70 deletions.
2 changes: 1 addition & 1 deletion e2e/DummyModule1/DummyModule1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using Prism.Ioc;
////using Prism.Logging;
using Prism.Modularity;
using Prism.Regions;
using Prism.Navigation.Regions;

namespace DummyModule
{
Expand Down
2 changes: 1 addition & 1 deletion e2e/DummyModule1/Views/DummyModuleView.axaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="clr-namespace:Prism.Regions.Behaviors;assembly=Prism.Avalonia"
xmlns:prism="clr-namespace:Prism.Navigation.Regions.Behaviors;assembly=Prism.Avalonia"
x:Class="DummyModule.View.DummyModuleView">
<StackPanel Orientation="Horizontal">
<TextBox Text="DummyModule1 Region View"
Expand Down
2 changes: 1 addition & 1 deletion e2e/DummyModule2/DumyModule2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using ModulesSample.Infrastructure;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
using Prism.Navigation.Regions;

namespace DummyModule2
{
Expand Down
4 changes: 2 additions & 2 deletions e2e/DummyModule2/Views/DummyModuleView2.axaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="clr-namespace:Prism.Regions.Behaviors;assembly=Prism.Avalonia"
xmlns:prismRegions="clr-namespace:Prism.Regions;assembly=Prism.Avalonia"
xmlns:prism="clr-namespace:Prism.Navigation.Regions.Behaviors;assembly=Prism.Avalonia"
xmlns:prismRegions="clr-namespace:Prism.Navigation.Regions;assembly=Prism.Avalonia"
x:Class="DummyModule2.View.DummyModuleView2">
<Grid>
<TextBox Name="RegionViewTextBox"
Expand Down
2 changes: 1 addition & 1 deletion e2e/DummyModule2/Views/DummyModuleView2.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using ModulesSample.Infrastructure;
using Prism.Avalonia.Infrastructure.Events;
using Prism.Events;
using Prism.Regions;
using Prism.Navigation.Regions;

namespace DummyModule2.View
{
Expand Down
2 changes: 1 addition & 1 deletion e2e/ModulesSample/MainWindow.axaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prismRegions="clr-namespace:Prism.Regions;assembly=Prism.Avalonia"
xmlns:prismRegions="clr-namespace:Prism.Navigation.Regions;assembly=Prism.Avalonia"
MinWidth="500" MinHeight="300"
x:Class="ModulesSample.MainWindow">
<StackPanel Orientation="Vertical">
Expand Down
3 changes: 2 additions & 1 deletion e2e/SampleDialogApp/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using Avalonia;
using Avalonia.Markup.Xaml;
using Prism.DryIoc;
Expand Down Expand Up @@ -27,5 +27,6 @@ protected override void RegisterTypes(IContainerRegistry containerRegistry)
containerRegistry.Register<MainWindow>();
containerRegistry.RegisterDialog<MessageBoxView, MessageBoxViewModel>();
containerRegistry.RegisterDialog<DialogView, DialogViewModel>();
containerRegistry.RegisterDialogWindow<CustomDialogWindow>(nameof(CustomDialogWindow));
}
}
71 changes: 51 additions & 20 deletions e2e/SampleDialogApp/ViewModels/DialogViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class DialogViewModel : BindableBase, IDialogAware
private readonly IDialogService _dialogService;
private string _customMessage = string.Empty;
private string _title = "Notification";

/// <summary>Custom Prism.Avalonia feature.</summary>
private Window? _parentWindow = null;

public DialogViewModel(IDialogService dialogService)
Expand All @@ -23,47 +25,75 @@ public DialogViewModel(IDialogService dialogService)
Title = "I'm a Sample Dialog!";
}

// New: v9.0.271-pre
public DialogCloseListener RequestClose { get; }

public string Title { get => _title; set => SetProperty(ref _title, value); }

// Custom Prism.Avalonia feature
/// <summary>Gets or sets the optional parent window of this Dialog pop-up.</summary>
public Window? ParentWindow { get => _parentWindow; set => SetProperty(ref _parentWindow, value); }

public DelegateCommand CmdModalDialog => new(() =>
{
var title = "MessageBox Title Here";
var message = "Hello, I am a modal MessageBox window.\n\n" +
$"I {(ParentWindow == null ? "dont" : "do")} have a parent.";
var message = "Hello, I am a modal MessageBox called via the ViewModel.\n\n" +
"My parent window is the DialogView's window.";

// v9.0.271
_dialogService.ShowDialog(
name: nameof(MessageBoxView),
parameters: new DialogParameters($"title={title}&message={message}")
{
// This informs the DialogService to use this window for MessageBoxView's parent.
{ KnownDialogParameters.ParentWindow , ParentWindow },
});

// v8.1.97
////_dialogService.ShowDialog(
//// ParentWindow,
//// nameof(MessageBoxView),
//// new DialogParameters($"title={title}&message={message}"));
});

public DelegateCommand CmdCustomDialogWindow => new(() =>
{
// This passes the "title" to the Custom window's Title
// that is based off of it's inner 'MessageBoxView' UserControl.
var title = "Custom Title";
var message = "Hello, I custom dialog window running inside of Prism.Avalonia!";

// NOTE:
// When setting, 'KnownDialogParameters.WindowName' the DialogService
// uses SampleDialogApp's CustomDialogWindow instead of
// Prism.Avalonia's default DialogWindow.
_dialogService.ShowDialog(
ParentWindow,
nameof(MessageBoxView),
new DialogParameters($"title={title}&message={message}"));
name: nameof(MessageBoxView),
new DialogParameters
{
{ "title", title },
{ "message", message },
{ KnownDialogParameters.WindowName, nameof(CustomDialogWindow) }
});
});

public DelegateCommand<string> CmdResult => new DelegateCommand<string>((param) =>
{
// None = 0
// OK = 1
// Cancel = 2
// Abort = 3
// Retry = 4
// Ignore = 5
// Yes = 6
// No = 7
// None = 0, OK = 1, Cancel = 2, Abort = 3, Retry = 4, Ignore = 5, Yes = 6, No = 7
ButtonResult result = ButtonResult.None;

if (int.TryParse(param, out int intResult))
result = (ButtonResult)intResult;

RaiseRequestClose(new DialogResult(result));
// NEW: 9.0.271-pre
RequestClose.Invoke(result);

// OLD: v8.1.97
////RaiseRequestClose(new DialogResult(result));
});

public string CustomMessage { get => _customMessage; set => SetProperty(ref _customMessage, value); }

public event Action<IDialogResult>? RequestClose;

public virtual bool CanCloseDialog()
{
// Allow the dialog to close
Expand All @@ -80,8 +110,9 @@ public void OnDialogOpened(IDialogParameters parameters)
CustomMessage = parameters.GetValue<string>("message");
}

public virtual void RaiseRequestClose(IDialogResult dialogResult)
{
RequestClose?.Invoke(dialogResult);
}
// OLD: v8.1.97
////public virtual void RaiseRequestClose(IDialogResult dialogResult)
////{
//// RequestClose?.Invoke(dialogResult);
////}
}
2 changes: 1 addition & 1 deletion e2e/SampleDialogApp/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public MainWindowViewModel(IDialogService dialogService)
var message = "Hello, I am a simple MessageBox modal window with an OK button.\n\n" +
"When too much text is added, a scrollbar will appear.";

_dialogService.ShowDialog(nameof(MessageBoxView), new DialogParameters($"title={title}&message={message}"), r => { });
_dialogService.ShowDialog(nameof(MessageBoxView), new DialogParameters($"title={title}&message={message}"));
});

public DelegateCommand CmdShowDialog => new DelegateCommand(() =>
Expand Down
37 changes: 18 additions & 19 deletions e2e/SampleDialogApp/ViewModels/MessageBoxViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public MessageBoxViewModel()
MaxWidth = 600;
}

public event Action<IDialogResult>? RequestClose;
// v9.0.271-pre
public DialogCloseListener RequestClose { get; }

// v8.1.97
// public event Action<IDialogResult>? RequestClose;

public string Title { get => _title; set => SetProperty(ref _title, value); }

Expand All @@ -37,29 +41,23 @@ public MessageBoxViewModel()

public DelegateCommand<string> CmdResult => new DelegateCommand<string>((param) =>
{
// None = 0
// OK = 1
// Cancel = 2
// Abort = 3
// Retry = 4
// Ignore = 5
// Yes = 6
// No = 7
// None = 0, OK = 1, Cancel = 2, Abort = 3, Retry = 4, Ignore = 5, Yes = 6, No = 7
ButtonResult result = ButtonResult.None;

if (int.TryParse(param, out int intResult))
result = (ButtonResult)intResult;

RaiseRequestClose(new DialogResult(result));
// v9.0.271-pre
RequestClose.Invoke(result);

// v8.1.97
// RaiseRequestClose(new DialogResult(result));
});

public string CustomMessage { get => _customMessage; set => SetProperty(ref _customMessage, value); }

public virtual bool CanCloseDialog()
{
// Allow the dialog to close
return true;
}
/// <summary>Allow the dialog to close</summary>
public virtual bool CanCloseDialog() => true;

public virtual void OnDialogClosed()
{
Expand All @@ -75,8 +73,9 @@ public void OnDialogOpened(IDialogParameters parameters)
CustomMessage = parameters.GetValue<string>("message");
}

public virtual void RaiseRequestClose(IDialogResult dialogResult)
{
RequestClose?.Invoke(dialogResult);
}
// v8.1.97
////public virtual void RaiseRequestClose(IDialogResult dialogResult)
////{
//// RequestClose?.Invoke(dialogResult);
////}
}
2 changes: 1 addition & 1 deletion e2e/SampleDialogApp/ViewModels/ViewModelBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Prism.Mvvm;
using Prism.Regions;
using Prism.Navigation.Regions;

namespace SampleDialogApp.ViewModels;

Expand Down
20 changes: 20 additions & 0 deletions e2e/SampleDialogApp/Views/CustomDialogWindow.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="400" d:DesignHeight="200"
x:Class="SampleDialogApp.Views.CustomDialogWindow"
Title="{Binding Title}"
Background="BlueViolet"
WindowStartupLocation="CenterOwner">
<Window.Styles>
<Style Selector="Window">
<Setter Property="SizeToContent" Value="WidthAndHeight" />
</Style>
</Window.Styles>
<Panel>
<StackPanel>
<Label Content="This content gets overridden!" />
</StackPanel>
</Panel>
</Window>
26 changes: 26 additions & 0 deletions e2e/SampleDialogApp/Views/CustomDialogWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Prism.Dialogs;

namespace SampleDialogApp.Views;

/// <summary>Custom dialog window host.</summary>
public partial class CustomDialogWindow : Window, IDialogWindow
{
public CustomDialogWindow()
{
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
}

/// <summary>The <see cref="IDialogResult"/> of the dialog.</summary>
public IDialogResult Result { get; set; }

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}
6 changes: 5 additions & 1 deletion e2e/SampleDialogApp/Views/DialogView.axaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<UserControl xmlns="https://github.com/avaloniaui"
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Expand All @@ -16,6 +16,10 @@
<Button Content="Modal from MVVM" Command="{Binding CmdModalDialog}" />
</StackPanel>

<StackPanel Orientation="Horizontal" Spacing="5">
<Button Content="Custom Dialog Window" Command="{Binding CmdCustomDialogWindow}" />
</StackPanel>

<StackPanel Orientation="Horizontal">
<Label Content="Custom Param:" />
<Label Content="{Binding CustomMessage}" />
Expand Down
22 changes: 17 additions & 5 deletions e2e/SampleDialogApp/Views/DialogView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,25 @@ private void BtnShowModal_Click(object sender, RoutedEventArgs args)
var dialogSvc = ContainerLocator.Current.Resolve<IDialogService>();

var title = "MessageBox Title Here";
var message = "Hello, I am a simple modal MessageBox with an OK button.\n\n" +
"I've been called by the `.axaml.cs` UserControl.";
var message = "Hello, I am a modal MessageBox called via the `.axaml.cs` UserControl.\n\n" +
"My parent window is the DialogView's window.";

// Really you could just use 'this.Parent'. This ensures that our parent is a window and not another UserControl.
var parentWindow = this.Parent is Window parent ? parent : null;

dialogSvc.ShowDialog(
this.Parent as Window,
nameof(MessageBoxView),
new DialogParameters($"title={title}&message={message}"),
r => { });
new DialogParameters($"title={title}&message={message}")
{
// This informs the DialogService to use this window for MessageBoxView's parent.
{ KnownDialogParameters.ParentWindow, this.Parent },
});

// v8.1.97
////dialogSvc.ShowDialog(
//// this.Parent as Window,
//// nameof(MessageBoxView),
//// new DialogParameters($"title={title}&message={message}"),
//// r => { });
}
}
2 changes: 1 addition & 1 deletion e2e/SampleDialogApp/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Window x:Class="SampleDialogApp.Views.MainWindow"
<Window x:Class="SampleDialogApp.Views.MainWindow"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Expand Down
2 changes: 1 addition & 1 deletion e2e/SampleMvvmApp/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
using Prism.Navigation.Regions;
using SampleMvvmApp.Services;
using SampleMvvmApp.ViewModels;
using SampleMvvmApp.Views;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using System.Threading.Tasks;
using Avalonia.Controls;
using Prism.Regions;
using Prism.Navigation.Regions;

namespace SampleMvvmApp.RegionAdapters
{
Expand Down Expand Up @@ -70,6 +70,7 @@ protected override void Adapt(IRegion region, ItemsControl regionTarget)
{
items.Add(enumerator.Current);
}

regionTarget.ItemsSource = items;
};
}
Expand Down
2 changes: 1 addition & 1 deletion e2e/SampleMvvmApp/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Prism.Regions;
using Prism.Navigation.Regions;

namespace SampleMvvmApp.ViewModels
{
Expand Down
Loading

0 comments on commit 560667c

Please sign in to comment.