How to avoid using WPF to create mechanisms / dimensions on my own controls?

I have developed a 3D engine in C #, and I want it to be able to be used from a WPF application through classes that can be used just like any other WPF control.

First implementation

I created a panel (I’ll call her EnginePanel) that extends the Grid class and a set of controls that can be inserted inside, for example SceneNodeControl, GeometryControl, etc. These controls inherit the FrameworkElement WPF class.

For SceneNodeControl, I discovered the Children property (UIElementCollection) and redefined the Logical / Visual control methods to look at this collection.

I also override the ArrangeOverride and MeasureOverride methods so that we call Arrange / Measure for each child of the SceneNodeControl and then return a constant. The size of the zero pixels.

This implementation works and allows me to use ItemsControl, ContentControl, and DataTemplates to populate my scene graph with the classic MVVM pattern.

Problem

My problem is that if I create a massive scene graph with hundreds of SceneNodeControls, the application will slow down dramatically.

A quick check using Visual Studio Profiler informed me that the Measure method of SceneNodeControl is responsible for 80% -90% of CPU usage (in terms of time).

OK, no problem. I have to remove these calculations, which are too heavy and, in addition, are useless in my case.

Solution 1

Arrange/Measure SceneNodeControl.

. MSDN, Arrange/Measure .

2

FrameworkContentElement. , .

, ItemsControls, ContentControl DataTemplates.

... !

, WPF, / ?

!

3

( ).

DependencyObject. , DataContextes WPF , , DependencyProperties.

1: DataTemplate, ItemsControl ContentControl, , , ...

2: DependencyObject "" DependencyObject. , "" , InheritanceContext InheritanceParent. , , Microsoft, , , , ...

, , . (Edit: It .)

:

- :

<controls:SceneNodeControl NodeName="Root">
    <ItemsControl ItemsSource="{Binding SceneNodes}">
        <ItemsControl.ItemTemplate>
            <DataTemplate DataType="{x:Type local:SceneNodeViewModel}">
                <controls:SceneNodeControl NodeName="{Binding Name}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</controls:SceneNodeControl>

Measure/Arrange SceneNodeControls, / .

- .

+4
1

, Arrange , NodeSceneControl , Visibility Visibility.Collapsed. , node .

0

All Articles