简体   繁体   中英

How do I put a functioning button in a StackLayout in Xamarin Forms?

I have googled around, and posted on the Xamarin Forums, and asked on Slack, but I have not figured out how to do this. I have also looked at a bunch of Stackoverflow posts.

I am trying to put a button in a StackLayout that is in a ScrollView.The favicon image is just a test image. I know it has more space than it needs. I'm doing this all in C#.

                    new StackLayout
                    {
                        Padding = 0,
                        Orientation = StackOrientation.Horizontal,
                        Children =
                        {
                            new Label
                            {
                                Text = "• ",
                                TextColor = Color.Black,
                            },
                            new Label
                            {
                                FontSize = 16,
                                Text = "Follow Surviving Sepsis Guidelines: ",
                                TextColor = Color.Black,
                                HorizontalOptions = LayoutOptions.Start
                            },

                          new WebView
                           {
                                HeightRequest = 100, WidthRequest = 100, Source = "https://developer.android.com/favicon.ico",
                           },

                          new Button
                          {

                            Text = "Surviving Sepsis Guidelines",

                            // Does not compile. "Invalid initializer member declarator"
                            Clicked += (sender,e) =>
                            {
                             Device.OpenUri(new Uri("http://www.sccm.org/Documents/SSC-Guidelines.pdf"));
                            }

                           },
                        }
                    },

This is the answer. Use "Command" not "Click."

Xamarin Forms Button OnClick

new Button {
    Text = "Add parameters"
    Command = new Command(() => {
        //Do something
    })
};

                          new Button
                          {

                            Text = "Surviving Sepsis Guidelines",


                            Command = new Command(() => {Device.OpenUri(new Uri("http://www.sccm.org/Documents/SSC-Guidelines.pdf"));})

                          },

I am not sure if I am understanding correct but is that what you are trying to do

Xaml:

<ContentPage.Content>
    <ScrollView>
        <StackLayout>
           <Button Text="Click me"/>            
        </StackLayout>
    </ScrollView>
</ContentPage.Content>

C#:

var scroll = new ScrollView();
Content = scroll;
var stack = new StackLayout();
stack.Children.Add(new Button{ Text="Click Me" });

the way i do it in xamarin.forms is like this

   var buttonnew = new Button
        {
            Text = "Click Me!",
            BackgroundColor = Color.FromHex("FF5A5F"),
            FontSize = 24                
        };


   buttonnew.Clicked += async (sender, args) =>
        {
            buttonnew.IsEnabled = false;
            // do your onclick process here

        };

     MainPage = new ContentPage
        {         
            BackgroundColor = Color.FromHex("BFD7EA"),
            Content = new StackLayout
            {
                VerticalOptions = LayoutOptions.Center,
                Children = {

                    buttonnew,


                }
            }
        };

This is how you can use StackLayout in a ScrollView.

<ScrollView BackgroundColor="Teal">
  <StackLayout Spacing="5"
               Padding="30"
               WidthRequest="400"
               HorizontalOptions="Center"
               VerticalOptions="CenterAndExpand"
               BackgroundColor="Transparent">
       <Label Text="Test" HorizontalOptions="Center"/>
       <StackLayout HorizontalOptions="Center">
           <Label Text="Test"/>
       </StackLayout>
  </StackLayout>
</ScrollView>

For more information please see this Xamarin documentation , there are plenty of sample code here. Hope it 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