首页 > 解决方案 > 如何一次选择 Listview 中的所有项目或从每一行获取每个主键?

问题描述

到目前为止,我已经得到了这个,它在 Listview 中获取所选项目的索引号,然后我使用它从我的数据库中获取数据。现在我需要一个离开来获取所有行索引号以获取 id 和符号并在 listview 向下滑动期间运行它。

        async void myPriceList_ItemSelected(System.Object sender, Xamarin.Forms.SelectedItemChangedEventArgs e)
        {
            userInput selectedOne = (userInput)e.SelectedItem;

            var id = selectedOne.Id.ToString();
            var symbol = selectedOne.Pair.ToString();
            using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
            {
                conn.CreateTable<userInput>();
                var userInfo = conn.Table<userInput>().ToList();
                myPriceList.ItemsSource = userInfo;


                var buyAmount = conn.Get<userInput>(id).buyAmount;
                var buyPair = conn.Get<userInput>(id).Pair.ToString();
                HttpClient client = new HttpClient();
                var response = await client.GetStringAsync("https://api.binance.com/api/v3/ticker/price?symbol=" + symbol);
                var cryptoconverted = JsonConvert.DeserializeObject<Crypto>(response);
                var currentPriceDouble = double.Parse(cryptoconverted.price);
                var finalAnswer = double.Parse(cryptoconverted.price) * double.Parse(buyAmount);
                conn.Execute("UPDATE userInput SET worth = " + finalAnswer + " where Id= " + id);
            

            };

这是我的列表视图

<Grid Margin="-10,0,0,0" Padding="7,0,7,0">
                <ListView x:Name="myPriceList" IsPullToRefreshEnabled="True" RefreshCommand="{Binding RefreshCommand}" IsRefreshing="{Binding IsRefreshing}" >
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="33*" />
                                        <ColumnDefinition Width="33*" />
                                        <ColumnDefinition Width="30*" />
                                    </Grid.ColumnDefinitions>
                                        <StackLayout Orientation="Vertical" Grid.Column="0" Margin="10,0,0,0">
                                            <Label Text="{Binding Pair}" FontAttributes="Bold" TextColor="Black" FontSize="12" Margin="0,0,0,-5"/>
                                            <Label Text="{Binding buyPrice}" TextColor="DarkSlateGray" FontSize="10" HorizontalTextAlignment="Start" Margin="0,0,0,-5"/>
                                            <Label Text="{Binding Id}" TextColor="DarkSlateGray" FontSize="10" HorizontalTextAlignment="Start"/>
                                        </StackLayout>
                                        <StackLayout Orientation="Vertical" Grid.Column="1" > 
                                            <Button Text="Delete" Clicked="Button_Clicked" CommandParameter="{Binding ItemName}"></Button>
                                        </StackLayout>
                                        <StackLayout Orientation="Vertical" Grid.Column="2">
                                            <Label Text="{Binding worth}" FontAttributes="Bold" TextColor="Black" FontSize="12" Margin="0,0,0,-5" HorizontalTextAlignment="End"/>
                                            <Label Text="{Binding buyAmount}" TextColor="DarkSlateGray" FontSize="10" HorizontalTextAlignment="End" Margin="0,0,0,-5"/>
                                            <Label Text="{Binding spent}" TextColor="DarkSlateGray" FontSize="10" HorizontalTextAlignment="End"/>
                                        </StackLayout>
                                </Grid>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
        </Grid>

这是刷新

        protected override void OnAppearing()
        {
            base.OnAppearing();

            myPriceList.RefreshCommand = new Command(() =>
            {
                //It should be here
                myPriceList.IsRefreshing = false;

            });


        }

标签: c#xamarinxamarin.forms

解决方案


我想在我的 Listview.selecteditem 中的任何内容在刷新命令下使用

如果要使用ListView.SelectedItemin ListView's RefreshCommand,可以使用ListView's SelectedItem绑定。

我做一个样品ListView's SelectedItem进入ListView's RefreshCommand

<ListView
            x:Name="listPlatforms"
            IsPullToRefreshEnabled="True"
            IsRefreshing="{Binding IsRefreshing}"
            ItemsSource="{Binding mylist}"
            RefreshCommand="{Binding RefreshCommand}"
            SelectedItem="{Binding selecteditem}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal">
                            <Label Text="{Binding Id}" />
                            <Label Text="{Binding Name}" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

public partial class Page14 : ContentPage
{      
    public Page14()
    {
        InitializeComponent();
       
        this.BindingContext = new userviewmodel();
    }  
}

public class userviewmodel:ViewModelBase
{
    public ObservableCollection<userInput> mylist { get; set; }
    public ICommand RefreshCommand { get; }
    private userInput _selecteditem;
    public userInput selecteditem
    {
        get { return _selecteditem; }
        set
        {
            _selecteditem = value;
            RaisePropertyChanged("selecteditem");
        }
    }
    private bool _isRefreshing = false;
    public bool IsRefreshing
    {
        get { return _isRefreshing; }
        set
        {
            _isRefreshing = value;
            RaisePropertyChanged("IsRefreshing");
        }
    }
    public userviewmodel()
    {
        mylist = new ObservableCollection<userInput>();
       for(int i=0; i<30; i++)
        {
            userInput user = new userInput();
            user.Id = i.ToString();
            user.Name = "cherry " + i;
            mylist.Add(user);
        }
        RefreshCommand = new Command(async () =>
        {
            IsRefreshing = true;
            if(selecteditem!=null)
            {
                Console.WriteLine(selecteditem.Name);
            }
           
             //RefreshData();

            IsRefreshing = false;
        });
    }
}

ViewModelBase 是实现 INotifyPropertyChanged 的​​类,用于通知数据更新。

public class ViewModelBase : INotifyPropertyChanged
{
   
    public event PropertyChangedEventHandler PropertyChanged;
   
    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

推荐阅读