Skip to content

Commit

Permalink
Merge pull request #194 from ProfessionalCSharp/193-winui-replace-mes…
Browse files Browse the repository at this point in the history
…sagedialog-with-contentdialog

193 winui replace messagedialog with contentdialog
  • Loading branch information
christiannagel authored Oct 16, 2023
2 parents 3502a35 + bc11adc commit 0cac349
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

using System.Runtime.InteropServices;
using System.Text;
using Windows.Storage;
Expand Down Expand Up @@ -61,8 +63,7 @@ public async void OnOpen()
}
catch (Exception ex)
{
MessageDialog dlg = new(ex.Message, "Error");
await dlg.ShowAsync();
await ShowErrorAsync(ex.Message);
}
}

Expand Down Expand Up @@ -91,8 +92,7 @@ public async void OnOpenDotnet()
}
catch (Exception ex)
{
MessageDialog dlg = new(ex.Message, "Error");
await dlg.ShowAsync();
await ShowErrorAsync(ex.Message);
}
}

Expand Down Expand Up @@ -125,8 +125,7 @@ public async void OnSaveDotnet()
}
catch (Exception ex)
{
MessageDialog dlg = new(ex.Message, "Error");
await dlg.ShowAsync();
await ShowErrorAsync(ex.Message);
}
}

Expand Down Expand Up @@ -157,8 +156,19 @@ public async void OnSave()
}
catch (Exception ex)
{
MessageDialog dlg = new(ex.Message, "Error");
await dlg.ShowAsync();
await ShowErrorAsync(ex.Message);
}
}

private async Task ShowErrorAsync(string message)
{
ContentDialog dlg = new()
{
Title = "Error",
Content = message,
PrimaryButtonText = "OK",
XamlRoot = Content.XamlRoot
};
await dlg.ShowAsync();
}
}
18 changes: 6 additions & 12 deletions 4_Apps/Patterns/BooksApp/BooksApp/Services/WinUIDialogService.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
using WinRT.Interop;

namespace BooksApp.Services;
namespace BooksApp.Services;

public class WinUIDialogService : IDialogService
{
public async Task ShowMessageAsync(string message)
{
MessageDialog dlg = new(message);
var hwnd = GetActiveWindow();
if (hwnd == IntPtr.Zero)
ContentDialog dlg = new()
{
throw new InvalidOperationException();
}
InitializeWithWindow.Initialize(dlg, hwnd);
Title = "Message",
Content = message,
PrimaryButtonText = "OK",
};
await dlg.ShowAsync();
}

[DllImport("user32.dll")]
private static extern IntPtr GetActiveWindow();
}
2 changes: 2 additions & 0 deletions 4_Apps/Patterns/BooksLib/ViewModels/BookDetailViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public override async Task OnSaveAsync()
{
if (EditItem is null) throw new InvalidOperationException();

await _dialogService.ShowMessageAsync("TEst save");

await _itemsService.AddOrUpdateAsync(EditItem);
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ public sealed partial class ButtonsPage : Page

private async void OnButtonClick(object sender, RoutedEventArgs e)
{
MessageDialog dlg = new("button 1 clicked");

IntPtr hwnd = WindowNative.GetWindowHandle(this);
InitializeWithWindow.Initialize(dlg, hwnd);
ContentDialog dlg = new()
{
Title = "Message",
Content = "button 1 clicked",
PrimaryButtonText = "OK",
XamlRoot = this.Content.XamlRoot
};
await dlg.ShowAsync();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,33 +98,33 @@ private async void OnDatesChanged(CalendarView sender, CalendarViewSelectedDates

string selectedDates = string.Join(", ", currentDatesSelected.Select(d => d.ToString("d")));

// await new MessageDialog($"dates selected: {selectedDates}").ShowAsync();
await ShowMessageAsync($"dates selected: {selectedDates}");
}

private async void OnDateChanged(CalendarDatePicker sender, CalendarDatePickerDateChangedEventArgs args)
{
// await new MessageDialog($"date changed to {args.NewDate}").ShowAsync();
await ShowMessageAsync($"date changed to: {args.NewDate}");
}

private async void OnDateChanged1(object sender, DatePickerValueChangedEventArgs e)
{
// await new MessageDialog($"date changed to {e.NewDate}").ShowAsync();
await ShowMessageAsync($"date changed to: {e.NewDate}");
}

private async void OnDatePicked(DatePickerFlyout sender, DatePickedEventArgs args)
{
// await new MessageDialog($"date changed to {args.NewDate}").ShowAsync();
await ShowMessageAsync($"date changed to: {args.NewDate}");
}

private async Task ShowMessageAsync(string message)
{
MessageDialog dlg = new(message);
IntPtr hwnd = WindowNative.GetWindowHandle(this);
InitializeWithWindow.Initialize(dlg, hwnd);
ContentDialog dlg = new()
{
Title = "Message",
Content = message,
PrimaryButtonText = "OK",
XamlRoot = this.XamlRoot
};
await dlg.ShowAsync();
}
}
21 changes: 15 additions & 6 deletions 4_Apps/Windows/XAMLIntro/Intro/Intro/Intro/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ public MainWindow()

button2.Click += async (sender, e) =>
{
MessageDialog dlg = new("button 2 clicked");
IntPtr hwnd = WindowNative.GetWindowHandle(this);
InitializeWithWindow.Initialize(dlg, hwnd);
ContentDialog dlg = new()
{
Title = "Message",
Content = "button 2 clicked",
PrimaryButtonText = "OK",
XamlRoot = this.Content.XamlRoot
};
await dlg.ShowAsync();
};

Expand All @@ -37,9 +42,13 @@ public MainWindow()

private async void OnButtonClick(object sender, RoutedEventArgs e)
{
MessageDialog dlg = new("button 1 clicked");
IntPtr hwnd = WindowNative.GetWindowHandle(this);
InitializeWithWindow.Initialize(dlg, hwnd);
ContentDialog dlg = new()
{
Title = "Message",
Content = "button 1 clicked",
PrimaryButtonText = "OK",
XamlRoot = this.Content.XamlRoot
};
await dlg.ShowAsync();
}
}
26 changes: 18 additions & 8 deletions 5_More/WinUI/WinUIAppEditor/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

using System.Runtime.InteropServices;
using System.Text;

using Windows.Security.ExchangeActiveSyncProvisioning;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
Expand Down Expand Up @@ -50,8 +52,7 @@ public async void OnOpen()
}
catch (Exception ex)
{
MessageDialog dlg = new(ex.Message, "Error");
await dlg.ShowAsync();
await ShowErrorAsync(ex.Message);
}
}

Expand Down Expand Up @@ -81,8 +82,7 @@ public async void OnOpenDotnet()
}
catch (Exception ex)
{
MessageDialog dlg = new(ex.Message, "Error");
await dlg.ShowAsync();
await ShowErrorAsync(ex.Message);
}
}

Expand Down Expand Up @@ -116,8 +116,7 @@ public async void OnSaveDotnet()
}
catch (Exception ex)
{
MessageDialog dlg = new(ex.Message, "Error");
await dlg.ShowAsync();
await ShowErrorAsync(ex.Message);
}
}

Expand Down Expand Up @@ -149,13 +148,24 @@ public async void OnSave()
}
catch (Exception ex)
{
MessageDialog dlg = new(ex.Message, "Error");
await dlg.ShowAsync();
await ShowErrorAsync(ex.Message);
}
}

public void OnClose()
{
App.Current.Exit();
}

private async Task ShowErrorAsync(string message)
{
ContentDialog dlg = new()
{
Title = "Error",
Content = message,
PrimaryButtonText = "OK",
XamlRoot = this.Content.XamlRoot
};
await dlg.ShowAsync();
}
}

0 comments on commit 0cac349

Please sign in to comment.