-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMainWindow.xaml.cs
executable file
·115 lines (97 loc) · 4.07 KB
/
MainWindow.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
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
115
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
namespace NGettext.Wpf.Example
{
public partial class MainWindow : INotifyPropertyChanged
{
private int _memoryLeakTestProgress;
private DateTime _currentTime;
private readonly string _someDeferredLocalization = Translation.Noop("Deferred localization");
private int _counter;
public MainWindow()
{
InitializeComponent();
var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(0.1) };
timer.Tick += (sender, args) => { CurrentTime = DateTime.Now; };
timer.Tick += (sender, args) => { Counter = (Counter + 1) % 1000; };
timer.Start();
}
public decimal SomeNumber => 1234567.89m;
public DateTime CurrentTime
{
get => _currentTime;
set
{
_currentTime = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentTime)));
}
}
private async void OpenMemoryLeakTestWindow(object sender, RoutedEventArgs e)
{
var leakTestWindowReference = GetWeakReferenceToLeakTestWindow();
for (var i = 0; i < 20; ++i)
{
if (!leakTestWindowReference.TryGetTarget(out _)) return;
await Task.Delay(TimeSpan.FromSeconds(1));
GC.Collect();
}
Debug.Assert(!leakTestWindowReference.TryGetTarget(out _), "memory leak detected");
}
private WeakReference<MemoryLeakTestWindow> GetWeakReferenceToLeakTestWindow()
{
var window = new MemoryLeakTestWindow();
window.Closed += async (o, args) =>
{
await Task.Delay(TimeSpan.FromSeconds(1));
++MemoryLeakTestProgress;
foreach (var locale in new[]
{"da-DK", "de-DE", "en-US", TrackCurrentCultureBehavior.CultureTracker?.CurrentCulture?.Name})
{
await Task.Delay(TimeSpan.FromSeconds(1));
if (TrackCurrentCultureBehavior.CultureTracker != null)
{
TrackCurrentCultureBehavior.CultureTracker.CurrentCulture = CultureInfo.GetCultureInfo(locale);
}
++MemoryLeakTestProgress;
}
};
window.Show();
MemoryLeakTestProgress = 0;
window.Close();
return new WeakReference<MemoryLeakTestWindow>(window);
}
public int MemoryLeakTestProgress
{
get => _memoryLeakTestProgress;
set
{
_memoryLeakTestProgress = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MemoryLeakTestProgress)));
}
}
public ICollection<ExampleEnum> EnumValues { get; } = Enum.GetValues(typeof(ExampleEnum)).Cast<ExampleEnum>().ToList();
public event PropertyChangedEventHandler PropertyChanged;
public string SomeDeferredLocalization => Translation._(_someDeferredLocalization);
public string Header => Translation._("NGettext.WPF Example");
public string PluralGettext => Translation.PluralGettext(1, "Singular", "Plural") +
"---" + Translation.PluralGettext(2, "Singular", "Plural");
public string PluralGettextParams => Translation.PluralGettext(1, "Singular {0:n3}", "Plural {0:n3}", 1m / 3m) +
"---" + Translation.PluralGettext(2, "Singular {0:n3}", "Plural {0:n3}", 1m / 3m);
public int Counter
{
get => _counter;
set
{
_counter = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Counter)));
}
}
}
}