mirror of
https://github.com/velopack/velopack.git
synced 2025-10-25 15:19:22 +00:00
Add WPF sample app
This commit is contained in:
9
examples/VeloWpfSample/App.xaml
Normal file
9
examples/VeloWpfSample/App.xaml
Normal file
@@ -0,0 +1,9 @@
|
||||
<Application x:Class="VeloWpfSample.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:VeloWpfSample"
|
||||
StartupUri="MainWindow.xaml">
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
</Application>
|
||||
10
examples/VeloWpfSample/App.xaml.cs
Normal file
10
examples/VeloWpfSample/App.xaml.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
using System.Windows;
|
||||
|
||||
namespace VeloWpfSample
|
||||
{
|
||||
public partial class App : Application
|
||||
{
|
||||
}
|
||||
}
|
||||
6
examples/VeloWpfSample/AssemblyInfo.cs
Normal file
6
examples/VeloWpfSample/AssemblyInfo.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
using System.Windows;
|
||||
|
||||
[assembly: ThemeInfo(
|
||||
ResourceDictionaryLocation.None,
|
||||
ResourceDictionaryLocation.SourceAssembly
|
||||
)]
|
||||
28
examples/VeloWpfSample/MainWindow.xaml
Normal file
28
examples/VeloWpfSample/MainWindow.xaml
Normal file
@@ -0,0 +1,28 @@
|
||||
<Window x:Class="VeloWpfSample.MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:VeloWpfSample"
|
||||
mc:Ignorable="d"
|
||||
WindowStartupLocation="CenterScreen"
|
||||
Title="VeloWpfSample" Height="600" Width="600">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="15" />
|
||||
<RowDefinition Height="*" />
|
||||
</Grid.RowDefinitions>
|
||||
<StackPanel>
|
||||
<TextBlock Margin="10" Name="TextStatus" />
|
||||
<StackPanel Orientation="Horizontal" Margin="10">
|
||||
<Button Name="BtnCheckUpdate" Content="Check for Updates" Click="BtnCheckUpdateClick" Padding="10,5" />
|
||||
<Button Margin="10,0" Name="BtnDownloadUpdate" Content="Download" Click="BtnDownloadUpdateClick" Padding="10,5" IsEnabled="False" />
|
||||
<Button Name="BtnRestartApply" Content="Restart & Apply" Click="BtnRestartApplyClick" Padding="10,5" IsEnabled="False" />
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
<ScrollViewer Name="ScrollLog" Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" VerticalScrollBarVisibility="Auto">
|
||||
<TextBlock Name="TextLog" Background="DarkGoldenrod" Foreground="White" TextWrapping="Wrap" />
|
||||
</ScrollViewer>
|
||||
</Grid>
|
||||
</Window>
|
||||
109
examples/VeloWpfSample/MainWindow.xaml.cs
Normal file
109
examples/VeloWpfSample/MainWindow.xaml.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Velopack;
|
||||
|
||||
namespace VeloWpfSample
|
||||
{
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
private UpdateManager _um;
|
||||
private UpdateInfo _update;
|
||||
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
_um = new UpdateManager(Program.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();
|
||||
}
|
||||
|
||||
private void LogUpdated(object sender, LogUpdatedEventArgs e)
|
||||
{
|
||||
// logs can be sent from other threads
|
||||
this.Dispatcher.InvokeAsync(() => {
|
||||
TextLog.Text = e.Text;
|
||||
ScrollLog.ScrollToEnd();
|
||||
});
|
||||
}
|
||||
|
||||
private void Progress(int percent)
|
||||
{
|
||||
// progress can be sent from other threads
|
||||
this.Dispatcher.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
48
examples/VeloWpfSample/MemoryLogger.cs
Normal file
48
examples/VeloWpfSample/MemoryLogger.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace VeloWpfSample
|
||||
{
|
||||
public class LogUpdatedEventArgs : EventArgs
|
||||
{
|
||||
public string Text { get; set; }
|
||||
}
|
||||
|
||||
public class MemoryLogger : ILogger
|
||||
{
|
||||
public event EventHandler<LogUpdatedEventArgs> LogUpdated;
|
||||
private readonly StringBuilder _sb = new StringBuilder();
|
||||
|
||||
public IDisposable BeginScope<TState>(TState state)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool IsEnabled(LogLevel logLevel)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
|
||||
{
|
||||
lock (_sb) {
|
||||
var message = formatter(state, exception);
|
||||
if (exception != null) message += "\n" + exception.ToString();
|
||||
Console.WriteLine("log: " + message);
|
||||
_sb.AppendLine(message);
|
||||
LogUpdated?.Invoke(this, new LogUpdatedEventArgs { Text = _sb.ToString() });
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
lock (_sb) {
|
||||
return _sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
56
examples/VeloWpfSample/Program.cs
Normal file
56
examples/VeloWpfSample/Program.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using Velopack;
|
||||
|
||||
namespace VeloWpfSample
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static bool WasFirstRun { get; private set; }
|
||||
|
||||
public static bool WasJustUpdated { get; private set; }
|
||||
|
||||
public static string UpdateUrl { get; private set; }
|
||||
|
||||
public static MemoryLogger Log { get; private set; }
|
||||
|
||||
// Since WPF has an "automatic" Program.Main, we need to create our own.
|
||||
// In order for this to work, you must also add the following to your .csproj:
|
||||
// <StartupObject>VeloWpfSample.Program</StartupObject>
|
||||
[STAThread]
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
try {
|
||||
// Logging is essential for debugging! Ideally you should write it to a file.
|
||||
Log = new MemoryLogger();
|
||||
|
||||
// It's important to Run() the VelopackApp as early as possible in app startup.
|
||||
VelopackApp.Build()
|
||||
.WithRestarted((v) => WasJustUpdated = true)
|
||||
.WithFirstRun((v) => WasFirstRun = true)
|
||||
.Run(Log);
|
||||
|
||||
// This is purely for demonstration purposes, we get the update URL from a
|
||||
// property defined by MSBuild, so we can locate the local releases directory.
|
||||
// In your production app, this should point to your update server.
|
||||
UpdateUrl = Assembly.GetEntryAssembly()
|
||||
.GetCustomAttributes<AssemblyMetadataAttribute>()
|
||||
.Where(x => x.Key == "WpfSampleReleaseDir")
|
||||
.Single().Value;
|
||||
|
||||
// We can now launch the WPF application as normal.
|
||||
var app = new App();
|
||||
app.InitializeComponent();
|
||||
app.Run();
|
||||
} catch (Exception ex) {
|
||||
MessageBox.Show("Unhandled exception: " + ex.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
23
examples/VeloWpfSample/VeloWpfSample.csproj
Normal file
23
examples/VeloWpfSample/VeloWpfSample.csproj
Normal file
@@ -0,0 +1,23 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net8.0-windows</TargetFramework>
|
||||
<ImplicitUsings>true</ImplicitUsings>
|
||||
<UseWPF>true</UseWPF>
|
||||
<StartupObject>VeloWpfSample.Program</StartupObject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Velopack" Version="0.*" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Used for demonstrating updates, so the installed application can find the Release directory -->
|
||||
<ItemGroup>
|
||||
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
|
||||
<_Parameter1>WpfSampleReleaseDir</_Parameter1>
|
||||
<_Parameter2>$(MSBuildThisFileDirectory)releases</_Parameter2>
|
||||
</AssemblyAttribute>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
16
examples/VeloWpfSample/build.bat
Normal file
16
examples/VeloWpfSample/build.bat
Normal file
@@ -0,0 +1,16 @@
|
||||
@echo off
|
||||
setlocal enabledelayedexpansion
|
||||
|
||||
if "%~1"=="" (
|
||||
echo Version number is required.
|
||||
echo Usage: build.bat [version]
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
set "version=%~1"
|
||||
|
||||
echo Compiling VeloWpfSample with dotnet...
|
||||
dotnet publish -c Release --no-self-contained -r win-x64 -o %~dp0publish
|
||||
|
||||
echo Building Velopack Release v%version%
|
||||
vpk pack -u VeloWpfSample -v %version% -o %~dp0releases -p %~dp0publish -f net8-x64-desktop
|
||||
28
examples/VeloWpfSample/readme.md
Normal file
28
examples/VeloWpfSample/readme.md
Normal file
@@ -0,0 +1,28 @@
|
||||
# VeloWpfSample
|
||||
_Prerequisites: vpk command line tool installed_
|
||||
|
||||
You can run this sample by executing the build script with a version number: `build.bat 1.0.0`. Once built, you can install the app - build more updates, and then test updates and so forth. The sample app will check the local release dir for new update packages.
|
||||
|
||||
In your production apps, you should deploy your updates to some kind of update server instead.
|
||||
|
||||
## WPF Implementation Notes
|
||||
WPF generates a `Program.Main(argv[])` method automatically for you, so it requires a couple of extra steps to get Velopack working with WPF.
|
||||
|
||||
1. You need to create your own `Program.cs` class, and add a static `Main()` method.
|
||||
2. In order for dotnet to execute this new Main() method instead of the default WPF one, you need to add the following to your .csproj:
|
||||
```xml
|
||||
<PropertyGroup>
|
||||
<StartupObject>YourNamespace.Program</StartupObject>
|
||||
</PropertyGroup>
|
||||
```
|
||||
3. You should run the `VelopackApp` builder before starting WPF as usual.
|
||||
```cs
|
||||
[STAThread]
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
VelopackApp.Build().Run();
|
||||
var application = new App();
|
||||
application.InitializeComponent();
|
||||
application.Run();
|
||||
}
|
||||
```
|
||||
@@ -1,3 +1,5 @@
|
||||
# Velopack Examples
|
||||
|
||||
- [**AvaloniaCrossPlat**](AvaloniaCrossPlat) - this Example app uses Avalonia to provide a desktop UI for Mac and Windows. The provided build scripts (`build-win.bat` and `build-osx.sh`) will compile Velopack dependencies, so check that you've installed the [Velopack compiling prerequisites](../docs/compiling.md) before cloning this repository.
|
||||
- [**AvaloniaCrossPlat**](AvaloniaCrossPlat) - this Example app uses Avalonia to provide a desktop UI for Mac and Windows. The provided build scripts (`build-win.bat` and `build-osx.sh`) will compile Velopack dependencies, so check that you've installed the [Velopack compiling prerequisites](../docs/compiling.md) before cloning this repository.
|
||||
|
||||
- [**VeloWpfSample**](VeloWpfSample) - demonstrates how to use Velopack effectively with WPF.
|
||||
Reference in New Issue
Block a user