简体   繁体   中英

.NET MAUI Button Text Concatenation

Does anyone know how to create a button in .NET MAUI where the text of the button contains multiple elements?

For example, I want to create a button where it has an icon and text, The icon is a binding to a static resource. Looking around, it looks like you can do it by using multi bindings by adding direct content to the button element in XAML, but .NET MAUI give an error stating that you cannot set direct content on a button.

I have tried adding direct content to the button, but that didn't work and other than that - I couldn't find much else to try.

Please help.

You best bet is to use either a VerticalStackLayout or a Grid including a label and the button.

<VerticalStackLayout>
    <Button/>
    <Label/>
</VerticalStackLayout>

or an example with Grid inside a border

<Border>
    <Grid>
       <Button VerticalOptions="Start"/>
       <Label VerticalOptions="End"/>
    </Grid>

    <Border.GestureRecognizers>
        <TapGestureRecognizer
             Tapped="TapGestureRecognizer_Tapped"
             Command="..."
             CommandParameter="..."/>
            
    </Border.GestureRecognizers>
</Border>

Also not that if the user taps the label and not the icon then your code will not execute. You have to include also code to your label. The same applies if the user taps the border.

hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM