mirror of
https://github.com/PacktPublishing/Learn-WinUI-3-Second-Edition.git
synced 2026-06-20 12:23:09 +00:00
Update completed solution for chapter 8
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user