-
Notifications
You must be signed in to change notification settings - Fork 9
/
App.xaml.cs
80 lines (71 loc) · 3.09 KB
/
App.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.IO;
using System.Windows;
using CSharpWpfChatGPT.Services;
using RestoreWindowPlace;
namespace CSharpWpfChatGPT
{
public partial class App : Application
{
private WindowPlace? _windowPlace;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
try
{
// TODO 1: See README.md and get your OpenAI API key: https://platform.openai.com/account/api-keys
// TODO 2: You can modify ChatViewModel to set default selected language: _selectedLang = LangList[..]
// TODO 3: Give my article 5 stars:) at https://www.codeproject.com/Tips/5377103/ChatGPT-API-in-Csharp-WPF-XAML-MVVM
string openaiApiKey;
if (e.Args?.Length > 0 && e.Args[0].StartsWith('/'))
{
// OpenAI API key from command line parameter such as "/sk-Ih...WPd" after removing '/'
openaiApiKey = e.Args[0].Remove(0, 1);
}
else
{
// Put your key from above here instead of using a command line parameter in the 'if' block
openaiApiKey = "<Your Open AI API Key is something like \"sk-Ih...WPd\">";
}
// Programmatically switch between SqlHistoryRepo and EmptyHistoryRepo
// If you have configured SQL Server, try SqlHistoryRepo
//IHistoryRepo historyRepo = new SqlHistoryRepo();
IHistoryRepo historyRepo = new EmptyHistoryRepo();
var chatGPTService = new WhetstoneChatGPTService(openaiApiKey);
var mainViewModel = new MainViewModel(historyRepo, chatGPTService);
var mainWindow = new MainWindow(mainViewModel);
SetupRestoreWindowPlace(mainWindow);
mainWindow.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "CSharpWpfChatGPT will exit on error");
Current?.Shutdown();
}
}
protected override void OnExit(ExitEventArgs e)
{
base.OnExit(e);
try
{
_windowPlace?.Save();
}
catch (Exception)
{
}
}
private void SetupRestoreWindowPlace(MainWindow mainWindow)
{
string windowPlaceConfigFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CSharpWpfChatGPTWindowPlace.config");
_windowPlace = new WindowPlace(windowPlaceConfigFilePath);
_windowPlace.Register(mainWindow);
// This logic works but I don't like the window being maximized
//if (!File.Exists(windowPlaceConfigFilePath))
//{
// // For the first time, maximize the window, so it won't go off the screen on laptop
// // WindowPlacement will take care of future runs
// mainWindow.WindowState = WindowState.Maximized;
//}
}
}
}