mirror of
https://gitee.com/akwkevin/aistudio.-wpf.-diagram
synced 2026-03-14 21:46:37 +08:00
项目结构调整
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace Wpf.CartesianChart.ThreadSafe
|
||||
{
|
||||
public class RelayCommand : ICommand
|
||||
{
|
||||
private Action _action;
|
||||
|
||||
public RelayCommand(Action action)
|
||||
{
|
||||
_action = action;
|
||||
}
|
||||
|
||||
public bool CanExecute(object parameter)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Execute(object parameter)
|
||||
{
|
||||
_action();
|
||||
}
|
||||
|
||||
public event EventHandler CanExecuteChanged;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<UserControl x:Class="Wpf.CartesianChart.ThreadSafe.ThreadSafeExample"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
|
||||
xmlns:threadSafe="clr-namespace:Wpf.CartesianChart.ThreadSafe"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="300" d:DesignWidth="300">
|
||||
<UserControl.DataContext>
|
||||
<threadSafe:ThreadSafeViewModel></threadSafe:ThreadSafeViewModel>
|
||||
</UserControl.DataContext>
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"></RowDefinition>
|
||||
<RowDefinition Height="*"></RowDefinition>
|
||||
</Grid.RowDefinitions>
|
||||
<Button Command="{Binding ReadCommand}">Start Lecture</Button>
|
||||
<lvc:CartesianChart Grid.Row="1" DisableAnimations="True" DataTooltip="{x:Null}">
|
||||
<lvc:CartesianChart.Series>
|
||||
<lvc:LineSeries Values="{Binding Values}" PointGeometry="{x:Null}" LineSmoothness="0"></lvc:LineSeries>
|
||||
</lvc:CartesianChart.Series>
|
||||
<lvc:CartesianChart.AxisX>
|
||||
<lvc:Axis MinValue="{Binding Min}"></lvc:Axis>
|
||||
</lvc:CartesianChart.AxisX>
|
||||
</lvc:CartesianChart>
|
||||
</Grid>
|
||||
</UserControl>
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace Wpf.CartesianChart.ThreadSafe
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for TreadSafeExample.xaml
|
||||
/// </summary>
|
||||
public partial class ThreadSafeExample : UserControl
|
||||
{
|
||||
public ThreadSafeExample()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using LiveCharts;
|
||||
|
||||
namespace Wpf.CartesianChart.ThreadSafe
|
||||
{
|
||||
public class ThreadSafeViewModel
|
||||
{
|
||||
private double _trend;
|
||||
private double _min;
|
||||
private double _count;
|
||||
|
||||
public ThreadSafeViewModel()
|
||||
{
|
||||
Values = new ChartValues<double>();
|
||||
ReadCommand = new RelayCommand(Read);
|
||||
}
|
||||
|
||||
public ChartValues<double> Values { get; set; }
|
||||
public RelayCommand ReadCommand { get; set; }
|
||||
public double Min
|
||||
{
|
||||
get { return _min; }
|
||||
set
|
||||
{
|
||||
_min = value;
|
||||
OnPropertyChanged("Min");
|
||||
}
|
||||
}
|
||||
|
||||
public double Count
|
||||
{
|
||||
get { return _count; }
|
||||
set
|
||||
{
|
||||
_count = value;
|
||||
OnPropertyChanged("Count");
|
||||
}
|
||||
}
|
||||
|
||||
private void Read()
|
||||
{
|
||||
//lets keep in memory only the last 200 records,
|
||||
//to keep everything running faster
|
||||
const int keepRecords = 200;
|
||||
|
||||
Action readFromTread = () =>
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
Thread.Sleep(1);
|
||||
var r = new Random();
|
||||
_trend += (r.NextDouble() < 0.8 ? 1 : -1) * r.Next(0, 10);
|
||||
Values.Add(_trend);
|
||||
Min = Values.Count - keepRecords;
|
||||
if (Values.Count > keepRecords) Values.RemoveAt(0);
|
||||
}
|
||||
// ReSharper disable once FunctionNeverReturns
|
||||
};
|
||||
|
||||
//are 8 task enough adding a new value every ms?
|
||||
Task.Factory.StartNew(readFromTread);
|
||||
Task.Factory.StartNew(readFromTread);
|
||||
Task.Factory.StartNew(readFromTread);
|
||||
Task.Factory.StartNew(readFromTread);
|
||||
Task.Factory.StartNew(readFromTread);
|
||||
Task.Factory.StartNew(readFromTread);
|
||||
Task.Factory.StartNew(readFromTread);
|
||||
Task.Factory.StartNew(readFromTread);
|
||||
}
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
protected virtual void OnPropertyChanged(string propertyName = null)
|
||||
{
|
||||
if (PropertyChanged != null)
|
||||
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user