【问题标题】:How to pass a ListView value to another page using an item button click?如何使用项目按钮单击将 ListView 值传递到另一个页面?
【发布时间】:2018-04-20 07:50:25
【问题描述】:

我有ListView,它基于username 填充Id

<Entry Text="{Binding username, Mode=TwoWay}"/>
<Button Command="{Binding SearchCommand}" />
<ListView ItemsSource="{Binding SearchId}" 
          HasUnevenRows="False" 
          x:Name="list" 
          ItemTapped="LstItems_OnItemTapped">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell >
                <StackLayout Orientation="Vertical" >
                    <Label x:Name="ident" 
                           Text="{Binding Id}"></Label>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>        
</ListView>

private async void LstItems_OnItemTapped(object sender, ItemTappedEventArgs e)
{ 
    var item = (UserDetail)e.Item;
    var newpage = new Contact(item.Id);

    await Navigation.PushAsync(newpage);
}

public Command SearchCommand
{
    get
    {
        return new Command(async () =>
        {
            var employeesServices = new EmployeesServices();
            SearchId= await employeesServices.GetUserDetailAsync(_username);

        });
    }
}

private List<UserDetail> _SearchId;           
public List<UserDetail> SearchId
{
    get { return _SearchId;  }
    set
    {
        _SearchId = value;
        OnPropertChanged();
    }
}

我可以成功地将id传递到另一个页面,现在我有另一个问题,当我点击列表视图时,我只能将Id从列表视图传递到另一个页面。

如果我点击按钮Id 直接通过下一页而不点击ListView,是否可以?

【问题讨论】:

  • 请编辑您的问题以添加您的代码,现在无法猜测您的目标。
  • 我已经编辑我的问题帮助将不胜感激
  • 那么这到底应该如何工作?用户选择列表中的一个项目,然后单击按钮,然后将他们带到一个新页面,将所选项目的 id 传递到该新页面?
  • 不,我想要的是用户不会点击列表上的项目,当用户点击按钮时 listview 从数据库中检索 Id 我想从同一个按钮传递 id,所以我想要的是单击用户 Listview 填充数据并将该数据传递到下一页
  • 所以 1) 用户在文本框中输入名称 2) 他们单击按钮,该按钮填充 ListView。您能否将SearchCommand 的代码添加到您的问题中,看看那里发生了什么? 3)他们做一些事情去下一页。那是什么,您能否也添加代码?

标签: c# xaml xamarin xamarin.forms


【解决方案1】:

您可以像这样在列表视图中使用SelectedItem

<ListView ItemsSource="{Binding SearchId}" HasUnevenRows="False" x:Name="list" ItemTapped="LstItems_OnItemTapped" SelectedItem="{Binding SelectedListItem,Mode=TwoWay}">

使用此功能后,您将拥有列表中当前选定的项目,您可以将其传递到任何页面! 希望这可以解决您的问题。

【讨论】:

  • 但是我怎样才能把这个项目传递给cs类的其他页面呢?我的意思是cs类中的incode是什么?帮助将不胜感激
  • @waqaswaqas 例如你的类 A(ModelOfListView item) 和你可以像这样传递的选定项目 Navigation.PushAsync(selecteditem);
  • 我已经编辑了我的问题得到另一个问题的帮助将不胜感激
  • 我强烈建议您不要编辑您的问题来报告其他问题。如果答案为您提出的问题提供了解决方案,请接受它,如果您遇到其他问题,请发布一个新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-15
  • 2014-01-01
相关资源
最近更新 更多