如何在 Xamarin Forms 的 StackLayout 中放置一个功能按钮?
How do I put a functioning button in a StackLayout in Xamarin Forms?
我用谷歌搜索,在 Xamarin 论坛上发帖,在 Slack 上提问,但我还没有弄清楚如何做到这一点。我还查看了一堆 Whosebug 帖子。
我试图在 StackLayout 中放置一个按钮,该按钮位于 ScrollView.The 图标图像只是一个测试图像。我知道它 space 比它需要的多。我正在用 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"));
}
},
}
},
我不确定我的理解是否正确,但这就是你想要做的
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" });
这就是答案。使用 "Command" 而不是 "Click."
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"));})
},
我在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,
}
}
};
这是在 ScrollView 中使用 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,这里有大量示例代码。希望对你有帮助。
我用谷歌搜索,在 Xamarin 论坛上发帖,在 Slack 上提问,但我还没有弄清楚如何做到这一点。我还查看了一堆 Whosebug 帖子。
我试图在 StackLayout 中放置一个按钮,该按钮位于 ScrollView.The 图标图像只是一个测试图像。我知道它 space 比它需要的多。我正在用 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"));
}
},
}
},
我不确定我的理解是否正确,但这就是你想要做的
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" });
这就是答案。使用 "Command" 而不是 "Click."
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"));})
},
我在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,
}
}
};
这是在 ScrollView 中使用 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,这里有大量示例代码。希望对你有帮助。