我正在尝试将自定义内容添加到 Xamarin Forms 中的按钮。
默认情况下,按钮是这样创建的:
<Button d:DataContext="{d:DesignInstance viewModel:AssessmentItemCategory}"
Clicked="Button_OnClicked"
Style="{StaticResource CategoryButtonStyle}"
Text={Binding Text} />
但我想创建此按钮的自定义内容。通常使用 WPF 我会这样做:
<Button d:DataContext="{d:DesignInstance viewModel:AssessmentItemCategory}"
Clicked="Button_OnClicked"
Style="{StaticResource CategoryButtonStyle}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<Label Text="{Binding Text}" Grid.Column="0" TextColor="Black"/>
<Label Text="-" Grid.Column="1" TextColor="Black"/>
</Grid>
</Button>
但这不起作用。
我也在寻找 DataTemplate 属性,但还没有找到。
如何在 Xamarin.Forms 中做到这一点?
谢谢保罗,
我创建了自己的 UserControl 来处理这个问题
这里是:
public partial class ContentButton : ContentView
{
public ContentButton()
{
InitializeComponent();
}
public event EventHandler Tapped;
public static readonly BindableProperty CommandProperty = BindableProperty.Create<ContentButton, ICommand>(c => c.Command, null);
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
private void TapGestureRecognizer_OnTapped(object sender, EventArgs e)
{
if(Tapped != null)
Tapped(this,new EventArgs());
}
}
并查看代码:
<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="RFA.Wireframes.Controls.ContentButton"
x:Name="ContentButtonView">
<ContentView.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_OnTapped" Command="{Binding Source={x:Reference ContentButtonView}, Path=Command}"></TapGestureRecognizer>
</ContentView.GestureRecognizers>
</ContentView>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)