行在启动时不显示,但在调整大小(WPF)时变为可见 [英] Line is not displayed on startup but becomes visible on resize (WPF)

查看:131
本文介绍了行在启动时不显示,但在调整大小(WPF)时变为可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序,其中仅在调整应用程序窗口的大小时才显示线条形状,如下所示:

I have a WPF application where a line shape is only displayed when the application window is resized, as shown here:

红线是即时绘制的,未在XAML中定义.它显示正常.

The red line is drawn on-the-fly, it is not defined in XAML. It displays fine.

蓝线是在XAML中定义的,但是其尺寸在OnRender()调用中已更改.如果未更改其尺寸,或者仅更改了一些尺寸,则在启动时会显示蓝线.但是,更改所有时,蓝线的尺寸将在启动时不显示.仅在调整应用程序大小时显示.

The blue line is defined in XAML, but its dimensions are changed within the OnRender() call. If its dimensions are not changed, or if only some of its dimensions are changed, the blue line is displayed on startup. But when all the dimensions of the blue line are changed, it is not displayed on startup. It is only displayed on application resize.

这是XAML代码:

<Window x:Class="canvas_line.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:canvas_line" 
        Title="MainWindow" Height="284" Width="228">
    <Canvas Name="canvas">
        <local:show_line />
        <Line Name="my_line" Stroke="Blue" 
            X1="50" Y1="40" X2="50" Y2="80" StrokeThickness="4" />
    </Canvas>
</Window>

以及C#代码隐藏文件:

And the C# code-behind file:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Linq;

namespace canvas_line
{
    public partial class show_line: FrameworkElement
    {
        protected override void OnRender(DrawingContext dc)
        {
            base.OnRender(dc);              // Good practice.

            // Draw a brand-new red line defined here only 
            // (not defined in XAML).

            Pen dp = new Pen(Brushes.Red, 4);
            dc.DrawLine(dp, new Point(100, 150), new Point(100, 200));

            // Now modify coordinates of the blue line defined in XAML.

            Canvas cp = (Canvas)this.Parent;
            var my_line = cp.Children.OfType<Line>().FirstOrDefault();
            my_line.X1 = 100;
            my_line.Y1 = 20;
            my_line.X2 = 100;
            my_line.Y2 = 50;

            // Problem: The blue line is not displayed unless
            // the application window is resized.
        }
    }
}

整个项目的zip文件在这里(项目中的代码很少,我将其修剪到演示问题所需的绝对最低限度):

The zip file for the entire project is here (there is very little code in the project, I have pruned it to the absolute minimum necessary to demo the problem):

https://anonfiles.com/file/18c6edce296927b43ed0b0d595574a80

我尝试了对UpdateLayout()InvalidateVisual()的各种调用组合,但都没有效果.为什么会发生这种情况,并且有解决方法吗?我的最终目标是能够对蓝线进行动画处理,使其闪烁.

I have tried all sorts of combinations of calls to UpdateLayout() and InvalidateVisual(), to no effect. Why is this happening, and is there a workaround? My ultimate goal is to be able to animate the blue line so as to have it blink.

我在Windows 7,Visual Studio 10,.NET 4.0客户端配置文件中.

I am on Windows 7, Visual Studio 10, .NET 4.0 Client Profile.

推荐答案

如果这样放置,它将正确绘制

if you put it like this it will draw properly

 <Canvas Name="canvas">
    <Line Name="my_line" Stroke="Blue" X1="50" Y1="40" X2="50" Y2="80" StrokeThickness="4" />
    <local:show_line />
 </Canvas>  

这篇关于行在启动时不显示,但在调整大小(WPF)时变为可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆