【问题标题】:i want to pass a parameter in a xaml button我想在 xaml 按钮中传递参数
【发布时间】:2021-03-03 11:14:49
【问题描述】:

你好,我想通过 xaml 中的按钮传递标签 它是一个列表视图,每一行都有她自己的标签,我想通过标签更新每一行

    <Label Grid.Column="2"
                                             Grid.Row="1"
                                             Text="{Binding Label}" 
                                             FontSize="20"
                                             TextColor="Black"
                                             Margin="0,10,0,0"
                                             FontFamily="{StaticResource font}"
                                             HorizontalOptions="Start"
                                             VerticalOptions="Center"/>

                                    <ImageButton Grid.Column="2"
                                             Grid.Row="0"
                                             HorizontalOptions="End"
                                             VerticalOptions="End"
                                             Source="plus.png"
                                             Margin="9"
                                            clicked ="update"
                                            BackgroundColor="Transparent"/>

以及类中的函数

public void update (object sender, EventArgs e) { 按标签更新 }

我知道有一些命令,但我是初学者,我不知道如何使用它我试过这个但我没有工作。

Command="{Binding Path=BindingContext.UpdateCommand, Source={x:Reference Name=listViewEvent}}"  
                                   CommandParameter="{Binding Label}"

功能是这样的

        UpdateCommand = new Command<string>(async (args) => {
            String Label = args as string;
             DisplayAlert("update", "update", "OK");
            _ = Navigation.PushAsync(new AddReminder(Label));
        });

并且警报没有显示 请问我需要帮助

【问题讨论】:

  • 点击按钮时是否要更新标签文本的值?
  • 不,我想通过标签更新sqlite中的对象
  • 我们可以为模型添加一些属性并将它们绑定到标签上。通过这种方式,我们可以获取 ViewModel 中的属性。在 MVVM 中传递元素并不是一个好的设计。

标签: xamarin xamarin.forms xamarin.android xamarin.ios


【解决方案1】:

你不需要“传递”Label

<ImageButton Grid.Column="2" Cilcked="update" ... />

protected void update(object sender, EventArgs a)
{
   var btn = (ImageButton)sender;

   // MyClassName should be the name of your model class
   // this will get you the object referenced by the selected row
   var item = (MyClassName)btn.BindingContext;
   // then you can reference any property of item
   // ie, item.Label, etc

}

【讨论】:

    【解决方案2】:
    <Label x:Name="myLabel"
           Grid.Column="2"
           Grid.Row="1"
           Text="{Binding Label}" 
           FontSize="20"
           TextColor="Black"
           Margin="0,10,0,0"
           FontFamily="{StaticResource font}"
           HorizontalOptions="Start"
           VerticalOptions="Center"/>
    
    <ImageButton Grid.Column="2"
                 Grid.Row="0"
                 HorizontalOptions="End"
                 VerticalOptions="End"
                 Source="plus.png"
                 Margin="9"
                 clicked ="update"
                 BackgroundColor="Transparent"
                 CommandParameter={x:Reference myLabel}/>
    
    

    然后在后面的代码中你可以得到标签

    protected void update(object sender, EventArgs a)
    {
       var btn = (ImageButton)sender;
       var label = btn.CommandParameter as Xamarin.Forms.Label;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-27
      • 2021-02-19
      • 2015-02-10
      • 1970-01-01
      • 2011-04-12
      • 1970-01-01
      • 2011-06-15
      • 2011-12-30
      相关资源
      最近更新 更多