Mesen2/UI/Windows/UpdatePromptWindow.axaml.cs
Sour 716227c0a7 UI: Update to Avalonia11-preview6
Notes:
-Uses a copy of VirtualizingStackPanel (to include fixes done after preview6) + fix another issue in lists
-Fixes memory leaks when closing windows (by setting DataContext to null, etc.)
2023-04-10 22:59:48 -04:00

77 lines
1.5 KiB
C#

using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Threading;
using Mesen.Utilities;
using Mesen.ViewModels;
using System;
using System.ComponentModel;
using System.Threading.Tasks;
namespace Mesen.Windows
{
public partial class UpdatePromptWindow : MesenWindow
{
private UpdatePromptViewModel _model;
[Obsolete("For designer only")]
public UpdatePromptWindow() : this(new(new())) { }
public UpdatePromptWindow(UpdatePromptViewModel model)
{
_model = model;
DataContext = model;
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
protected override void OnClosing(WindowClosingEventArgs e)
{
base.OnClosing(e);
if(_model.IsUpdating) {
e.Cancel = true;
}
}
private void OnUpdateClick(object sender, RoutedEventArgs e)
{
_model.Progress = 0;
_model.IsUpdating = true;
Task.Run(async () => {
bool result;
try {
result = await _model.UpdateMesen();
} catch(Exception ex) {
result = false;
Dispatcher.UIThread.Post(() => {
_model.IsUpdating = false;
MesenMsgBox.ShowException(ex);
});
return;
}
Dispatcher.UIThread.Post(() => {
_model.IsUpdating = false;
if(result) {
Close(true);
}
});
});
}
private void OnCancelClick(object sender, RoutedEventArgs e)
{
Close(false);
}
}
}