feat: add overlay dialog sample.
This commit is contained in:
18
demo/Ursa.Demo/Dialogs/DialogWithAction.axaml
Normal file
18
demo/Ursa.Demo/Dialogs/DialogWithAction.axaml
Normal 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>
|
||||
13
demo/Ursa.Demo/Dialogs/DialogWithAction.axaml.cs
Normal file
13
demo/Ursa.Demo/Dialogs/DialogWithAction.axaml.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
36
demo/Ursa.Demo/Dialogs/DialogWithActionViewModel.cs
Normal file
36
demo/Ursa.Demo/Dialogs/DialogWithActionViewModel.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
8
demo/Ursa.Demo/Dialogs/PlainDialog.axaml
Normal file
8
demo/Ursa.Demo/Dialogs/PlainDialog.axaml
Normal 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>
|
||||
13
demo/Ursa.Demo/Dialogs/PlainDialog.axaml.cs
Normal file
13
demo/Ursa.Demo/Dialogs/PlainDialog.axaml.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
8
demo/Ursa.Demo/Dialogs/PlainDialogViewModel.cs
Normal file
8
demo/Ursa.Demo/Dialogs/PlainDialogViewModel.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
|
||||
namespace Ursa.Demo.Dialogs;
|
||||
|
||||
public class PlainDialogViewModel: ObservableObject
|
||||
{
|
||||
|
||||
}
|
||||
@@ -13,6 +13,10 @@
|
||||
mc:Ignorable="d">
|
||||
<Grid ColumnDefinitions="Auto, *">
|
||||
<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 ShowLocalOverlayDialogCommand}">Show Local Overlay Dialog</Button>
|
||||
<Button Command="{Binding ShowGlobalOverlayDialogCommand}"> Show Global Overlay Dialog </Button>
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Windows.Input;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Ursa.Controls;
|
||||
using Ursa.Demo.Dialogs;
|
||||
using Ursa.Demo.Pages;
|
||||
|
||||
namespace Ursa.Demo.ViewModels;
|
||||
@@ -14,6 +15,14 @@ public class DialogDemoViewModel: ObservableObject
|
||||
public ICommand ShowGlobalOverlayDialogCommand { get; }
|
||||
public ICommand ShowGlobalDialogCommand { get; }
|
||||
|
||||
private object? _result;
|
||||
|
||||
public object? Result
|
||||
{
|
||||
get => _result;
|
||||
set => SetProperty(ref _result, value);
|
||||
}
|
||||
|
||||
public DialogDemoViewModel()
|
||||
{
|
||||
ShowLocalOverlayDialogCommand = new AsyncRelayCommand(ShowLocalOverlayDialog);
|
||||
@@ -23,16 +32,19 @@ public class DialogDemoViewModel: ObservableObject
|
||||
|
||||
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()
|
||||
{
|
||||
await DialogBox.ShowOverlayAsync<ButtonGroupDemo, ButtonGroupDemoViewModel>(new ButtonGroupDemoViewModel(), "GlobalHost");
|
||||
await DialogBox.ShowOverlayAsync<DialogWithAction, DialogWithActionViewModel, bool>(new DialogWithActionViewModel(), "GlobalHost");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -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 TViewModel: new()
|
||||
{
|
||||
@@ -59,6 +59,6 @@ public static class DialogBox
|
||||
t.DataContext = vm;
|
||||
var host = OverlayDialogManager.GetOverlayDialogHost(hostId);
|
||||
host?.AddDialog(t);
|
||||
return null;
|
||||
return t.ShowAsync<TResult>();
|
||||
}
|
||||
}
|
||||
@@ -27,18 +27,36 @@ public class DialogControl: ContentControl
|
||||
{
|
||||
_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)
|
||||
{
|
||||
if (this.DataContext is IDialogContext context)
|
||||
{
|
||||
OnClose?.Invoke(this, context.DefaultCloseResult);
|
||||
OnClose?.Invoke(this, args);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user