问题描述
我正在尝试在wpf(vb.net)中复制我之前在winform中制作的应用程序。 在旧的应用程序中,我基本上是打开一个二进制文件并将其加载到一个数组中,然后使用它绘制2d折线图。文件大小约为2 / 4MB,因此我放置了一个滑块,每次更改它时,我都会使用滑块偏移量来调用绘图子。 现在我完全是wpf的新手,我想出了如何绘制图表,但是当移动滑块或通过代码更改数组的值时(下一步),我不明白如何更新图表。 为了进行简单的测试,我在启动时和单击button1时加载文件,然后修改数组值,我会看到更新图表,但找不到方法。
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:testwpf2d"
mc:Ignorable="d"
Title="MainWindow" Height="1920" Width="1080">
<Grid x:Name="BaseGrid">
<Canvas Name="paintSurface" >
<Canvas.Background>
<SolidColorBrush Color="Black" Opacity="1"/>
</Canvas.Background>
<Button Content="Button" Canvas.Left="184" Canvas.Top="36" Width="75" Click="Button_Click"/>
</Canvas>
</Grid>
</Window>
Private Sub BaseGrid_Loaded(sender As Object,e As RoutedEventArgs) Handles BaseGrid.Loaded
Dim openFileDialogORI As OpenFileDialog = New OpenFileDialog()
If openFileDialogORI.ShowDialog = True Then
Try
Dim MyFileORIStream As FileStream = New FileStream(openFileDialogORI.FileName,FileMode.Open,FileAccess.Read)
Dim ORIBuffer As BinaryReader = New BinaryReader(MyFileORIStream)
Dim fInfo As New FileInfo(openFileDialogORI.FileName)
Dim numBytes As Long = fInfo.Length
MyORIArray = ORIBuffer.ReadBytes(CInt(numBytes))
ORIBuffer.Close()
MyFileORIStream.Close()
Dim NomeFileOri As String = openFileDialogORI.FileName
Dim info As New FileInfo(openFileDialogORI.FileName)
Dim length As Integer = CInt(info.Length)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
MessageBox.Show("File loaded")
For i As Integer = 1 To 2000
Dim Line As New Line
Line.X1 = i
Line.X2 = i + 1
Line.Y1 = MyORIArray(i)
Line.Y2 = MyORIArray(i + 1)
Line.Stroke = Brushes.YellowGreen
Line.StrokeThickness = 0.25
BaseGrid.Children.Add(Line)
Next
End Sub
Private Sub Button_Click(sender As Object,e As RoutedEventArgs)
Dim Rand As New Random
For i As Integer = 1 To 1000
MyORIArray(i) = Rand.NextDouble * 50
Next
MessageBox.Show("new data ")
BaseGrid.UpdateLayout()
paintSurface.UpdateLayout()
End Sub
解决方法
我解决了从数组创建点集合并绘制一条折线的问题,当更改数组点时(总是通过引发事件(按钮,鼠标等)的用户交互,我更新或重新创建点集合,然后使用update.layout。我知道这不是最好的方法,但是对于这个小小的应用程序来说很好