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">
<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>

View File

@@ -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;
}
}