سؤال

I am working on a countdown control for a WPF application. The following xaml demonstrates what I am after:

<Canvas>
    <Canvas>
        <Ellipse Width="110" Height="110" Fill="Black" Canvas.Left="95" Canvas.Top="95" />
        <Ellipse Width="100" Height="100" Fill="Red" Canvas.Left="100" Canvas.Top="100" />
        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="72" Canvas.Left="131" Canvas.Top="100">3</TextBlock>
    </Canvas>
    <Canvas>
        <Path Stroke="Blue" Fill="White">
            <Path.Data>
                <PathGeometry>
                    <PathFigure StartPoint="150,150">
                        <LineSegment Point="200,150" />
                        <ArcSegment x:Name="arc1" Point="200,150" Size="50,50" SweepDirection="Clockwise" />
                        <ArcSegment x:Name="arc2" Point="200,150" Size="50,50" SweepDirection="Clockwise" />
                        <LineSegment Point="150,150" />
                    </PathFigure>
                </PathGeometry>
            </Path.Data>
            <Path.Triggers>
                <EventTrigger RoutedEvent="Path.Loaded">
                    <BeginStoryboard>
                        <Storyboard Duration="0:0:1" Storyboard.TargetProperty="Point" RepeatBehavior="Forever">
                            <PointAnimationUsingPath Duration="0:0:0.5" Storyboard.TargetName="arc1">
                                <PointAnimationUsingPath.PathGeometry>
                                    <PathGeometry>
                                        <PathFigure StartPoint="200,150">
                                            <ArcSegment Size="50,50" Point="100,150" SweepDirection="Clockwise" />
                                        </PathFigure>
                                    </PathGeometry>
                                </PointAnimationUsingPath.PathGeometry>
                            </PointAnimationUsingPath>
                            <PointAnimationUsingPath Duration="0:0:0.5" Storyboard.TargetName="arc2">
                                <PointAnimationUsingPath.PathGeometry>
                                    <PathGeometry>
                                        <PathFigure StartPoint="200,150">
                                            <ArcSegment Size="50,50" Point="100,150" SweepDirection="Clockwise" />
                                        </PathFigure>
                                    </PathGeometry>
                                </PointAnimationUsingPath.PathGeometry>
                            </PointAnimationUsingPath>
                            <PointAnimationUsingPath BeginTime="0:0:0.5" Duration="0:0:0.5" Storyboard.TargetName="arc2">
                                <PointAnimationUsingPath.PathGeometry>
                                    <PathGeometry>
                                        <PathFigure StartPoint="100,150">
                                            <ArcSegment Size="50,50" Point="200,150" SweepDirection="Clockwise" />
                                        </PathFigure>
                                    </PathGeometry>
                                </PointAnimationUsingPath.PathGeometry>
                            </PointAnimationUsingPath>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Path.Triggers>
        </Path>          
    </Canvas>
</Canvas>

Now, instead of making the whole thing white, I would like to "reveal" the next number in the countdown. I thought using the OpacityMask would work, but when I tried that things got weird. Here is the xaml I am working on:

<Canvas>
    <Canvas>
        <Ellipse Width="110" Height="110" Fill="Black" Canvas.Left="95" Canvas.Top="95" />
        <Ellipse Width="100" Height="100" Fill="Red" Canvas.Left="100" Canvas.Top="100" />
        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="72" Canvas.Left="131" Canvas.Top="100">3</TextBlock>
    </Canvas>
    <Canvas>
        <Ellipse Width="110" Height="110" Fill="Black" Canvas.Left="95" Canvas.Top="95" />
        <Ellipse Width="100" Height="100" Fill="Yellow" Canvas.Left="100" Canvas.Top="100" />
        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="72" Canvas.Left="131" Canvas.Top="100">2</TextBlock>
        <Canvas.OpacityMask>
            <VisualBrush>
                <VisualBrush.Visual>
                    <Path Stroke="Blue" Fill="White">
                        <Path.Data>
                            <PathGeometry>
                                <PathFigure StartPoint="150,150">
                                    <LineSegment Point="200,150" />
                                    <ArcSegment x:Name="arc1" Point="200,150" Size="50,50" SweepDirection="Clockwise" />
                                    <ArcSegment x:Name="arc2" Point="200,150" Size="50,50" SweepDirection="Clockwise" />
                                    <LineSegment Point="150,150" />
                                </PathFigure>
                            </PathGeometry>
                        </Path.Data>
                        <Path.Triggers>
                            <EventTrigger RoutedEvent="Path.Loaded">
                                <BeginStoryboard>
                                    <Storyboard Duration="0:0:10" Storyboard.TargetProperty="Point" RepeatBehavior="Forever">
                                        <PointAnimationUsingPath Duration="0:0:5" Storyboard.TargetName="arc1">
                                            <PointAnimationUsingPath.PathGeometry>
                                                <PathGeometry>
                                                    <PathFigure StartPoint="200,150">
                                                        <ArcSegment Size="50,50" Point="100,150" SweepDirection="Clockwise" />
                                                    </PathFigure>
                                                </PathGeometry>
                                            </PointAnimationUsingPath.PathGeometry>
                                        </PointAnimationUsingPath>
                                        <PointAnimationUsingPath Duration="0:0:5" Storyboard.TargetName="arc2">
                                            <PointAnimationUsingPath.PathGeometry>
                                                <PathGeometry>
                                                    <PathFigure StartPoint="200,150">
                                                        <ArcSegment Size="50,50" Point="100,150" SweepDirection="Clockwise" />
                                                    </PathFigure>
                                                </PathGeometry>
                                            </PointAnimationUsingPath.PathGeometry>
                                        </PointAnimationUsingPath>
                                        <PointAnimationUsingPath BeginTime="0:0:5" Duration="0:0:5" Storyboard.TargetName="arc2">
                                            <PointAnimationUsingPath.PathGeometry>
                                                <PathGeometry>
                                                    <PathFigure StartPoint="100,150">
                                                        <ArcSegment Size="50,50" Point="200,150" SweepDirection="Clockwise" />
                                                    </PathFigure>
                                                </PathGeometry>
                                            </PointAnimationUsingPath.PathGeometry>
                                        </PointAnimationUsingPath>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                        </Path.Triggers>
                    </Path>
                </VisualBrush.Visual>
            </VisualBrush>
        </Canvas.OpacityMask>                      
    </Canvas>
</Canvas>

I am not sure how to describe what is happening, or why it is behaving in this way, but it is not what I would like nor what I expected. Is there something simple I am missing? Or can anyone offer an alternative approach that will accomplish what I want?

هل كانت مفيدة؟

المحلول

I found the issue. It was related to the viewport/viewbox of the visual brush I was using to create the opacity mask. Here is the relevant change:

<Canvas.OpacityMask>
            <VisualBrush Viewbox="0,0,300,300" ViewboxUnits="Absolute" Viewport="0,0,300,300" ViewportUnits="Absolute"> ...

I am not an expert in WPF by any means, but my understanding of what was going wrong is as follows. The default viewport of a brush starts at the top-left of the bounding container. In my case the bounding container was the path I was animating. During the animation the top-left of the container was changing. The different segments making up the path were defined relative to this changing top-left point. Basically, they were being drawn relative to a moving target. The viewbox settings are there to indicate that we want to work with the entire original visual with the same coordinate system it uses.

Anyone with a better understanding of what was going on who can give a clearer explanation, feel free to edit this answer or add a comment.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top