-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Implement Alerts (Alert, Prompt and ActionSheet) #1328
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
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
81de2e8
Implement Alerts in Maui Core
jsuarezruiz 1b2cb16
Updated Windows implementation
jsuarezruiz 35e419b
Updated sample
jsuarezruiz de1c912
Updated sample
jsuarezruiz 093028b
Updated Page
jsuarezruiz da3f435
Merge branch 'main' into alerts
jsuarezruiz de5c03b
Fix broken unit tests
jsuarezruiz 18eeb5f
Merge main branch
jsuarezruiz fb54376
Merge branch 'main' into alerts
jsuarezruiz cb41bec
Fixed build error
jsuarezruiz 7a15773
Fixed misaligned code issue
jsuarezruiz e97e9cb
Merge branch 'main' into alerts
jsuarezruiz 07808be
Added Alerts sample
jsuarezruiz 9d19d55
Moved Alerts to controls
jsuarezruiz 9d63c2f
Removed unnecessary changes
jsuarezruiz 71efc0a
Removed more unnecessary changes
jsuarezruiz c355bbc
Clean code
jsuarezruiz 1e348b7
Merge branch 'main' into alerts
jsuarezruiz a3972dd
Merge branch 'main' into alerts
jsuarezruiz 214e4a0
Fix build errors
jsuarezruiz 7c0c0b9
Fix WinUI Build errors
jsuarezruiz ed9f035
Removed unnecesary #if in AlertManager
jsuarezruiz 4f119ee
Remove more compilation directives
jsuarezruiz 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
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/Controls/samples/Controls.Sample/Pages/Core/AlertsPage.xaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<views:BasePage | ||
xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
x:Class="Maui.Controls.Sample.Pages.AlertsPage" | ||
xmlns:views="clr-namespace:Maui.Controls.Sample.Pages.Base" | ||
Title="Alerts"> | ||
<views:BasePage.Content> | ||
<StackLayout | ||
Margin="12"> | ||
<Label | ||
Text="Display Alert" | ||
Style="{StaticResource Headline}"/> | ||
<Button | ||
Text="Alert Simple" | ||
Clicked="OnAlertSimpleClicked" /> | ||
<Button | ||
Text="Alert Yes/No" | ||
Clicked="OnAlertYesNoClicked" /> | ||
<Label | ||
Text="Display ActionSheet" | ||
Style="{StaticResource Headline}"/> | ||
<Button | ||
Text="ActionSheet Simple" | ||
Clicked="OnActionSheetSimpleClicked" /> | ||
<Button | ||
Text="ActionSheet Cancel/Delete" | ||
Clicked="OnActionSheetCancelDeleteClicked" /> | ||
<Label | ||
Text="Display Prompt" | ||
Style="{StaticResource Headline}"/> | ||
<Button | ||
Text="Question 1" | ||
Clicked="OnQuestion1ButtonClicked" /> | ||
<Label | ||
x:Name="question1ResultLabel" /> | ||
<Button | ||
Text="Question 2" | ||
Clicked="OnQuestion2ButtonClicked" /> | ||
<Label | ||
x:Name="question2ResultLabel" /> | ||
</StackLayout> | ||
</views:BasePage.Content> | ||
</views:BasePage> |
58 changes: 58 additions & 0 deletions
58
src/Controls/samples/Controls.Sample/Pages/Core/AlertsPage.xaml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using Microsoft.Maui; | ||
|
||
namespace Maui.Controls.Sample.Pages | ||
{ | ||
public partial class AlertsPage | ||
{ | ||
public AlertsPage() | ||
{ | ||
InitializeComponent(); | ||
} | ||
|
||
async void OnAlertSimpleClicked(object sender, EventArgs e) | ||
{ | ||
await DisplayAlert("Alert", "You have been alerted", "OK"); | ||
} | ||
|
||
async void OnAlertYesNoClicked(object sender, EventArgs e) | ||
{ | ||
var answer = await DisplayAlert("Question?", "Would you like to play a game", "Yes", "No"); | ||
Debug.WriteLine("Answer: " + answer); | ||
} | ||
|
||
async void OnActionSheetSimpleClicked(object sender, EventArgs e) | ||
{ | ||
var action = await DisplayActionSheet("ActionSheet: Send to?", "Cancel", null, "Email", "Twitter", "Facebook"); | ||
Debug.WriteLine("Action: " + action); | ||
} | ||
|
||
async void OnActionSheetCancelDeleteClicked(object sender, EventArgs e) | ||
{ | ||
var action = await DisplayActionSheet("ActionSheet: SavePhoto?", "Cancel", "Delete", "Photo Roll", "Email"); | ||
Debug.WriteLine("Action: " + action); | ||
} | ||
|
||
async void OnQuestion1ButtonClicked(object sender, EventArgs e) | ||
{ | ||
string result = await DisplayPromptAsync("Question 1", "What's your name?", initialValue: string.Empty); | ||
|
||
if (!string.IsNullOrWhiteSpace(result)) | ||
{ | ||
question1ResultLabel.Text = $"Hello {result}."; | ||
} | ||
} | ||
|
||
async void OnQuestion2ButtonClicked(object sender, EventArgs e) | ||
{ | ||
string result = await DisplayPromptAsync("Question 2", "What's 5 + 5?", initialValue: "10", maxLength: 2, keyboard: Keyboard.Numeric); | ||
|
||
if (!string.IsNullOrWhiteSpace(result)) | ||
{ | ||
int number = Convert.ToInt32(result); | ||
question2ResultLabel.Text = number == 10 ? "Correct." : "Incorrect."; | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
src/Controls/src/Core/Platform/AlertManager/ActionSheetDialog.Windows.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
#nullable disable | ||
using System; | ||
using System.Linq; | ||
using Microsoft.Maui.Controls.Internals; | ||
using Microsoft.UI.Xaml; | ||
using Microsoft.UI.Xaml.Controls; | ||
|
||
namespace Microsoft.Maui.Controls.Platform | ||
{ | ||
public sealed class ActionSheetContent : UserControl | ||
{ | ||
readonly ActionSheetArguments _options; | ||
|
||
public event EventHandler OptionSelected; | ||
|
||
public ActionSheetContent(ActionSheetArguments sheetOptions) | ||
{ | ||
Initialize(); | ||
|
||
_options = sheetOptions; | ||
|
||
TitleBlock.Text = _options.Title ?? string.Empty; | ||
OptionsList.ItemsSource = _options.Buttons.ToList(); | ||
|
||
if (_options.FlowDirection == Controls.FlowDirection.RightToLeft) | ||
{ | ||
TitleBlock.FlowDirection = UI.Xaml.FlowDirection.RightToLeft; | ||
OptionsList.FlowDirection = UI.Xaml.FlowDirection.RightToLeft; | ||
} | ||
else if (_options.FlowDirection == Controls.FlowDirection.LeftToRight) | ||
{ | ||
TitleBlock.FlowDirection = UI.Xaml.FlowDirection.LeftToRight; | ||
OptionsList.FlowDirection = UI.Xaml.FlowDirection.LeftToRight; | ||
} | ||
|
||
if (_options.FlowDirection == Controls.FlowDirection.RightToLeft) | ||
{ | ||
if (_options.Cancel != null) | ||
{ | ||
LeftBtn.Content = _options.Cancel; | ||
if (_options.Destruction != null) | ||
RightBtn.Content = _options.Destruction; | ||
} | ||
else if (_options.Destruction != null) | ||
LeftBtn.Content = _options.Destruction; | ||
} | ||
else | ||
{ | ||
if (_options.Cancel != null) | ||
{ | ||
RightBtn.Content = _options.Cancel; | ||
if (_options.Destruction != null) | ||
LeftBtn.Content = _options.Destruction; | ||
} | ||
else if (_options.Destruction != null) | ||
RightBtn.Content = _options.Destruction; | ||
} | ||
|
||
LeftBtn.Visibility = LeftBtn.Content == null ? UI.Xaml.Visibility.Collapsed : UI.Xaml.Visibility.Visible; | ||
RightBtn.Visibility = RightBtn.Content == null ? UI.Xaml.Visibility.Collapsed : UI.Xaml.Visibility.Visible; | ||
} | ||
|
||
internal TextBlock TitleBlock { get; private set; } | ||
|
||
internal UI.Xaml.Controls.ListView OptionsList { get; private set; } | ||
|
||
internal UI.Xaml.Controls.Button LeftBtn { get; private set; } | ||
|
||
internal UI.Xaml.Controls.Button RightBtn { get; private set; } | ||
|
||
void Initialize() | ||
{ | ||
var mainLayout = new UI.Xaml.Controls.Grid | ||
{ | ||
Padding = new UI.Xaml.Thickness(10), | ||
RowDefinitions = | ||
{ | ||
new UI.Xaml.Controls.RowDefinition { Height = new UI.Xaml.GridLength(0, UI.Xaml.GridUnitType.Star) }, | ||
new UI.Xaml.Controls.RowDefinition { Height = new UI.Xaml.GridLength(0, UI.Xaml.GridUnitType.Auto) } | ||
} | ||
}; | ||
|
||
var firstLayout = new UI.Xaml.Controls.Grid | ||
{ | ||
RowDefinitions = | ||
{ | ||
new UI.Xaml.Controls.RowDefinition { Height = new UI.Xaml.GridLength(0, UI.Xaml.GridUnitType.Auto) }, | ||
new UI.Xaml.Controls.RowDefinition { Height = new UI.Xaml.GridLength(0, UI.Xaml.GridUnitType.Star) } | ||
} | ||
}; | ||
|
||
TitleBlock = new TextBlock { FontSize = 18, MaxLines = 2 }; | ||
firstLayout.Children.Add(TitleBlock); | ||
UI.Xaml.Controls.Grid.SetRow(TitleBlock, 0); | ||
|
||
OptionsList = new UI.Xaml.Controls.ListView { IsItemClickEnabled = true, Margin = new UI.Xaml.Thickness(0, 10, 0, 10), SelectionMode = UI.Xaml.Controls.ListViewSelectionMode.None }; | ||
OptionsList.ItemClick += ListItemSelected; | ||
firstLayout.Children.Add(OptionsList); | ||
UI.Xaml.Controls.Grid.SetRow(OptionsList, 1); | ||
|
||
var secondLayout = new UI.Xaml.Controls.Grid | ||
{ | ||
ColumnDefinitions = | ||
{ | ||
new UI.Xaml.Controls.ColumnDefinition { Width = new UI.Xaml.GridLength(0, UI.Xaml.GridUnitType.Star) }, | ||
new UI.Xaml.Controls.ColumnDefinition { Width = new UI.Xaml.GridLength(0, UI.Xaml.GridUnitType.Star) } | ||
}, | ||
VerticalAlignment = VerticalAlignment.Bottom | ||
}; | ||
|
||
LeftBtn = new UI.Xaml.Controls.Button { Height = 32, HorizontalAlignment = HorizontalAlignment.Stretch, Margin = new UI.Xaml.Thickness(0, 0, 5, 0) }; | ||
LeftBtn.Click += ActionButtonClicked; | ||
secondLayout.Children.Add(LeftBtn); | ||
UI.Xaml.Controls.Grid.SetColumn(LeftBtn, 0); | ||
|
||
RightBtn = new UI.Xaml.Controls.Button { Height = 32, HorizontalAlignment = HorizontalAlignment.Stretch, Margin = new UI.Xaml.Thickness(5, 0, 0, 0) }; | ||
RightBtn.Click += ActionButtonClicked; | ||
secondLayout.Children.Add(RightBtn); | ||
UI.Xaml.Controls.Grid.SetColumn(RightBtn, 1); | ||
|
||
mainLayout.Children.Add(firstLayout); | ||
UI.Xaml.Controls.Grid.SetRow(firstLayout, 0); | ||
|
||
mainLayout.Children.Add(secondLayout); | ||
UI.Xaml.Controls.Grid.SetRow(secondLayout, 1); | ||
|
||
Content = mainLayout; | ||
} | ||
|
||
void ListItemSelected(object sender, ItemClickEventArgs e) | ||
{ | ||
var selection = (string)e.ClickedItem; | ||
_options.SetResult(selection); | ||
|
||
OptionSelected?.Invoke(this, null); | ||
} | ||
|
||
void ActionButtonClicked(object sender, RoutedEventArgs e) | ||
{ | ||
var button = (UI.Xaml.Controls.Button)sender; | ||
var selection = (string)button.Content; | ||
_options.SetResult(selection); | ||
|
||
OptionSelected?.Invoke(this, null); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.