C# 单击按钮时动态发送id xamarin
我有一个动态生成的列表,显示发票编号和发票日期,还有一个“更多”按钮,当按下该按钮时,用户将深入查看发票 我正在努力确定如何绑定id,然后单击“更多”按钮发送id 我的代码:C# 单击按钮时动态发送id xamarin,c#,android,xamarin,C#,Android,Xamarin,我有一个动态生成的列表,显示发票编号和发票日期,还有一个“更多”按钮,当按下该按钮时,用户将深入查看发票 我正在努力确定如何绑定id,然后单击“更多”按钮发送id 我的代码: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App1.LoggedIn">
<ContentPage.Content>
<StackLayout Padding="10">
<Label x:Name="header" FontSize="25" Text="Invoices"
HorizontalOptions="Center" VerticalOptions="CenterAndExpand"
/>
<ListView x:Name="BillView" ItemsSource="{Binding Bills}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Label YAlign="Center" XAlign="Center" Grid.Column="0" Text="{Binding InvoiceNumber}" />
<Label YAlign="Center" XAlign="Center" Grid.Column="1" Text="{Binding InvoiceDate}" />
<Button Grid.Column="2" Text="More" VerticalOptions="StartAndExpand" Clicked="GetInvoiceDetails(HELP!!)" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
尝试将命令设置为按钮,而不是单击事件
<Button Grid.Column="2" BindingContext="{Binding Source={x:Reference BillView}, Path=BindingContext}" Command="{Binding YOUCOMMAND}" CommandParameter="{Binding Source={x:Reference Item}, Path=BindingContext}"></Button>
由于您使用了列表视图,并且您的命令位于DataTemplate中,所以绑定将附加到ItemSource中每个单独模型的绑定上下文。若您使用Mvvm,则可以单击按钮传输模型 您可以使用此命令来实现它
<Button Grid.Column="2" Text="More" VerticalOptions="StartAndExpand" Command="{ Binding BindingContext.GetIdCommand, Source={x:Reference Name=BillView} }" CommandParameter="{Binding .}" />
这是MyBillViewModel的
public class MyBillViewModel
{
public ICommand GetIdCommand { protected set; get; }
public ObservableCollection<Bill> Bills { get; set; }
public INavigation Navigation { get; set; }
public MyBillViewModel(INavigation navigation)
{
this.Navigation = navigation;
Bills = new ObservableCollection<Bill>();
Bills.Add(new Bill { InvoiceDate = "2020/2/23", InvoiceNumber = "1" });
Bills.Add(new Bill { InvoiceDate = "2020/2/23", InvoiceNumber = "2" });
Bills.Add(new Bill { InvoiceDate = "2020/2/23", InvoiceNumber = "3" });
Bills.Add(new Bill { InvoiceDate = "2020/2/23", InvoiceNumber = "4" });
Bills.Add(new Bill { InvoiceDate = "2020/2/23", InvoiceNumber = "5" });
Bills.Add(new Bill { InvoiceDate = "2020/2/23", InvoiceNumber = "6" });
Bills.Add(new Bill { InvoiceDate = "2020/2/23", InvoiceNumber = "7" });
GetIdCommand = new Command<Bill>(async (key) => {
Bill bill=(Bill)key;
await Navigation.PushAsync(new Page2(bill));
});
}
}
这是我的跑步记录
我将我的演示更新到github,您可以参考它
不要单击事件,而是尝试command
event当我放置断点时,它似乎没有点击命令。在哪里添加断点?没有断点,工作正常吗?我将断点放在YOUCOMMAND函数中,它永远不会被击中。让我自己再试一次,你能将演示更新到github吗?
<StackLayout Padding="10">
<Label x:Name="header" FontSize="25" Text="Invoices"
HorizontalOptions="Center" VerticalOptions="CenterAndExpand"
/>
<ListView x:Name="BillView" ItemsSource="{Binding Bills}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Label YAlign="Center" x:Name="MyInvoiceNumber" XAlign="Center" Grid.Column="0" Text="{Binding InvoiceNumber}" />
<Label YAlign="Center" XAlign="Center" Grid.Column="1" Text="{Binding InvoiceDate}" />
<Button Grid.Column="2" Text="More" VerticalOptions="StartAndExpand" Command="{ Binding BindingContext.GetIdCommand, Source={x:Reference Name=BillView} }" CommandParameter="{Binding .}" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new MyBillViewModel(Navigation);
}
}
public class MyBillViewModel
{
public ICommand GetIdCommand { protected set; get; }
public ObservableCollection<Bill> Bills { get; set; }
public INavigation Navigation { get; set; }
public MyBillViewModel(INavigation navigation)
{
this.Navigation = navigation;
Bills = new ObservableCollection<Bill>();
Bills.Add(new Bill { InvoiceDate = "2020/2/23", InvoiceNumber = "1" });
Bills.Add(new Bill { InvoiceDate = "2020/2/23", InvoiceNumber = "2" });
Bills.Add(new Bill { InvoiceDate = "2020/2/23", InvoiceNumber = "3" });
Bills.Add(new Bill { InvoiceDate = "2020/2/23", InvoiceNumber = "4" });
Bills.Add(new Bill { InvoiceDate = "2020/2/23", InvoiceNumber = "5" });
Bills.Add(new Bill { InvoiceDate = "2020/2/23", InvoiceNumber = "6" });
Bills.Add(new Bill { InvoiceDate = "2020/2/23", InvoiceNumber = "7" });
GetIdCommand = new Command<Bill>(async (key) => {
Bill bill=(Bill)key;
await Navigation.PushAsync(new Page2(bill));
});
}
}
public class Bill
{
public string InvoiceNumber { get; set; }
public string InvoiceDate { get; set; }
}