feat: add overlay dialog sample.

This commit is contained in:
rabbitism
2024-01-22 18:32:24 +08:00
parent 5c62131a0a
commit f0ec32c870
10 changed files with 139 additions and 9 deletions

View File

@@ -0,0 +1,18 @@
<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"
xmlns:local="clr-namespace:Ursa.Demo.Dialogs"
x:DataType="local:DialogWithActionViewModel"
x:CompileBindings="True"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Ursa.Demo.Dialogs.DialogWithAction">
<StackPanel Margin="0 16 0 0">
<TextBlock Text="{Binding Title}"></TextBlock>
<Calendar SelectedDate="{Binding Date}" ></Calendar>
<StackPanel HorizontalAlignment="Right" Orientation="Horizontal" Spacing="20">
<Button Content="OK" Command="{Binding OKCommand}"></Button>
<Button Content="Cancel" Command="{Binding CancelCommand}"></Button>
</StackPanel>
</StackPanel>
</UserControl>

View File

@@ -0,0 +1,13 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace Ursa.Demo.Dialogs;
public partial class DialogWithAction : UserControl
{
public DialogWithAction()
{
InitializeComponent();
}
}

View File

@@ -0,0 +1,36 @@
using System;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Ursa.Controls;
namespace Ursa.Demo.Dialogs;
public partial class DialogWithActionViewModel: ObservableObject, IDialogContext
{
[ObservableProperty] private string _title;
[ObservableProperty] private DateTime _date;
public object? DefaultCloseResult { get; set; } = true;
public event EventHandler<object>? Closed;
public ICommand OKCommand { get; set; }
public ICommand CancelCommand { get; set; }
public DialogWithActionViewModel()
{
OKCommand = new RelayCommand(OK);
CancelCommand = new RelayCommand(Cancel);
Title = "Please select a date";
Date = DateTime.Now;
}
private void OK()
{
Closed?.Invoke(this, true);
}
private void Cancel()
{
Closed?.Invoke(this, false);
}
}

View File

@@ -0,0 +1,8 @@
<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"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Ursa.Demo.Dialogs.PlainDialog">
</UserControl>

View File

@@ -0,0 +1,13 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace Ursa.Demo.Dialogs;
public partial class PlainDialog : UserControl
{
public PlainDialog()
{
InitializeComponent();
}
}

View File

@@ -0,0 +1,8 @@
using CommunityToolkit.Mvvm.ComponentModel;
namespace Ursa.Demo.Dialogs;
public class PlainDialogViewModel: ObservableObject
{
}

View File

@@ -13,6 +13,10 @@
mc:Ignorable="d"> mc:Ignorable="d">
<Grid ColumnDefinitions="Auto, *"> <Grid ColumnDefinitions="Auto, *">
<StackPanel Grid.Column="0"> <StackPanel Grid.Column="0">
<TextBlock>
<Run Text="Result: "></Run>
<Run Text="{Binding Result}"></Run>
</TextBlock>
<Button Command="{Binding ShowGlobalDialogCommand}">Show Dialog</Button> <Button Command="{Binding ShowGlobalDialogCommand}">Show Dialog</Button>
<Button Command="{Binding ShowLocalOverlayDialogCommand}">Show Local Overlay Dialog</Button> <Button Command="{Binding ShowLocalOverlayDialogCommand}">Show Local Overlay Dialog</Button>
<Button Command="{Binding ShowGlobalOverlayDialogCommand}"> Show Global Overlay Dialog </Button> <Button Command="{Binding ShowGlobalOverlayDialogCommand}"> Show Global Overlay Dialog </Button>

View File

@@ -4,6 +4,7 @@ using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Input;
using Ursa.Controls; using Ursa.Controls;
using Ursa.Demo.Dialogs;
using Ursa.Demo.Pages; using Ursa.Demo.Pages;
namespace Ursa.Demo.ViewModels; namespace Ursa.Demo.ViewModels;
@@ -14,6 +15,14 @@ public class DialogDemoViewModel: ObservableObject
public ICommand ShowGlobalOverlayDialogCommand { get; } public ICommand ShowGlobalOverlayDialogCommand { get; }
public ICommand ShowGlobalDialogCommand { get; } public ICommand ShowGlobalDialogCommand { get; }
private object? _result;
public object? Result
{
get => _result;
set => SetProperty(ref _result, value);
}
public DialogDemoViewModel() public DialogDemoViewModel()
{ {
ShowLocalOverlayDialogCommand = new AsyncRelayCommand(ShowLocalOverlayDialog); ShowLocalOverlayDialogCommand = new AsyncRelayCommand(ShowLocalOverlayDialog);
@@ -23,16 +32,19 @@ public class DialogDemoViewModel: ObservableObject
private async Task ShowGlobalDialog() private async Task ShowGlobalDialog()
{ {
var result = await DialogBox.ShowAsync<ButtonGroupDemo, ButtonGroupDemoViewModel, string>(new ButtonGroupDemoViewModel()); var result = await DialogBox.ShowAsync<DialogWithAction, DialogWithActionViewModel, bool>(new DialogWithActionViewModel());
Result = result;
} }
private async Task ShowGlobalOverlayDialog() private async Task ShowGlobalOverlayDialog()
{ {
await DialogBox.ShowOverlayAsync<ButtonGroupDemo, ButtonGroupDemoViewModel>(new ButtonGroupDemoViewModel(), "GlobalHost"); await DialogBox.ShowOverlayAsync<DialogWithAction, DialogWithActionViewModel, bool>(new DialogWithActionViewModel(), "GlobalHost");
} }
private async Task ShowLocalOverlayDialog() private async Task ShowLocalOverlayDialog()
{ {
await DialogBox.ShowOverlayAsync<ButtonGroupDemo, ButtonGroupDemoViewModel>(new ButtonGroupDemoViewModel(), "LocalHost"); var result = await DialogBox.ShowOverlayAsync<DialogWithAction, DialogWithActionViewModel, bool>(
new DialogWithActionViewModel(), "LocalHost");
Result = result;
} }
} }

View File

@@ -47,7 +47,7 @@ public static class DialogBox
} }
public static async Task<object?> ShowOverlayAsync<TView, TViewModel>(TViewModel vm, string hostId) public static Task<TResult> ShowOverlayAsync<TView, TViewModel, TResult>(TViewModel vm, string hostId)
where TView : Control, new() where TView : Control, new()
where TViewModel: new() where TViewModel: new()
{ {
@@ -59,6 +59,6 @@ public static class DialogBox
t.DataContext = vm; t.DataContext = vm;
var host = OverlayDialogManager.GetOverlayDialogHost(hostId); var host = OverlayDialogManager.GetOverlayDialogHost(hostId);
host?.AddDialog(t); host?.AddDialog(t);
return null; return t.ShowAsync<TResult>();
} }
} }

View File

@@ -27,18 +27,36 @@ public class DialogControl: ContentControl
{ {
_closeButton.Click += Close; _closeButton.Click += Close;
} }
}
public void Show() if (this.DataContext is IDialogContext context)
{
context.Closed += Close;
}
}
public Task<T> ShowAsync<T>()
{ {
var tcs = new TaskCompletionSource<T>();
this.OnClose+= (sender, args) =>
{
if (args is T result)
{
tcs.SetResult(result);
}
else
{
tcs.SetResult(default);
}
};
return tcs.Task;
} }
private void Close(object sender, object args) private void Close(object sender, object args)
{ {
if (this.DataContext is IDialogContext context) if (this.DataContext is IDialogContext context)
{ {
OnClose?.Invoke(this, context.DefaultCloseResult); OnClose?.Invoke(this, args);
} }
else else
{ {