单词不包装?

发布于 2025-02-11 18:55:08 字数 1226 浏览 1 评论 0原文

我目前已经将以下属性设置为一个按钮,并且我希望它可以包含文字包装。我希望文本动态地安装在按钮内,但仍未从按钮上运行,但它仍在按钮...有什么想法吗?

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="quiz_app.Views.StudyPage"
             Title="Study">
    <StackLayout BackgroundColor="White"
                 Padding="100"
                 HorizontalOptions="CenterAndExpand"
                 VerticalOptions="CenterAndExpand">
        <VerticalStackLayout>
            <Button
                 Text="How much wood could a woodchuck chuck if a woodchuck could chuck wood?"
                 FontSize="40"
                 TextColor="Black"
                 FontFamily="Helvetica"
                 BackgroundColor="#CCFFCC"
                 VerticalOptions="CenterAndExpand" 
                 HorizontalOptions="CenterAndExpand"
                 BorderWidth="5"
                 HeightRequest="400"
                 BorderColor="#82D682"
                 LineBreakMode='WordWrap'>
            </Button>
        </VerticalStackLayout>
    </StackLayout>
</ContentPage>

I have currently set the following properties to a button, and I want it to word wrap. I want the text to fit inside the button dynamically and not run off the button but it's still running off the button... any ideas?

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="quiz_app.Views.StudyPage"
             Title="Study">
    <StackLayout BackgroundColor="White"
                 Padding="100"
                 HorizontalOptions="CenterAndExpand"
                 VerticalOptions="CenterAndExpand">
        <VerticalStackLayout>
            <Button
                 Text="How much wood could a woodchuck chuck if a woodchuck could chuck wood?"
                 FontSize="40"
                 TextColor="Black"
                 FontFamily="Helvetica"
                 BackgroundColor="#CCFFCC"
                 VerticalOptions="CenterAndExpand" 
                 HorizontalOptions="CenterAndExpand"
                 BorderWidth="5"
                 HeightRequest="400"
                 BorderColor="#82D682"
                 LineBreakMode='WordWrap'>
            </Button>
        </VerticalStackLayout>
    </StackLayout>
</ContentPage>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

想你的星星会说话 2025-02-18 18:55:09

谢谢@ewerspej的选择,但由于某种原因,我无法让GesturerCognizer工作,因为它没有将其视为成员,所以我只是在边境内部包装了一个标签,这做了我想做的事情。

下面编辑的代码:

<Border>
    <Label
        Text="How much wood could a woodchuck chuck if a woodchuck could chuck wood?"
        FontSize="40"
        TextColor="Black"
        FontFamily="Helvetica"
        BackgroundColor="#CCFFCC"
        VerticalOptions="CenterAndExpand" 
        HorizontalOptions="CenterAndExpand"
        HeightRequest="400"
        LineBreakMode='WordWrap'>
    </Label>
</Border>

Thank you @ewerspej for the alternative but I could not get the GestureRecognizer to work for some reason it wasn't recognizing it as a member so I just wrapped a label inside of a border and that did what I wanted to do.

Edited code below:

<Border>
    <Label
        Text="How much wood could a woodchuck chuck if a woodchuck could chuck wood?"
        FontSize="40"
        TextColor="Black"
        FontFamily="Helvetica"
        BackgroundColor="#CCFFCC"
        VerticalOptions="CenterAndExpand" 
        HorizontalOptions="CenterAndExpand"
        HeightRequest="400"
        LineBreakMode='WordWrap'>
    </Label>
</Border>
世态炎凉 2025-02-18 18:55:09

如果您参考 https://github.com/dotnet/ MAUI/essugy/9102 您会看到按钮中的LineBreakMode已“固定”。但是有副作用,看来文本包装不完全居中:

实际:

“

期望:

“

我举起一个讨论论坛github.com/dotnet/maui/discussions/27592“ rel =“ nofollow noreferrer”> https://github.com/dotnet/maui/discussions/27592 讨论这一点。

我一直在寻找解决方法,我注意到这里的其他答案选择不使用button,而是border+Label带有tapgesturererecognizer。这种解决方法的缺点是丢失了可访问的键盘转换。

为了保持两全其美,我已经捣碎了网格+边框+标签+按钮。正确的视觉表示由border+标签提供。与无障碍键盘快捷键的正确用户交互由Invisible 按钮提供。 网格将两者捣碎,以便border+标签button都是几何大小和位置。

<Grid HorizontalOptions="Center" WidthRequest="250">
    <Border
        Padding="10,10,10,10"
        BackgroundColor="{Binding BackgroundColor, x:DataType=Button, Source={Reference myButton}}"
        Stroke="{Binding BorderColor, x:DataType=Button, Source={Reference myButton}}"
        StrokeShape="RoundRectangle 10,10,10,10">
        <Label
            HorizontalTextAlignment="Center"
            Text="{Binding Text, x:DataType=Button, Source={Reference myButton}}"
            TextColor="{Binding TextColor, x:DataType=Button, Source={Reference myButton}}"
            VerticalOptions="Center" />
    </Border>
    <Button
        x:Name="myButton"
        BackgroundColor="#CCFFCC"
        BorderColor="#82D682"
        BorderWidth="5"
        FontFamily="Helvetica"
        HorizontalOptions="CenterAndExpand"
        LineBreakMode="WordWrap"
        Opacity="0"
        Text="How much wood could a woodchuck chuck if a woodchuck could chuck wood?"
        TextColor="Black"
        VerticalOptions="CenterAndExpand" />
</Grid>

The status of this issue has changed, if you refer to https://github.com/dotnet/maui/issues/9102 you will see that LineBreakMode in Button has been "fixed". There is a side effect though, it appears that the text is wrapped not perfectly centered:

Actual:

WoodChuck1.png

Expected:

WoodChuck2.png

I raise a discussion forum https://github.com/dotnet/maui/discussions/27592 to discuss this.

I have been looking at workarounds and I noticed the other answers here have opted not to use Button but a Border+Label with a TapGestureRecognizer. The downside of this workaround is the loss of accessible keyboard-shortcuts.

To keep the best of both worlds, I have mashed up Grid+Border+Label+Button. The correct visual representation is provided by Border+Label. The correct user interaction with accessible keyboard shortcuts is supplied by an invisible Button. The Grid mashes up the two so that both the Border+Label and the Button are the same geometric size and position.

<Grid HorizontalOptions="Center" WidthRequest="250">
    <Border
        Padding="10,10,10,10"
        BackgroundColor="{Binding BackgroundColor, x:DataType=Button, Source={Reference myButton}}"
        Stroke="{Binding BorderColor, x:DataType=Button, Source={Reference myButton}}"
        StrokeShape="RoundRectangle 10,10,10,10">
        <Label
            HorizontalTextAlignment="Center"
            Text="{Binding Text, x:DataType=Button, Source={Reference myButton}}"
            TextColor="{Binding TextColor, x:DataType=Button, Source={Reference myButton}}"
            VerticalOptions="Center" />
    </Border>
    <Button
        x:Name="myButton"
        BackgroundColor="#CCFFCC"
        BorderColor="#82D682"
        BorderWidth="5"
        FontFamily="Helvetica"
        HorizontalOptions="CenterAndExpand"
        LineBreakMode="WordWrap"
        Opacity="0"
        Text="How much wood could a woodchuck chuck if a woodchuck could chuck wood?"
        TextColor="Black"
        VerticalOptions="CenterAndExpand" />
</Grid>
南巷近海 2025-02-18 18:55:08

只是为了完整的清酒:

使用tapgestureRecognizer的可能解决方案可以如下:

<Label
    Text="How much wood could a woodchuck chuck if a woodchuck could chuck wood?"
    FontSize="40"
    TextColor="Black"
    FontFamily="Helvetica"
    BackgroundColor="#CCFFCC"
    VerticalOptions="CenterAndExpand" 
    HorizontalOptions="CenterAndExpand"
    HeightRequest="400"
    LineBreakMode='WordWrap'>
    <Label.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding SomeCommand}" />
    </Label.GestureRecognizers>
<Label>

Just for completeness sake:

A possible solution using a TapGestureRecognizer could look as follows:

<Label
    Text="How much wood could a woodchuck chuck if a woodchuck could chuck wood?"
    FontSize="40"
    TextColor="Black"
    FontFamily="Helvetica"
    BackgroundColor="#CCFFCC"
    VerticalOptions="CenterAndExpand" 
    HorizontalOptions="CenterAndExpand"
    HeightRequest="400"
    LineBreakMode='WordWrap'>
    <Label.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding SomeCommand}" />
    </Label.GestureRecognizers>
<Label>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文