-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created wpf sample and extensions library
- Loading branch information
1 parent
aacbd21
commit 88c8d7b
Showing
35 changed files
with
1,885 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<Application x:Class="SocketHook.HostedWpfSample.App" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:converters="clr-namespace:SocketHook.HostedWpfSample.Converters" | ||
ShutdownMode="OnMainWindowClose"> | ||
|
||
<Application.Resources> | ||
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/> | ||
<converters:BitmapToSource x:Key="BitmapToSourceConverter"/> | ||
<Style TargetType="ScrollBar"> | ||
<Setter Property="Background" Value="#3f3f46" /> | ||
<Setter Property="BorderBrush" Value="#3f3f46" /> | ||
<Setter Property="Foreground" Value="#686868" /> | ||
</Style> | ||
|
||
<Style TargetType="Grid"> | ||
<Setter Property="Background" Value="#252526" /> | ||
</Style> | ||
|
||
<Style TargetType="Label"> | ||
<Setter Property="Foreground" Value="#f1f1f1" /> | ||
</Style> | ||
|
||
<Style TargetType="TextBox"> | ||
<Setter Property="BorderBrush" Value="#3f3f46" /> | ||
<Setter Property="Background" Value="#333337" /> | ||
<Setter Property="Foreground" Value="#f1f1f1" /> | ||
</Style> | ||
|
||
<Style TargetType="{x:Type Button}"> | ||
<Setter Property="BorderBrush" Value="#3f3f46" /> | ||
<Setter Property="Background" Value="#2d2d30" /> | ||
<Setter Property="Foreground" Value="#f1f1f1" /> | ||
|
||
<Style.Triggers> | ||
<Trigger Property="IsMouseOver" Value="True"> | ||
<Setter Property="Background" Value="#3e3e40"/> | ||
<Setter Property="BorderBrush" Value="#3399ff"/> | ||
</Trigger> | ||
</Style.Triggers> | ||
</Style> | ||
</Application.Resources> | ||
</Application> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System; | ||
using System.IO; | ||
using System.Windows; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using SocketHook.Extensions; | ||
using SocketHook.HostedWpfSample.Services; | ||
using SocketHook.HostedWpfSample.ViewModels; | ||
using SocketHook.HostedWpfSample.Views; | ||
|
||
namespace SocketHook.HostedWpfSample | ||
{ | ||
/// <summary> | ||
/// Interaction logic for App.xaml | ||
/// </summary> | ||
public partial class App : Application | ||
{ | ||
public static IHost RunningHost { get; private set; } // keeps a lifetime instance | ||
|
||
private static IHost CreateHost(string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
//.ConfigureHostConfiguration(config => config.AddCommandLine(args)) | ||
.ConfigureAppConfiguration(config => | ||
{ | ||
config.SetBasePath(Directory.GetCurrentDirectory()); | ||
config.AddJsonFile("appsettings.json", optional: true); | ||
config.AddCommandLine(args); | ||
}) | ||
.ConfigureServices((ctx, services) => | ||
{ | ||
if(ctx.HostingEnvironment.IsDevelopment()) | ||
services.AddLogging(logger => | ||
{ | ||
logger.SetMinimumLevel(LogLevel.Trace); | ||
logger.AddFile($"app{DateTime.Now:yyyy-dd-M--HH-mm-ss}.log"); | ||
}); | ||
|
||
services.AddSocketHook(opt => | ||
{ | ||
opt.AddConfiguration(ctx.Configuration); | ||
opt.Configure(x => | ||
{ | ||
x.UseHookServiceFactory = true; | ||
x.OpenHookOnStartup = true; | ||
x.KillAllOnExit = true; | ||
}); | ||
}); | ||
|
||
services.AddSingleton<IProcessObserverService, ProcessObserverService>(); | ||
services.AddSingleton<IInjectOptionsService, InjectOptionsService>(); | ||
services.AddTransient<InjectOptionsViewModel>(); | ||
services.AddTransient<MainWindowViewModel>(); | ||
services.AddTransient<InjectOptionsView>(); | ||
services.AddTransient<MainWindow>(); | ||
}).Build(); | ||
|
||
protected override void OnStartup(StartupEventArgs e) | ||
{ | ||
DispatcherUnhandledException += (o, s) => | ||
{ | ||
if (s.Handled) return; | ||
MessageBox.Show($"An unhandled exception was propagated to the UI thread :\n{s.Exception}", "Fatal error", | ||
MessageBoxButton.OK, MessageBoxImage.Error); | ||
|
||
RunningHost?.StopAsync()?.GetAwaiter().GetResult(); | ||
RunningHost?.Dispose(); | ||
}; | ||
|
||
RunningHost = CreateHost(e.Args); | ||
RunningHost.Start(); | ||
|
||
var mainWindow = RunningHost.Services.GetRequiredService<MainWindow>(); | ||
MainWindow = mainWindow; | ||
mainWindow.Show(); | ||
base.OnStartup(e); | ||
} | ||
|
||
protected override void OnExit(ExitEventArgs e) | ||
{ | ||
try { RunningHost.Dispose(); } catch { /* discarded */ } | ||
base.OnExit(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Windows; | ||
|
||
[assembly: ThemeInfo( | ||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located | ||
//(used if a resource is not found in the page, | ||
// or application resource dictionaries) | ||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located | ||
//(used if a resource is not found in the page, | ||
// app, or any theme specific resource dictionaries) | ||
)] |
57 changes: 57 additions & 0 deletions
57
samples/SocketHook.HostedWpfSample/Commands/ActionCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Windows.Input; | ||
|
||
namespace SocketHook.HostedWpfSample.Commands | ||
{ | ||
public class ActionCommand : ICommand | ||
{ | ||
private readonly Action _execute; | ||
private Func<bool> _predicate; | ||
|
||
public event EventHandler CanExecuteChanged | ||
{ | ||
add => CommandManager.RequerySuggested += value; | ||
remove => CommandManager.RequerySuggested -= value; | ||
} | ||
|
||
private ActionCommand(Action execute, Func<bool> predicate = default) | ||
{ | ||
_execute = execute; | ||
_predicate = predicate; | ||
} | ||
|
||
public static ICommand Create(Action execute, Func<bool> predicate = default) => | ||
new ActionCommand(execute, predicate); | ||
|
||
public static ICommand Create<T>(Action<T> execute, Predicate<T> predicate = default) => | ||
new ActionCommand<T>(execute, predicate); | ||
|
||
public bool CanExecute(object parameter) => (_predicate ??= () => true)(); | ||
public void Execute(object parameter) => _execute(); | ||
} | ||
|
||
public class ActionCommand<T> : ICommand | ||
{ | ||
private readonly Predicate<T> _canExecute; | ||
private readonly Action<T> _execute; | ||
|
||
public event EventHandler CanExecuteChanged | ||
{ | ||
add => CommandManager.RequerySuggested += value; | ||
remove => CommandManager.RequerySuggested -= value; | ||
} | ||
|
||
public ActionCommand(Action<T> execute) | ||
: this(execute, null) => _execute = execute; | ||
|
||
public ActionCommand(Action<T> execute, Predicate<T> canExecute) | ||
{ | ||
_execute = execute ?? throw new ArgumentNullException(nameof(execute)); | ||
_canExecute = canExecute; | ||
} | ||
|
||
public bool CanExecute(object parameter) => _canExecute == null || _canExecute((T)parameter); | ||
|
||
public void Execute(object parameter) => _execute((T)parameter); | ||
} | ||
} |
Oops, something went wrong.