WPF 更新行

WPF Update lines

我正在尝试在 wpf(vb.net) 中复制我以前在 winform 中制作的应用程序。 在旧应用程序中,我基本上打开一个二进制文件并将其加载到一个数组中,然后用它绘制一个二维折线图。文件大小约为 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。我知道这不是最好的方法,但对于这个小应用程序来说效果很好