Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin 将按钮值作为命令参数传递_Xamarin_Xamarin.forms_Xamarin.android - Fatal编程技术网

Xamarin 将按钮值作为命令参数传递

Xamarin 将按钮值作为命令参数传递,xamarin,xamarin.forms,xamarin.android,Xamarin,Xamarin.forms,Xamarin.android,我有一个计算器样式的网格。如何在Xamarin表单中将按钮文本作为命令参数传递: 内容资源 <ContentPage.Resources> <ResourceDictionary> <Style x:Key="PhoneKeys" TargetType="Button"> <Setter Property="BackgroundColor" Value="White"/> &l

我有一个计算器样式的网格。如何在Xamarin表单中将按钮文本作为命令参数传递:

内容资源

<ContentPage.Resources>
    <ResourceDictionary>
        <Style x:Key="PhoneKeys" TargetType="Button">
            <Setter Property="BackgroundColor" Value="White"/>
            <Setter Property="FontSize" Value="36"/>
            <Setter Property="Command" Value="{Binding Source={x:Reference contactsListView}, Path=BindingContext.TapCommand1}"/>
            <Setter Property="CommandParameter" Value="Pass Button Text"/>
        </Style>
    </ResourceDictionary>
</ContentPage.Resources>

网格布局

<Button Style="{StaticResource PhoneKeys}" Text="1" Grid.Row="1" Grid.Column="0" />
<Button Style="{StaticResource PhoneKeys}" Text="1" Grid.Row="1" Grid.Column="0" />
<Button Style="{StaticResource PhoneKeys}" Text="2" Grid.Row="1" Grid.Column="1" />
<Button Style="{StaticResource PhoneKeys}" Text="3" Grid.Row="1" Grid.Column="2" />
<Button Style="{StaticResource PhoneKeys}" Text="4" Grid.Row="2" Grid.Column="0" />
<Button Style="{StaticResource PhoneKeys}" Text="5" Grid.Row="2" Grid.Column="1" />
<Button Style="{StaticResource PhoneKeys}" Text="6" Grid.Row="2" Grid.Column="2" />
<Button Style="{StaticResource PhoneKeys}" Text="7" Grid.Row="3" Grid.Column="0" />
<Button Style="{StaticResource PhoneKeys}" Text="8" Grid.Row="3" Grid.Column="1" />
<Button Style="{StaticResource PhoneKeys}" Text="9" Grid.Row="3" Grid.Column="2" />
<Button Style="{StaticResource PhoneKeys}" Text="*" Grid.Row="4" Grid.Column="0" />
<Button Style="{StaticResource PhoneKeys}" Text="0" Grid.Row="4" Grid.Column="1" />
<Button Style="{StaticResource PhoneKeys}" Text="#" Grid.Row="4" Grid.Column="2" />

使用此语法获取控件的属性“{x:Reference myButton}” 您可以发送自按钮引用以获取完整的按钮对象

风格

    <Setter Property="CommandParameter" Value="{x:Reference myButton}" />
或者使用每个按钮名称在每个按钮中定义命令参数的最佳方法



我认为这样做是不可能的。您必须在
按钮
本身上定义
命令参数
,或者在
按钮
后面有一些可绑定的对象,该对象具有可绑定的属性。为什么不只编写所有命令参数呢?正如您已经使用文本属性所做的那样。这将比试图想出一个如何使其具有风格的答案花费更少的时间。但我认为,如果你写在它应该通过整个按钮。如果你写,它应该传递文本
<Button x:Name="myButton" Style="{StaticResource PhoneKeys}" Text="#" Grid.Row="4" Grid.Column="2" />
public ICommand TapCommand1 => new Command((obj) =>
{
        var button = obj as Button;
        var text = button.Text;
});
        <Button x:Name="myButton" Style="{StaticResource PhoneKeys}" CommandParameter="{x:Reference myButton}" Text="#" Grid.Row="4" Grid.Column="2" />