Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Popup弹窗方式修改 #182

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Exmaple2.0/Exmaple2.0.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
</PropertyGroup>

<ItemGroup>
<Folder Include="Models\" />
<AvaloniaResource Include="Assets\**" />
</ItemGroup>

<ItemGroup>
<AvaloniaXaml Remove="Models\**" />
<Compile Remove="Models\**" />
<EmbeddedResource Remove="Models\**" />
<None Remove="Models\**" />
</ItemGroup>

<ItemGroup>
<ProjectCapability Include="Avalonia" />
<TrimmerRootAssembly Include="Avalonia.Themes.Fluent" />
Expand Down
19 changes: 11 additions & 8 deletions Exmaple2.0/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@
<Setter Property="Height" Value="100" />
</Style>
</Window.Styles>
<ScrollViewer>
<StackPanel>
<Button Name="Standard_Show" Classes="View" Click="Standard_Show_OnClick" Content="Standard Show"/>
<Button Name="Standard_Dialog" Classes="View" Click="Standard_Dialog_OnClick" Content="Standard Dialog"/>
<Button Name="Standard_Popup" Classes="View" Click="Standard_Popup_OnClick" Content="Custom PopUp"/>
<Button Name="Custom_Show" Classes="View" Click="Custom_Dialog_OnClick" Content="Custom Show"/>
<Button Name="Custom_MarkDown" Classes="View" Click="Custom_MarkDown_OnClick" Content="Custom Markdown"/>
<Button Name="Custom_Dialog" Classes="View" Click="Custom_Dialog_OnClick" Content="Custom dialog with image"/>
<Button Name="Custom_PopUp" Classes="View" Click="Custom_PopUp_OnClick" Content="Custom popup"/>
</StackPanel>
<Button Name="Standard_Show" Classes="View" Click="Standard_Show_OnClick" Content="Standard Show"/>
<Button Name="Standard_Dialog" Classes="View" Click="Standard_Dialog_OnClick" Content="Standard Dialog"/>
<Button Name="Standard_Popup" Classes="View" Click="Standard_Popup_OnClick" Content="Custom PopUp"/>
<Button Name="Custom_Show" Classes="View" Click="Custom_Dialog_OnClick" Content="Custom Show"/>
<Button Name="Custom_MarkDown" Classes="View" Click="Custom_MarkDown_OnClick" Content="Custom Markdown"/>
<Button Name="Custom_Dialog" Classes="View" Click="Custom_Dialog_OnClick" Content="Custom dialog with image"/>
<Button Name="Custom_PopUp" Classes="View" Click="Custom_PopUp_OnClick" Content="Custom popup"/>

<Button Name="Custom_CloseClickAway" Classes="View" Click="Custom_CloseClickAway_OnClick" Content="Custom CloseClickAway"/>
</StackPanel>
</ScrollViewer>
</Window>
39 changes: 39 additions & 0 deletions Exmaple2.0/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,43 @@ private void Custom_MarkDown_OnClick(object sender, RoutedEventArgs e)
{
throw new System.NotImplementedException();
}

private async void Custom_CloseClickAway_OnClick(object sender, RoutedEventArgs e) {
var result =await MessageBoxManager.GetMessageBoxCustom(
new MessageBoxCustomParams {
ButtonDefinitions = new List<ButtonDefinition>{
new ButtonDefinition { Name = "Yes", },
new ButtonDefinition { Name = "No", },
new ButtonDefinition { Name = "Cancel", }
},
ContentTitle = "title",
ContentMessage = "Informative note:" +
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc ut pulvinar est, eget porttitor magna. Maecenas nunc elit, pretium nec mauris vel, cursus faucibus leo. Mauris consequat magna vel mi malesuada semper. Donec nunc justo, rhoncus vel viverra a, ultrices vel nibh. Praesent ut libero a nunc placerat vulputate. Morbi ullamcorper pharetra lectus, ut lobortis ex consequat sit amet. Vestibulum pellentesque quam at justo hendrerit, et tincidunt nisl mattis. Curabitur eu nibh enim.\n",
Icon = MsBox.Avalonia.Enums.Icon.Question,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
CanResize = false,
MaxWidth = 500,
MaxHeight = 800,
SizeToContent = SizeToContent.WidthAndHeight,
ShowInCenter = true,
Topmost = false,
}).ShowAsPopupAsync(this);
await MessageBoxManager.GetMessageBoxCustom(
new MessageBoxCustomParams {
ButtonDefinitions = new List<ButtonDefinition>{
new ButtonDefinition { Name = "OK", }
},
ContentTitle = "Result",
ContentMessage = "You Selected:"+ result,
Icon = MsBox.Avalonia.Enums.Icon.Question,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
CanResize = false,
MaxWidth = 500,
MaxHeight = 800,
SizeToContent = SizeToContent.WidthAndHeight,
ShowInCenter = true,
Topmost = false,
CloseOnClickAway=true
}).ShowAsPopupAsync(this);
}
}
5 changes: 5 additions & 0 deletions MsBox.Avalonia/Dto/AbstractMessageBoxParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public abstract class AbstractMessageBoxParams
/// Input
/// </summary>
public InputParams InputParams { get; set; }

/// <summary>
/// param to set closeOnClickAway
/// </summary>
public bool CloseOnClickAway { get; set; } = false;
}

public class HyperLinkParams
Expand Down
20 changes: 18 additions & 2 deletions MsBox.Avalonia/MsBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Avalonia.Controls.ApplicationLifetimes;
using DialogHostAvalonia;
using MsBox.Avalonia.Base;
using MsBox.Avalonia.ViewModels;
using MsBox.Avalonia.Windows;

namespace MsBox.Avalonia;
Expand Down Expand Up @@ -95,6 +96,7 @@ public Task<T> ShowWindowDialogAsync(Window owner)
return tcs.Task;
}

private readonly string ClickAwayParam = "MsBoxIdentifier_Cancel";
/// <summary>
/// Show messagebox as popup
/// </summary>
Expand All @@ -116,20 +118,34 @@ public Task<T> ShowAsPopupAsync(ContentControl owner)
_viewModel.SetFullApi(_view);
owner.Content = null;
dh.Content = parentContent;

dh.CloseOnClickAway = false;
if (_viewModel is AbstractMsBoxViewModel abv) dh.CloseOnClickAway = abv.CloseOnClickAway;
dh.CloseOnClickAwayParameter = ClickAwayParam;
dh.DialogClosing += (ss, ee) => {
if (ee.Parameter?.ToString() == ClickAwayParam) {
_view.Close();
}
};

owner.Content = dh;
var tcs = new TaskCompletionSource<T>();
_view.SetCloseAction(() =>
{
tcs.TrySetResult(_view.GetButtonResult());
DialogHost.Close(dh.Identifier);
var r = _view.GetButtonResult();

if (dh.CurrentSession != null && dh.CurrentSession.IsEnded == false) {
DialogHost.Close(dh.Identifier);
}

owner.Content = null;
dh.Content = null;
owner.Content = parentContent;
if (style != null)
{
owner.Styles.Remove(style);
}
tcs.TrySetResult(r);
});
DialogHost.Show(_view, dh.Identifier);
return tcs.Task;
Expand Down
2 changes: 2 additions & 0 deletions MsBox.Avalonia/ViewModels/AbstractMsBoxViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ protected AbstractMsBoxViewModel(AbstractMessageBoxParams @params, Icon icon = I
LocationOfMyWindow = @params.WindowStartupLocation;
SystemDecorations = @params.SystemDecorations;
Topmost = @params.Topmost;
CloseOnClickAway = @params.CloseOnClickAway;

if (@params.HyperLinkParams != null)
{
Expand Down Expand Up @@ -95,6 +96,7 @@ protected AbstractMsBoxViewModel(AbstractMessageBoxParams @params, Icon icon = I
public WindowStartupLocation LocationOfMyWindow { get; }

public event PropertyChangedEventHandler PropertyChanged;
public bool CloseOnClickAway { get; private set; }

#region Hyperlink properties
public abstract RelayCommand HyperLinkCommand { get; internal set; }
Expand Down