question

manualoyau-4681 avatar image
0 Votes"
manualoyau-4681 asked manualoyau-4681 commented

Use foreach for form button controls

Hello,

I want to loop (foreach) through my image button or labels in a xamarin.form.

as in this example
https://docs.microsoft.com/en-us/answers/questions/463433/use-foreach-for-form-button-controls.html

Thank you
Manu

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT edited

Hello,​

Welcome to our Microsoft Q&A platform!

You can put your Controls to <StackLayout> in the ContentPage.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:sho="http://sharpnado.com" xmlns:app141="clr-namespace:App141"
             x:Class="App141.MainPage">


    <StackLayout>
        <Button Text="dddd"></Button>
        <Button Text="ddddg11"></Button>

        <app141:MyEntryHideKeyboard  x:Name="MyEntry"></app141:MyEntryHideKeyboard>
    </StackLayout>

</ContentPage>


Then we can use foreach for form button controls in the backround code.

public MainPage()
        {
            InitializeComponent();

            foreach (View i in ((StackLayout)this.Content).Children.Where(x => x.GetType() == typeof(Button))) {
                Button button= i as Button;
                
            
            }
            
        }


Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@manualoyau-4681

May I know if you have got any chance to check my answer? I am glad to help if you have any other questions

0 Votes 0 ·
manualoyau-4681 avatar image
0 Votes"
manualoyau-4681 answered manualoyau-4681 commented

Hello,
I hadn't seen your answer, sorry.
here is my work

137259-capture-decran-146.png




Merci pour votre aide
Manu


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What is root layout in your XAML? Please share your XAML code.

0 Votes 0 ·

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XamlSamples.PageCollection">

 <Grid>
     <ScrollView  >
         <FlexLayout>
             <StackLayout>
                 <Button Text="Bouton01" />
                 <Button Text="Bouton02" />
                 <Button Text="Bouton03" />
                 <Label x:Name="Nb" FontSize="Title" HorizontalTextAlignment="Center" />
             </StackLayout>
         </FlexLayout>
     </ScrollView>

</Grid>

</ContentPage>


0 Votes 0 ·