C# 如何在xamarin表单中将按钮用作stacklayout,以便对其进行自定义并处理文本位置和图像位置?
如何在xamarin表单中将按钮用作stacklayout,以便对其进行自定义并处理文本位置和图像位置?大概是这样的:C# 如何在xamarin表单中将按钮用作stacklayout,以便对其进行自定义并处理文本位置和图像位置?,c#,xaml,xamarin,xamarin.forms,C#,Xaml,Xamarin,Xamarin.forms,如何在xamarin表单中将按钮用作stacklayout,以便对其进行自定义并处理文本位置和图像位置?大概是这样的: void Button_Tapped(object sender, System.EventArgs e) { //Functionality Here } 您可以使用自定义渲染器设置按钮的内容。您可以将手势识别器放置在堆栈布局中,就像它是按钮一样,例如: <StackLayout BackgroundColor="AliceBlue"> &
void Button_Tapped(object sender, System.EventArgs e)
{
//Functionality Here
}
您可以使用自定义渲染器设置按钮的内容。您可以将手势识别器放置在堆栈布局中,就像它是按钮一样,例如:
<StackLayout BackgroundColor="AliceBlue">
<Image HeightRequest="35" Source="ButtonImage"/>
<Label Text="ButtonText" FontSize="10" HorizontalOptions="Center" />
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="Button_Tapped"/>
</StackLayout.GestureRecognizers>
</StackLayout>
见答案。已创建自定义控件,该控件是包含以下内容的按钮:
public class ContentButton:ContentView
{
private readonly TapGestureRecognizer _tapGestureRecognizer;
public ContentButton()
{
_tapGestureRecognizer = new TapGestureRecognizer();
GestureRecognizers.Add(_tapGestureRecognizer);
}
protected override void OnChildAdded(Element child)
{
base.OnChildAdded(child);
if (child is View childview)
{
childview.GestureRecognizers.Add(_tapGestureRecognizer);
}
}
public static readonly BindableProperty CommandProperty = BindableProperty.Create(nameof(Command), typeof(ICommand),
typeof(ContentButton), null, BindingMode.Default, null, CommandPropertyChanged);
private static void CommandPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
if (newValue is ICommand command && bindable is ContentButton contentButton)
{
contentButton._tapGestureRecognizer.Command = command;
}
}
public ICommand Command
{
get => (ICommand)GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}
}
为什么要使用stacklayout作为按钮您可以设置文本位置和all-in按钮,因为其中有水平选项和垂直选项的属性。我看到的不同之处是边框,您可以使用此属性。所以有什么比你想要的这个按钮更不同?我可以设置两个文本行吗?一个是固定的,另一个是可绑定的?以及如何设置位置或填充?我不希望堆栈布局作为按钮,但我希望像堆栈布局一样自由设置文本和图像位置。这就是我的意思。我非常确定,您可以将堆栈布局放在按钮中,并将所有图像和文本放在堆栈布局中。或者,事实上,您可以使用任何您想要的面板关于TabIndex和焦点事件的内容是什么?