正确/高效地处理WPF画布调整大小

时间:2016-04-09 13:27:33

标签: c# wpf canvas

我附上工作代码。我的问题:我是否正确/有效地处理调整大小?我在画布中心的WPF画布圆上画一个场景,在画布中心画一条垂直和水平线。当我调整画布大小时,我清除所有子项并根据新的画布大小重绘整个场景。说RenderTransform等有更好的方法吗?仅供参考我的真实应用程序要比这个简单的例子复杂得多,我只想发布这段代码,因为它演示了用于演示我正在尝试的真正优化的最短代码。

XAML:

    <Window x:Class="Sample2.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:Sample2"
        mc:Ignorable="d"
        Title="MainWindow" Height="400" Width="400"  SizeChanged="Window_SizeChanged">
    <Grid>
        <Canvas x:Name="MainCanvas">

        </Canvas>
    </Grid>
</Window>

代码背后:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Sample2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void DrawScene()
        {
             MainCanvas.Children.Clear();

            double ctrX = MainCanvas.ActualWidth    / 2;
            double ctrY = MainCanvas.ActualHeight   / 2;

            Ellipse elps = new Ellipse();
            elps.Width = elps.Height = 100;
            elps.Stroke = Brushes.Red;
            MainCanvas.Children.Add(elps);
            Canvas.SetLeft(elps, ctrX-elps.Width/2);
            Canvas.SetTop(elps,  ctrY-elps.Height/2);

            Line lnVertical   = new Line();
            lnVertical.Stroke = Brushes.Blue;
            lnVertical.X1 = lnVertical.X2 = ctrX;
            lnVertical.Y1 = 0;
            lnVertical.Y2 = MainCanvas.ActualHeight;
            MainCanvas.Children.Add(lnVertical);

            Line lnHorizontal   = new Line();
            lnHorizontal.Stroke = Brushes.Red;
            lnHorizontal.X1 = 0;
            lnHorizontal.X2 = MainCanvas.ActualWidth;
            lnHorizontal.Y1 = lnHorizontal.Y2 = ctrY;
            MainCanvas.Children.Add(lnHorizontal);
        }
        //private void Window_Loaded(object sender, RoutedEventArgs e)
        //{

        //}

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            DrawScene();
        }
    }
}

我的实际应用是数据驱动的,看起来像这样。井管中唯一真实的数据驱动元素是(颜色/类型)和深度:圆位置在VM中为位置计算,以避免重叠 enter image description here

1 个答案:

答案 0 :(得分:0)

通过与朋友交谈,我找到了这个问题的最终答案。克莱门斯就在附近,如果我能够使用单笔画颜色,可能会创造一个非常有效的答案。 resize事件在我的应用程序no中处理,每个子框架elelent都有一个简单的Invalidate。

  SizeChanged += (s, e) =>
        {

            WellCanvas.SetCoordinateSystem(wm.OverallStartDepth,
                                            wm.OverallEndDepth,

                                            wm.OverallEndDepth,
                                            wm.OverallStartDepth);
            **// This seemed to solve the problem**
            foreach (FrameworkElement elem in WellCanvas.Children)
                elem.InvalidateVisual();

        };