0

由于我的问题相当罕见,而且我不知道是什么原因造成的,因此我将尝试包含所有可能与之相关的代码,并精简到最基本的部分以避免不必要的混乱。

基本上,我有一个TabControl包含TabItemswhoesContent是 customUserControl的。这些控件具有以下形式:

<UserControl>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinition> 
        <ScrollViewer Height="Auto" HorizontalAlignment="Stretch" Margin="0" Name="scrollViewer11" VerticalAlignment="Stretch" Width="Auto" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"></ScrollViewer>
        <ScrollViewer Grid.Column="1" Height="Auto" Name="scrollViewer12" Width="Auto"  Margin="0" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"></ScrollViewer>
        <ScrollViewer Grid.Row="1" Height="Auto" Name="scrollViewer21" Width="Auto" Margin="0" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"></ScrollViewer>
        <ScrollViewer Grid.Row="1" Grid.Column="1" Height="Auto" Name="scrollViewer22" Width="Auto"  Margin="0"  HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"></ScrollViewer>
    </Grid>
</UserControl>

行和列的数量取决于所使用的控件,范围从 1x1 网格中的单个单元格到 3x2 网格中的 6 个单元格(每个都包含ScrollViewer)。

现在到我的实际 C# 代码。用户可以在每个单元格中添加WriteableBitmap代码组织如下:

public partial class MainWindow : Window
{
    List<WorkSpace> WorkSpaceList = new List<WorkSpace>();
    private WorkSpace currentSpace = null;

    //one of the methods that adds to the TabControl, here a 2x2 grid:

    private void NewWorkspaceFour(WorkSpace ws)
    {
        WorkSpaceFour workSpaceFour = new WorkSpaceFour();
        for (int i = 0; i < 4; i++)
        {
            WorkPlace wp = new WorkPlace();
            ws.workPlaceList.Add(wp);

            switch (i)
            {
                case 0:
                    workSpaceFour.scrollViewer11.Content = wp.grid;
                    break;
                case 1:
                    workSpaceFour.scrollViewer12.Content = wp.grid;
                    break;
                case 2:
                    workSpaceFour.scrollViewer21.Content = wp.grid;
                    break;
                case 3:
                    workSpaceFour.scrollViewer22.Content = wp.grid;
                    break;
            }
        }

        ws.tabItem.Content = workSpace;
        tabControlImages.Items.Add(ws.tabItem);
    }

    //triggered in UI e.g. by moving a Slider
    private void NewSettings(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
         currentSpace.NewSettings((float)(e.NewValue));
    }

}

internal class WorkSpace
{
    internal delegate void NewSettingHandler(float e);

    internal List<WorkPlace> workPlaceList = new List<WorkPlace>();
    internal TabItem tabItem = new TabItem();
    internal WorkPlace currentPlace = null;

    internal NewSettingsHandler NewSettings;

    internal WorkSpace()
    {
        NewSettings += ChangeSettings;
    }

    internal void ChangeSettings(float newValue)
    {
        //do something small with newValue
        currentPlace.RecalculateImage();
    }

    //...more methods that would use "newValue" in a different way, thus the delegate
}

internal class WorkPlace
{
    internal WriteableBitmap bitmap;
    internal Image image = new Image {//settings};
    internal Grid grid = new Grid {//settings};
    internal Grid gridImage = new Grid {//settings};

    internal WorkPlace()
    {
        grid.Children.Add(gridImage);
        gridImage.Children.Add(image);
    }

    internal void RecalculateImage()
    {
        //some rather long calculations...

        bitmap.WritePixels(...,...,...,...);
        image.Source = bitmap;
    }
}

通过该程序,用户可以更改选项卡以更改currentSpace并单击单元格以更改各自的currentPlace两者,只需更改引用即可,即currentSpace = space其中space指的WorkSpace是包含新选择的TabItem

现在问题如下。当一个选项卡包含一个简单的 1x1 网格,其中包含一个图像并且我移动滑块时,它运行得非常顺利。当一个选项卡包含一个 3x2 网格并且该网格中只有一个单元格包含 aWorkPlace和 abitmap时,null它的工作原理相同。但是,当 3x2 网格完全被“painted”填充bitmap并且滑块移动时,会出现明显的滞后,即使 6 个图像中只有一个被重新计算重绘。我不明白为什么会这样。它可能与渲染或对象引用有关,但我不太了解 C#,无法在这里看到和问题。

希望代码不会太长(我已经尽可能地精简了)并且足够清晰。如果没有,请说出来,我会更新/添加它。在这个程序的早期版本中,用户界面基本相同,但图像的重新计算却完全不同,但我从未观察到这个问题。

4

0 回答 0