Moved logic from MainWindow.axaml.cs to HeaderView.axaml.cs
This commit is contained in:
parent
ac01b81b35
commit
a0d7806388
@ -1,59 +1,12 @@
|
|||||||
using System;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using Avalonia.Interactivity;
|
|
||||||
using Avalonia.Threading;
|
|
||||||
using XSDVisualiser.Core;
|
|
||||||
using XSDVisualiser.Models;
|
|
||||||
using XSDVisualiser.Desktop.ViewModels;
|
|
||||||
using XSDVisualiser.Desktop.Views;
|
|
||||||
|
|
||||||
namespace XSDVisualiser.Desktop
|
namespace XSDVisualiser.Desktop
|
||||||
{
|
{
|
||||||
public partial class MainWindow : Window
|
public partial class MainWindow : Window
|
||||||
{
|
{
|
||||||
private Button _openBtn = null!;
|
|
||||||
|
|
||||||
public MainWindow()
|
public MainWindow()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
var header = this.FindControl<HeaderView>("Header");
|
|
||||||
var btn = header?.FindControl<Button>("OpenBtn");
|
|
||||||
if (btn == null) return;
|
|
||||||
_openBtn = btn;
|
|
||||||
_openBtn.Click += OpenBtn_Click;
|
|
||||||
}
|
|
||||||
|
|
||||||
private async void OpenBtn_Click(object? sender, RoutedEventArgs e)
|
|
||||||
{
|
|
||||||
var ofd = new OpenFileDialog
|
|
||||||
{
|
|
||||||
AllowMultiple = false,
|
|
||||||
Title = "Open XSD file"
|
|
||||||
};
|
|
||||||
ofd.Filters!.Add(new FileDialogFilter { Name = "XSD schema", Extensions = { "xsd" } });
|
|
||||||
var files = await ofd.ShowAsync(this);
|
|
||||||
if (files is { Length: > 0 })
|
|
||||||
{
|
|
||||||
await ParseAndShowAsync(files[0]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Task ParseAndShowAsync(string path)
|
|
||||||
{
|
|
||||||
return Task.Run(() =>
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var parser = new XsdSchemaParser();
|
|
||||||
var model = parser.Parse(path);
|
|
||||||
Dispatcher.UIThread.Post(() => DataContext = new MainWindowViewModel(model));
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Dispatcher.UIThread.Post(() => DataContext = new MainWindowViewModel(new SchemaModel { TargetNamespace = ex.Message }));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,11 +1,77 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Avalonia;
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Interactivity;
|
||||||
|
using Avalonia.Threading;
|
||||||
|
using Avalonia.Platform.Storage;
|
||||||
|
using XSDVisualiser.Core;
|
||||||
|
using XSDVisualiser.Models;
|
||||||
|
using XSDVisualiser.Desktop.ViewModels;
|
||||||
|
|
||||||
namespace XSDVisualiser.Desktop.Views;
|
namespace XSDVisualiser.Desktop.Views;
|
||||||
|
|
||||||
public partial class HeaderView : UserControl
|
public partial class HeaderView : UserControl
|
||||||
{
|
{
|
||||||
|
private readonly Button? _openBtn;
|
||||||
|
|
||||||
public HeaderView()
|
public HeaderView()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
_openBtn = this.FindControl<Button>("OpenBtn");
|
||||||
|
if (_openBtn != null)
|
||||||
|
{
|
||||||
|
_openBtn.Click += OpenBtn_Click;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async void OpenBtn_Click(object? sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
if (TopLevel.GetTopLevel(this) is not Window topLevel)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var storageProvider = topLevel.StorageProvider;
|
||||||
|
var options = new FilePickerOpenOptions
|
||||||
|
{
|
||||||
|
AllowMultiple = false,
|
||||||
|
Title = "Open XSD file",
|
||||||
|
FileTypeFilter =
|
||||||
|
[
|
||||||
|
new FilePickerFileType("XSD schema")
|
||||||
|
{
|
||||||
|
Patterns = ["*.xsd"]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
var results = await storageProvider.OpenFilePickerAsync(options);
|
||||||
|
if (results.Count <= 0) return;
|
||||||
|
|
||||||
|
var file = results[0];
|
||||||
|
var path = file.TryGetLocalPath();
|
||||||
|
if (!string.IsNullOrEmpty(path))
|
||||||
|
{
|
||||||
|
await ParseAndShowAsync(path!, topLevel);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Dispatcher.UIThread.Post(() => topLevel.DataContext = new MainWindowViewModel(new SchemaModel { TargetNamespace = "Selected file is not a local file. Please choose a local .xsd." }));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Task ParseAndShowAsync(string path, Window hostWindow)
|
||||||
|
{
|
||||||
|
return Task.Run(() =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var parser = new XsdSchemaParser();
|
||||||
|
var model = parser.Parse(path);
|
||||||
|
Dispatcher.UIThread.Post(() => hostWindow.DataContext = new MainWindowViewModel(model));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Dispatcher.UIThread.Post(() => hostWindow.DataContext = new MainWindowViewModel(new SchemaModel { TargetNamespace = ex.Message }));
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user