Files
velopack/samples/CSharpAvalonia/MainWindow.axaml.cs
Caelan 3128d34b14 Rework solution structure to allow more types of library projects (#196)
* Rename avalonia & wpf samples

* Rename rest of samples

* fix sample readme

* Fix compat util tests

* rename / move all src projects

* remove package lock files and move libC# again

* Add rust lib and cargo workspace

* Replace locksmith lib with new filelocksmith-rs library

* Deprecated type

* fix setup compile

* Use thiserror for error handling

* Rename some enums and formatting

* Add missing SHA256

* wip c++ library

* cpp wip

* alphabetize readme

* Try to get build working again

* Fix some conditionally compiled bits

* cross config should be in workspace root

* Fix PathHelper for new rust target dir

* Missed one old path to velopack.csproj

* remove obsolete coverage code

* testawareapp.exe no longer exists
2024-08-06 22:56:40 +01:00

106 lines
3.1 KiB
C#

using System;
using System.Text;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Threading;
using Microsoft.Extensions.Logging;
using Velopack;
namespace CSharpAvalonia;
public partial class MainWindow : Window
{
private UpdateManager _um;
private UpdateInfo _update;
public MainWindow()
{
InitializeComponent();
var updateUrl = SampleHelper.GetReleasesDir(); // replace with your update path/url
_um = new UpdateManager(updateUrl, logger: Program.Log);
TextLog.Text = Program.Log.ToString();
Program.Log.LogUpdated += LogUpdated;
UpdateStatus();
}
private async void BtnCheckUpdateClick(object sender, RoutedEventArgs e)
{
Working();
try {
// ConfigureAwait(true) so that UpdateStatus() is called on the UI thread
_update = await _um.CheckForUpdatesAsync().ConfigureAwait(true);
} catch (Exception ex) {
Program.Log.LogError(ex, "Error checking for updates");
}
UpdateStatus();
}
private async void BtnDownloadUpdateClick(object sender, RoutedEventArgs e)
{
Working();
try {
// ConfigureAwait(true) so that UpdateStatus() is called on the UI thread
await _um.DownloadUpdatesAsync(_update, Progress).ConfigureAwait(true);
} catch (Exception ex) {
Program.Log.LogError(ex, "Error downloading updates");
}
UpdateStatus();
}
private void BtnRestartApplyClick(object sender, RoutedEventArgs e)
{
_um.ApplyUpdatesAndRestart(_update);
}
private void LogUpdated(object sender, LogUpdatedEventArgs e)
{
// logs can be sent from other threads
Dispatcher.UIThread.InvokeAsync(() => {
TextLog.Text = e.Text;
ScrollLog.ScrollToEnd();
});
}
private void Progress(int percent)
{
// progress can be sent from other threads
Dispatcher.UIThread.InvokeAsync(() => {
TextStatus.Text = $"Downloading ({percent}%)...";
});
}
private void Working()
{
Program.Log.LogInformation("");
BtnCheckUpdate.IsEnabled = false;
BtnDownloadUpdate.IsEnabled = false;
BtnRestartApply.IsEnabled = false;
TextStatus.Text = "Working...";
}
private void UpdateStatus()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine($"Velopack version: {VelopackRuntimeInfo.VelopackNugetVersion}");
sb.AppendLine($"This app version: {(_um.IsInstalled ? _um.CurrentVersion : "(n/a - not installed)")}");
if (_update != null) {
sb.AppendLine($"Update available: {_update.TargetFullRelease.Version}");
BtnDownloadUpdate.IsEnabled = true;
} else {
BtnDownloadUpdate.IsEnabled = false;
}
if (_um.IsUpdatePendingRestart) {
sb.AppendLine("Update ready, pending restart to install");
BtnRestartApply.IsEnabled = true;
} else {
BtnRestartApply.IsEnabled = false;
}
TextStatus.Text = sb.ToString();
BtnCheckUpdate.IsEnabled = true;
}
}