2017-02-06 31 views
0

我搜索了一下,发布在Xamarin论坛上,并在Slack上提问,但我还没有想出如何做到这一点。我也看了一堆Stackoverflow的帖子。如何在Xamarin Forms的StackLayout中放置一个正常运行的按钮?

我试图把一个按钮放在一个ScrollView.The图标的StackLayout只是一个测试图像。我知道它有比它需要更多的空间。我在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")); 
          } 

          }, 
         } 
        }, 

回答

1

这是回答。使用“命令”而不是“点击”。

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

          }, 
0

我不知道如果我理解正确的,但是,你正在尝试做的

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

我做在xamarin.forms的方式是这样的

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, 


       } 
      } 
     }; 
0

这是你如何可以在滚动型使用StackLayout。

<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> 

欲了解更多信息,请参阅this Xamarin documentation,这里还有大量的示例代码。希望能帮助到你。

相关问题