update pr/58
This commit is contained in:
1 parent
e9329ef982
commit
ce93f34467
8 files changed
+65
-112
No files matched your search
@@ -1,4 +1,5 @@
|
|||||||
using System.Windows.Controls;
|
using System.Windows;
|
||||||
|
using System.Windows.Controls;
|
||||||
using System.Windows.Documents;
|
using System.Windows.Documents;
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
using HandyControl.Interactivity;
|
using HandyControl.Interactivity;
|
||||||
@@ -10,8 +11,14 @@ namespace HandyControl.Controls
|
|||||||
{
|
{
|
||||||
private Adorner _container;
|
private Adorner _container;
|
||||||
|
|
||||||
// It can not be replaced with the FrameworkElement.IsLoaded property, which will be false when the dialog just created.
|
public static readonly DependencyProperty IsClosedProperty = DependencyProperty.Register(
|
||||||
public bool IsClosed { get; private set; }
|
"IsClosed", typeof(bool), typeof(Dialog), new PropertyMetadata(default(bool)));
|
||||||
|
|
||||||
|
public bool IsClosed
|
||||||
|
{
|
||||||
|
get => (bool) GetValue(IsClosedProperty);
|
||||||
|
internal set => SetValue(IsClosedProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
public Dialog()
|
public Dialog()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -5,29 +5,29 @@ using HandyControl.Controls;
|
|||||||
|
|
||||||
namespace HandyControl.Tools.Extension
|
namespace HandyControl.Tools.Extension
|
||||||
{
|
{
|
||||||
public interface IDialogResultable
|
public interface IDialogResultable<T>
|
||||||
{
|
{
|
||||||
object Result { get; set; }
|
T Result { get; set; }
|
||||||
|
|
||||||
Action ClosureToken { get; set; }
|
Action CloseAction { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class DialogExtension
|
public static class DialogExtension
|
||||||
{
|
{
|
||||||
public static Task<TResult> GetResultAsync<TResult>(this Dialog @this)
|
public static Task<TResult> GetResultAsync<TResult>(this Dialog dialog)
|
||||||
{
|
{
|
||||||
var tcs = new TaskCompletionSource<TResult>();
|
var tcs = new TaskCompletionSource<TResult>();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (@this.IsClosed)
|
if (dialog.IsClosed)
|
||||||
{
|
{
|
||||||
SetResult();
|
SetResult();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@this.Unloaded += OnUnloaded;
|
dialog.Unloaded += OnUnloaded;
|
||||||
@this.GetViewModel<IDialogResultable>().ClosureToken = @this.Close;
|
dialog.GetViewModel<IDialogResultable<TResult>>().CloseAction = dialog.Close;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
@@ -40,7 +40,7 @@ namespace HandyControl.Tools.Extension
|
|||||||
// ReSharper disable once ImplicitlyCapturedClosure
|
// ReSharper disable once ImplicitlyCapturedClosure
|
||||||
void OnUnloaded(object sender, RoutedEventArgs args)
|
void OnUnloaded(object sender, RoutedEventArgs args)
|
||||||
{
|
{
|
||||||
@this.Unloaded -= OnUnloaded;
|
dialog.Unloaded -= OnUnloaded;
|
||||||
SetResult();
|
SetResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,7 +48,7 @@ namespace HandyControl.Tools.Extension
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
tcs.TrySetResult(ExtractResult<TResult>(@this));
|
tcs.TrySetResult(dialog.GetViewModel<IDialogResultable<TResult>>().Result);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
@@ -57,16 +57,16 @@ namespace HandyControl.Tools.Extension
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Dialog Initialize<TViewModel>(this Dialog @this, Action<TViewModel> configure)
|
public static Dialog Initialize<TViewModel>(this Dialog dialog, Action<TViewModel> configure)
|
||||||
{
|
{
|
||||||
configure?.Invoke(@this.GetViewModel<TViewModel>());
|
configure?.Invoke(dialog.GetViewModel<TViewModel>());
|
||||||
|
|
||||||
return @this;
|
return dialog;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TViewModel GetViewModel<TViewModel>(this Dialog @this)
|
public static TViewModel GetViewModel<TViewModel>(this Dialog dialog)
|
||||||
{
|
{
|
||||||
if (!(@this.Content is FrameworkElement frameworkElement))
|
if (!(dialog.Content is FrameworkElement frameworkElement))
|
||||||
throw new InvalidOperationException("The dialog is not a derived class of the FrameworkElement. ");
|
throw new InvalidOperationException("The dialog is not a derived class of the FrameworkElement. ");
|
||||||
|
|
||||||
if (!(frameworkElement.DataContext is TViewModel viewModel))
|
if (!(frameworkElement.DataContext is TViewModel viewModel))
|
||||||
@@ -74,22 +74,5 @@ namespace HandyControl.Tools.Extension
|
|||||||
|
|
||||||
return viewModel;
|
return viewModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static TResult ExtractResult<TResult>(Dialog dialog)
|
|
||||||
{
|
|
||||||
var viewModelResult = dialog.GetViewModel<IDialogResultable>().Result;
|
|
||||||
|
|
||||||
switch (viewModelResult)
|
|
||||||
{
|
|
||||||
case TResult result:
|
|
||||||
return result;
|
|
||||||
case null when !typeof(TResult).IsValueType:
|
|
||||||
return default(TResult); // The default value of the value type may cause confusion, so should not help the user decide.
|
|
||||||
default:
|
|
||||||
throw new InvalidCastException("Could not cast " +
|
|
||||||
$"the {(viewModelResult != null ? $"{viewModelResult.GetType()} type" : "null value")} " +
|
|
||||||
$"to the {typeof(TResult)} type. ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,31 +3,14 @@
|
|||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:langs="clr-namespace:HandyControlDemo.Properties.Langs"
|
xmlns:langs="clr-namespace:HandyControlDemo.Properties.Langs"
|
||||||
xmlns:controls="clr-namespace:HandyControl.Controls;assembly=HandyControl"
|
xmlns:controls="clr-namespace:HandyControl.Controls;assembly=HandyControl"
|
||||||
xmlns:interactivity="clr-namespace:HandyControl.Interactivity;assembly=HandyControl"
|
|
||||||
CornerRadius="10"
|
CornerRadius="10"
|
||||||
Width="400"
|
Width="400"
|
||||||
Height="247"
|
Height="247"
|
||||||
DataContext="{Binding Path=InteractiveDialog, Source={StaticResource Locator}}"
|
DataContext="{Binding InteractiveDialog, Source={StaticResource Locator}}"
|
||||||
Background="{DynamicResource RegionBrush}">
|
Background="{DynamicResource RegionBrush}">
|
||||||
<controls:SimplePanel>
|
<StackPanel VerticalAlignment="Center" Margin="32">
|
||||||
<TextBlock Style="{StaticResource TextBlockLargeBold}"
|
<TextBlock Style="{StaticResource TextBlockLargeBold}" Text="{Binding Message}"/>
|
||||||
Text="{Binding Message}"
|
<TextBox Margin="0,32,0,0" Style="{StaticResource TextBoxExtend}" Text="{Binding Result,UpdateSourceTrigger=PropertyChanged}" controls:InfoElement.Placeholder="{x:Static langs:Lang.PleaseInput}"/>
|
||||||
Margin="30"
|
<Button Width="120" Margin="0,32,0,0" Command="{Binding CloseCmd}" Style="{StaticResource ButtonPrimary}" Content="{x:Static langs:Lang.Ok}"/>
|
||||||
VerticalAlignment="Top" />
|
</StackPanel>
|
||||||
|
|
||||||
<TextBox x:Name="TextBox"
|
|
||||||
Style="{StaticResource TextBoxBaseStyle}"
|
|
||||||
Text="{x:Static langs:Lang.PleaseInput}"
|
|
||||||
Margin="70"
|
|
||||||
VerticalAlignment="Top" />
|
|
||||||
<Button Width="120"
|
|
||||||
Height="32"
|
|
||||||
Command="{Binding CloseCommand}"
|
|
||||||
CommandParameter="{Binding Path=Text, ElementName=TextBox}"
|
|
||||||
Style="{StaticResource ButtonPrimary}"
|
|
||||||
Content="{x:Static langs:Lang.Ok}"
|
|
||||||
HorizontalAlignment="Center"
|
|
||||||
VerticalAlignment="Bottom"
|
|
||||||
Margin="0 0 0 30" />
|
|
||||||
</controls:SimplePanel>
|
|
||||||
</Border>
|
</Border>
|
||||||
@@ -1,8 +1,5 @@
|
|||||||
namespace HandyControlDemo.UserControl.Basic
|
namespace HandyControlDemo.UserControl.Basic
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Interaction logic for InteractiveDialog.xaml
|
|
||||||
/// </summary>
|
|
||||||
public partial class InteractiveDialog
|
public partial class InteractiveDialog
|
||||||
{
|
{
|
||||||
public InteractiveDialog()
|
public InteractiveDialog()
|
||||||
|
|||||||
@@ -2,20 +2,15 @@
|
|||||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:langs="clr-namespace:HandyControlDemo.Properties.Langs"
|
xmlns:langs="clr-namespace:HandyControlDemo.Properties.Langs"
|
||||||
|
xmlns:controls="clr-namespace:HandyControl.Controls;assembly=HandyControl"
|
||||||
Background="{DynamicResource RegionBrush}"
|
Background="{DynamicResource RegionBrush}"
|
||||||
DataContext="{Binding DialogDemo,Source={StaticResource Locator}}"
|
MinWidth="300"
|
||||||
Width="300">
|
DataContext="{Binding DialogDemo,Source={StaticResource Locator}}">
|
||||||
<StackPanel>
|
<controls:TransitioningContentControl>
|
||||||
<Button Margin="32"
|
<StackPanel Margin="32" VerticalAlignment="Center">
|
||||||
Content="{x:Static langs:Lang.TextDialog}"
|
<Button Content="{x:Static langs:Lang.TextDialog}" Command="{Binding ShowTextCmd}" />
|
||||||
Command="{Binding ShowTextCmd}" />
|
<TextBox Margin="0,32,0,0" Text="{Binding DialogResult,UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource TextBoxExtend}" controls:InfoElement.Placeholder="{x:Static langs:Lang.PleaseInput}"/>
|
||||||
<TextBox x:Name="TextBox"
|
<Button Margin="0,10,0,0" Content="{x:Static langs:Lang.InteractiveDialog}" Command="{Binding ShowInteractiveDialogCmd}"/>
|
||||||
Style="{StaticResource TextBoxBaseStyle}"
|
</StackPanel>
|
||||||
Text="{Binding DialogResult}"
|
</controls:TransitioningContentControl>
|
||||||
Margin="20 0"/>
|
|
||||||
<Button Margin="32"
|
|
||||||
Content="{x:Static langs:Lang.InteractiveDialog}"
|
|
||||||
Command="{Binding ShowInteractiveDialogCmd}"
|
|
||||||
CommandParameter="{Binding Path=Text, ElementName=TextBox}"/>
|
|
||||||
</StackPanel>
|
|
||||||
</UserControl>
|
</UserControl>
|
||||||
@@ -1,36 +1,30 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Windows.Input;
|
|
||||||
using GalaSoft.MvvmLight;
|
using GalaSoft.MvvmLight;
|
||||||
using GalaSoft.MvvmLight.CommandWpf;
|
using GalaSoft.MvvmLight.CommandWpf;
|
||||||
using HandyControl.Tools.Extension;
|
using HandyControl.Tools.Extension;
|
||||||
|
|
||||||
namespace HandyControlDemo.ViewModel.Basic
|
namespace HandyControlDemo.ViewModel.Basic
|
||||||
{
|
{
|
||||||
public class InteractiveDialogViewModel : ViewModelBase, IDialogResultable
|
public class InteractiveDialogViewModel : ViewModelBase, IDialogResultable<string>
|
||||||
{
|
{
|
||||||
|
public Action CloseAction { get; set; }
|
||||||
|
|
||||||
|
private string _result;
|
||||||
|
|
||||||
|
public string Result
|
||||||
|
{
|
||||||
|
get => _result;
|
||||||
|
set => Set(ref _result, value);
|
||||||
|
}
|
||||||
|
|
||||||
private string _message;
|
private string _message;
|
||||||
|
|
||||||
|
|
||||||
public object Result { get; set; }
|
|
||||||
|
|
||||||
public Action ClosureToken { get; set; }
|
|
||||||
|
|
||||||
public ICommand CloseCommand { get; }
|
|
||||||
|
|
||||||
public string Message
|
public string Message
|
||||||
{
|
{
|
||||||
get => _message;
|
get => _message;
|
||||||
set => Set(ref _message, value);
|
set => Set(ref _message, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public RelayCommand CloseCmd => new Lazy<RelayCommand>(() => new RelayCommand(() => CloseAction?.Invoke())).Value;
|
||||||
public InteractiveDialogViewModel()
|
|
||||||
{
|
|
||||||
CloseCommand = new RelayCommand<string>(result =>
|
|
||||||
{
|
|
||||||
Result = result;
|
|
||||||
ClosureToken();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,9 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Windows.Input;
|
using System.Threading.Tasks;
|
||||||
using GalaSoft.MvvmLight;
|
using GalaSoft.MvvmLight;
|
||||||
using GalaSoft.MvvmLight.CommandWpf;
|
using GalaSoft.MvvmLight.CommandWpf;
|
||||||
using HandyControl.Controls;
|
using HandyControl.Controls;
|
||||||
using HandyControl.Tools.Extension;
|
using HandyControl.Tools.Extension;
|
||||||
using HandyControlDemo.Properties.Langs;
|
|
||||||
using HandyControlDemo.UserControl;
|
using HandyControlDemo.UserControl;
|
||||||
using HandyControlDemo.UserControl.Basic;
|
using HandyControlDemo.UserControl.Basic;
|
||||||
using HandyControlDemo.ViewModel.Basic;
|
using HandyControlDemo.ViewModel.Basic;
|
||||||
@@ -15,32 +14,28 @@ namespace HandyControlDemo.ViewModel
|
|||||||
{
|
{
|
||||||
private string _dialogResult;
|
private string _dialogResult;
|
||||||
|
|
||||||
public RelayCommand ShowTextCmd => new Lazy<RelayCommand>(() =>
|
|
||||||
new RelayCommand(ShowText)).Value;
|
|
||||||
|
|
||||||
public string DialogResult
|
public string DialogResult
|
||||||
{
|
{
|
||||||
get => _dialogResult;
|
get => _dialogResult;
|
||||||
set => Set(ref _dialogResult, value);
|
set => Set(ref _dialogResult, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ICommand ShowInteractiveDialogCmd { get; }
|
public RelayCommand ShowTextCmd => new Lazy<RelayCommand>(() =>
|
||||||
|
new RelayCommand(ShowText)).Value;
|
||||||
public DialogDemoViewModel()
|
|
||||||
{
|
|
||||||
DialogResult = Lang.PleaseInput;
|
|
||||||
|
|
||||||
ShowInteractiveDialogCmd = new RelayCommand<string>(async message =>
|
|
||||||
{
|
|
||||||
DialogResult = await Dialog.Show<InteractiveDialog>()
|
|
||||||
.Initialize<InteractiveDialogViewModel>(vm => vm.Message = message)
|
|
||||||
.GetResultAsync<string>();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ShowText()
|
private void ShowText()
|
||||||
{
|
{
|
||||||
Dialog.Show(new TextDialog());
|
Dialog.Show(new TextDialog());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public RelayCommand ShowInteractiveDialogCmd => new Lazy<RelayCommand>(() =>
|
||||||
|
new RelayCommand(async () => await ShowInteractiveDialog())).Value;
|
||||||
|
|
||||||
|
private async Task ShowInteractiveDialog()
|
||||||
|
{
|
||||||
|
DialogResult = await Dialog.Show<InteractiveDialog>()
|
||||||
|
.Initialize<InteractiveDialogViewModel>(vm => vm.Message = DialogResult)
|
||||||
|
.GetResultAsync<string>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Reflection.Emit;
|
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using CommonServiceLocator;
|
using CommonServiceLocator;
|
||||||
using GalaSoft.MvvmLight.Ioc;
|
using GalaSoft.MvvmLight.Ioc;
|
||||||
|
|||||||
Reference in new issue
Block a user