如何在UWP / WinRT

时间:2015-11-11 20:46:58

标签: c# xaml windows-runtime uwp

我需要更改UWP应用中文本的显示顺序,但遗憾的是我找不到任何直接的解决方案。

WinRT中的文本块不支持此属性,至少我无法从MSDN中找到有关此功能的任何信息。我找到了一个解决方案,我需要创建一个" New"文本块控件,它支持垂直顺序的文本显示,但解决方案是Silverlight,所以我正在研究它是否有效。

这就是textblock正常工作的方式: enter image description here

这就是我希望它工作的文本块:

enter image description here

我知道有一种方法可以设置宽度和文本包装的东西,但它只适用于某个屏幕尺寸&分辨率,这意味着在其他屏幕下文本将无法正常显示

任何提示都将不胜感激。

2 个答案:

答案 0 :(得分:3)

要在UWP中获得“真实”的垂直文本,请尝试以下操作:

<TextBlock Text="Rotated Text" 
           FontSize="18" 
           Foreground="Black">
    <TextBlock.RenderTransform>
        <RotateTransform Angle="-90" />
    </TextBlock.RenderTransform>
</TextBlock>

答案 1 :(得分:0)

编辑 - 使用用户控制的UWP版权

VerticalTextBlock - 代码隐藏

public partial class VerticalTextBlock : UserControl
{

    public VerticalTextBlock()
    {
        InitializeComponent();
    }

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text",
            typeof(string),
            typeof(VerticalTextBlock),
            new PropertyMetadata(string.Empty, textChangeHandler));

    private static void textChangeHandler(
        DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var prop = d as VerticalTextBlock;
        var textBlock = prop.TheTextBlock;
        var str = (e.NewValue as string);
        textBlock.Inlines.Clear();
        for (int i = 0; i < str.Length-1; i++)
        {
            textBlock.Inlines.Add(new Run() { Text = str[i] + Environment.NewLine });
        }
        textBlock.Inlines.Add(new Run() { Text = str[str.Length-1].ToString()});
    }
}

VerticalTextBlock - XAML

<UserControl
    ...
    >
    <TextBlock x:Name="TheTextBlock"/>
</UserControl>

用法和测试 - XAML

<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBlock x:Name="a" Text="ASD"></TextBlock>
    <local:VerticalTextBlock x:Name="b" Text="{Binding ElementName=a, Path=Text}" />
    <local:VerticalTextBlock x:Name="c" Text="{Binding ElementName=b, Path=Text}" />
    <TextBlock x:Name="d" Text="{Binding ElementName=c, Path=Text}"></TextBlock>
    <TextBlock TextAlignment="Center" HorizontalAlignment="Left">
        <Run Text="A"/>
        <LineBreak/>
        <Run Text="S"/>
        <LineBreak/>
        <Run Text="D"/>
        <LineBreak/>
        <Run Text="A"/>
        <LineBreak/>
        <Run Text="S"/>
        <LineBreak/>
        <Run Text="D"/>
    </TextBlock>
</StackPanel>

原始答案 - 没有注意到它是UWP而非WPF

你让我感兴趣,因为我在Android中只做过这个,所以有一些解决方案可行但我决定尝试自定义控件扩展TextBlock

public partial class VerticalTextBlock : TextBlock
{
    public VerticalTextBlock()
    {
        InitializeComponent();
    }

    new public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    new public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text",
            typeof(string),
            typeof(VerticalTextBlock),
            new PropertyMetadata(string.Empty, textChangeHandler));

    private static void textChangeHandler(
        DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var prop = d as VerticalTextBlock;
        var str = (e.NewValue as string);
        var inlines = str.Select(x => new Run(x + Environment.NewLine));
        prop.Inlines.Clear();
        prop.Inlines.AddRange(inlines);
    }
}

XAML中的用法

<local:VerticalTextBlock Text="AABBCCDDEEFF" />
相关问题