如何在UWP中设置TextBlock的比例

时间:2016-09-06 10:19:24

标签: xaml animation text uwp scale

当我使用Storyboard放大TextBlock时,它会在缩放时像素化,并且仅在完成时重新渲染。

<DoubleAnimationUsingKeyFrames Storyboard.TargetName="TextBlock" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleY)">
      <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1.5"/>

有没有办法每帧重新渲染TextBlock?

2 个答案:

答案 0 :(得分:2)

我找到了一个解决方案,虽然它不再是关于TextBlock,但对我来说它有用:

    private void CreateText(string text)
    {
        _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;
        CreateDevice();

        _spriteTextVisual = _compositor.CreateSpriteVisual();
        _spriteTextVisual.Size = new Vector2(512, 512);

        _drawingTextSurface = _graphicsDevice.CreateDrawingSurface(new Size(512, 512), DirectXPixelFormat.B8G8R8A8UIntNormalized, DirectXAlphaMode.Premultiplied);

        using (var ds = CanvasComposition.CreateDrawingSession(_drawingTextSurface))
        {
            ds.Clear(Colors.Transparent);
            ds.DrawText(text, new Rect(0, 0, 512, 512), Colors.Black, new CanvasTextFormat
            {
                FontSize = 32,
                FontWeight = FontWeights.Light,
                VerticalAlignment = CanvasVerticalAlignment.Top,
                HorizontalAlignment = CanvasHorizontalAlignment.Center,
                LineSpacing = 32
            });
        }

        _surfaceTextBrush = _compositor.CreateSurfaceBrush(_drawingTextSurface);

        _spriteTextVisual.Brush = _surfaceTextBrush;

        ElementCompositionPreview.SetElementChildVisual(this, _spriteTextVisual);
    }

    private void CreateDevice()
    {
        _device = CanvasDevice.GetSharedDevice();
        _device.DeviceLost += Device_DeviceLost;

        if (_graphicsDevice == null)
        {
            _graphicsDevice = CanvasComposition.CreateCompositionGraphicsDevice(_compositor, _device);
        }
        else
        {
            CanvasComposition.SetCanvasDevice(_graphicsDevice, _device);
        }
    }

    private async void Device_DeviceLost(CanvasDevice sender, object args)
    {
        _device.DeviceLost -= Device_DeviceLost;
        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, CreateDevice);
    }

只需制作最大比例尺的文本。

答案 1 :(得分:0)

这似乎是设计 - see this answer by Jerry Nixon在一个非常相似的问题上。

显然这是为了确保动画流畅,因为在每一帧完全渲染字体会很昂贵。

唯一可以解决此问题的方法是将TextBlock包裹在Viewbox元素中并缩放Viewbox的{​​{1}}属性。但是,这不会自动生效,您还必须将Height添加到EnableDependentAnimation="True"元素。不幸的是,你会发现这种方法也会导致每个帧的布局更新,因此非常不和谐。