Update completed solution for chapter 8

This commit is contained in:
Alvin Ashcraft
2023-07-29 14:58:47 -04:00
parent 21e22a0369
commit 4c9e111e61
10 changed files with 387 additions and 7 deletions
@@ -14,6 +14,7 @@
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel>
@@ -69,6 +70,12 @@
Margin="4,0">
<StackPanel HorizontalAlignment="Right"
Orientation="Horizontal">
<Button Command="{x:Bind ViewModel.SendToastCommand}"
Content="Send Notification"
Margin="8,8,0,8"/>
<Button Command="{x:Bind ViewModel.SendToastWithTextCommand}"
Content="Send Notification with Text"
Margin="8,8,0,8"/>
<Button Command="{x:Bind ViewModel.AddEditCommand}"
Content="Add/Edit Item"
Margin="8,8,0,8"/>
@@ -77,5 +84,6 @@
Margin="8"/>
</StackPanel>
</Border>
<InfoBar x:Name="notifyInfoBar" Grid.Row="3"/>
</Grid>
</Page>
@@ -2,6 +2,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using MyMediaCollection.ViewModels;
using MyMediaCollection.Helpers;
namespace MyMediaCollection.Views
{
@@ -10,10 +11,13 @@ namespace MyMediaCollection.Views
/// </summary>
public sealed partial class MainPage : Page
{
public static MainPage Current;
public MainPage()
{
ViewModel = App.HostContainer.Services.GetService<MainViewModel>();
this.InitializeComponent();
Current = this;
Loaded += MainPage_Loaded;
}
@@ -27,5 +31,63 @@ namespace MyMediaCollection.Views
}
public MainViewModel ViewModel;
public void NotifyUser(string message, InfoBarSeverity severity, bool isOpen = true)
{
if (DispatcherQueue.HasThreadAccess)
{
UpdateStatus(message, severity, isOpen);
}
else
{
DispatcherQueue.TryEnqueue(() =>
{
UpdateStatus(message, severity, isOpen);
});
}
}
private void UpdateStatus(string message, InfoBarSeverity severity, bool isOpen)
{
notifyInfoBar.Message = message;
notifyInfoBar.IsOpen = isOpen;
notifyInfoBar.Severity = severity;
}
public void NotificationReceived(NotificationShared.Notification notification)
{
var text = $"{notification.Originator}; Action: {notification.Action}";
if (notification.HasInput)
{
if (string.IsNullOrWhiteSpace(notification.Input))
text += "; No input received";
else
text += $"; Input received: {notification.Input}";
}
if (DispatcherQueue.HasThreadAccess)
DisplayMessageDialog(text);
else
{
DispatcherQueue.TryEnqueue(() =>
{
DisplayMessageDialog(text);
});
}
}
private void DisplayMessageDialog(string message)
{
ContentDialog notifyDialog = new()
{
XamlRoot = this.XamlRoot,
Title = "Notification received",
Content = message,
CloseButtonText = "Ok"
};
notifyDialog.ShowAsync();
}
}
}