-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
114 lines (98 loc) · 3.38 KB
/
MainWindow.xaml.cs
File metadata and controls
114 lines (98 loc) · 3.38 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System.Windows.Controls;
using DevExpress.Xpf.WindowsUI;
using Microsoft.Extensions.Caching.Memory;
using Rome.Helpers;
using Rome.Pages;
using Rome.Pages.Auctions;
using Rome.Pages.Investments;
using Rome.Pages.PressReleases;
namespace Rome;
public partial class MainWindow
{
private readonly IMemoryCache _cache;
private readonly Dispatcher _dispatcher = Dispatcher.CurrentDispatcher;
public MainWindow()
{
InitializeComponent();
_cache = App.Current.Services.GetRequiredService<IMemoryCache>();
SourceInitialized += MainWindow_SourceInitialized;
Closing += MainWindow_Closing;
Frame.Navigated += Frame_Navigated;
}
public void Navigate(RomePage pageEnum, object? parameter = null)
{
NavList.Tag = parameter;
NavList.SelectedItem = NavList.Items[(int)pageEnum];
}
private void ListView_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
var item = (ListViewItem?)e.AddedItems[0];
var pageEnum = (RomePage)item!.Tag;
NavHeader.Text = pageEnum.Title;
_ = _dispatcher.BeginInvoke(() =>
{
Frame.Navigate(GetPage(pageEnum), NavList.Tag);
NavList.Tag = null;
}, DispatcherPriority.Loaded);
}
private void Frame_Navigated(object sender, DevExpress.Xpf.WindowsUI.Navigation.NavigationEventArgs e)
{
if (e.Content is INavigable page)
{
page.HandleNavigatedTo(e.Parameter);
}
}
private void MainWindow_SourceInitialized(object? sender, EventArgs e)
{
this.RestoreWindowPlacement();
Navigate(RomePage.Home);
NavList.Focus();
}
private void MainWindow_Closing(object? sender, System.ComponentModel.CancelEventArgs e)
{
this.SaveWindowPlacement();
}
private NavigationPage GetPage(RomePage pageEnum)
{
return pageEnum switch
{
RomePage.Home => _cache.GetOrCreate(RomePage.Home, _ => new HomePage())!,
RomePage.Auctions => _cache.GetOrCreate(RomePage.Auctions, _ => new AuctionsPage())!,
RomePage.Investments => _cache.GetOrCreate(RomePage.Investments, _ => new InvestmentsPage())!,
RomePage.PressReleases => _cache.GetOrCreate(RomePage.PressReleases, _ => new PressReleasesPage())!,
_ => throw new NotImplementedException()
};
}
}
public enum RomePage
{
Home,
Auctions,
Investments,
PressReleases
}
#pragma warning disable IDE0079 // Remove unnecessary suppression
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "<Pending>")]
#pragma warning restore IDE0079 // Remove unnecessary suppression
public static class PageExtensions
{
extension(RomePage pageEnum)
{
public Type ToPageType()
{
return pageEnum switch
{
RomePage.Home => typeof(HomePage),
RomePage.Auctions => typeof(AuctionsPage),
RomePage.Investments => typeof(InvestmentsPage),
RomePage.PressReleases => typeof(PressReleasesPage),
_ => throw new NotImplementedException()
};
}
public string Title => pageEnum switch
{
RomePage.PressReleases => "Press Releases",
_ => pageEnum.ToString()
};
}
}