-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathYourViewModel.cs
More file actions
65 lines (58 loc) · 1.86 KB
/
YourViewModel.cs
File metadata and controls
65 lines (58 loc) · 1.86 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
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
namespace ScottPlotV4MVVMDemo
{
public partial class YourViewModel : ObservableValidator
{
[ObservableProperty]
private Point _extents = new Point(640, 65535);
private const int maxIterations = 5000;
private int _iterationCount = 0;
[ObservableProperty]
private ObservableCollection<Point> _points = new ObservableCollection<Point>();
private DispatcherTimer _timer;
private readonly Random _random = new Random();
private readonly Stopwatch _stopwatch = new Stopwatch();
public YourViewModel()
{
InitializeTimer();
}
private void InitializeTimer()
{
_timer = new DispatcherTimer
{
Interval = TimeSpan.FromMilliseconds(1000 / 100) // 100 Hz
};
_timer.Tick += TimerTick;
_timer.Start();
}
private void TimerTick(object? sender, EventArgs e)
{
_stopwatch.Restart();
RandomPoints();
_stopwatch.Stop();
double renderTime = _stopwatch.Elapsed.TotalMilliseconds;
double fps = 1000 / renderTime;
Trace.WriteLine($"Frame rendered in {renderTime} ms ({fps:F2} FPS)");
_iterationCount++;
if (_iterationCount >= maxIterations)
{
_timer.Stop();
Trace.WriteLine("DONE");
}
}
private void RandomPoints()
{
Points.Clear();
for (int i = 0; i < 640; i++)
{
ushort val = (ushort)_random.Next(0, 65535);
Points.Add(new Point(i, val));
}
}
}
}