add command-line filename argument, so open-with works
This commit is contained in:
parent
a52468f03b
commit
c8e70f41bc
@ -15,7 +15,12 @@ public partial class App : Application
|
||||
{
|
||||
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
||||
{
|
||||
desktop.MainWindow = new MainWindow();
|
||||
string? fileToOpen = null;
|
||||
if (desktop.Args?.Length > 0)
|
||||
{
|
||||
fileToOpen = desktop.Args[0];
|
||||
}
|
||||
desktop.MainWindow = new MainWindow(fileToOpen);
|
||||
}
|
||||
|
||||
base.OnFrameworkInitializationCompleted();
|
||||
|
||||
@ -17,7 +17,11 @@ namespace NotePad
|
||||
private bool _isModified;
|
||||
private string? _originalText;
|
||||
|
||||
public MainWindow()
|
||||
public MainWindow() : this(null)
|
||||
{
|
||||
}
|
||||
|
||||
public MainWindow(string? fileToOpen)
|
||||
{
|
||||
InitializeComponent();
|
||||
UpdateTitle();
|
||||
@ -26,6 +30,30 @@ namespace NotePad
|
||||
textBox.TextChanged += OnTextChanged;
|
||||
|
||||
Closing += OnWindowClosing;
|
||||
|
||||
if (!string.IsNullOrEmpty(fileToOpen) && File.Exists(fileToOpen))
|
||||
{
|
||||
_ = LoadFileAsync(fileToOpen);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task LoadFileAsync(string filePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
var text = await File.ReadAllTextAsync(filePath);
|
||||
_currentFilePath = filePath;
|
||||
_currentFileName = Path.GetFileName(filePath);
|
||||
|
||||
this.FindControl<TextBox>("EditorTextBox")!.Text = text;
|
||||
_originalText = text;
|
||||
_isModified = false;
|
||||
UpdateTitle();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// Silently ignore file loading errors on startup
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateTitle()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user