0

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"));
                            }

                           },
                        }
                    },

4 Answers 4

1

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"));})

                          },
Sign up to request clarification or add additional context in comments.

Comments

0

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" });

Comments

0

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,


                }
            }
        };

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.