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
C# 如何一次选择Listview中的所有项目或从每行获取每个主键?_C#_Xamarin_Xamarin.forms - Fatal编程技术网

C# 如何一次选择Listview中的所有项目或从每行获取每个主键?

C# 如何一次选择Listview中的所有项目或从每行获取每个主键?,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,到目前为止,我已经得到了它,它获取Listview中所选项目的索引号,然后我使用它从数据库中获取数据。现在我需要一个away来获取所有的行索引号,以获取id和符号,并在向下滑动listview时运行它 async void myPriceList_ItemSelected(System.Object sender, Xamarin.Forms.SelectedItemChangedEventArgs e) { userInput selec

到目前为止,我已经得到了它,它获取Listview中所选项目的索引号,然后我使用它从数据库中获取数据。现在我需要一个away来获取所有的行索引号,以获取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);
            

            };
我想查看Listview中的任何内容。选择要在刷新命令下使用的编辑项

如果要在
ListView的刷新命令中使用
ListView.SelectedItem
,可以使用
ListView的SelectedItem
绑定

我做了一个示例,在
列表视图的刷新命令中获取
列表视图的SelectedItem

<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;
        });
    }
}

根据您的描述和代码,首先在ListView中加载列表数据并获取ListView selecteditem,然后从sqlite数据库中获取一些数据来绑定ListView,然后使用
ListView.selecteditem通过api获取一些数据,但我不清楚您的问题是什么。@CherryBu MSFT我想查看列表视图中的任何内容。选择“编辑项”将在“刷新”命令下使用,但我必须获取列表视图中每个项目的项目主键才能执行此操作,我无法获取。如果您希望列表中的所有项目,只需使用
IfemSource
collection@Jason我在哪里以及如何做到这一点?ThanksI无法给出具体示例,因为您还没有展示如何填充ListView。但在某个地方,您会得到一个分配给
ItemsSource
的数据列表。您需要保留对该数据的引用,并且每当您需要对所有项目执行某些操作时,您都有一个列表,其中应该包含您需要的所有内容。
        protected override void OnAppearing()
        {
            base.OnAppearing();

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

            });


        }
<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;
        });
    }
}
public class ViewModelBase : INotifyPropertyChanged
{
   
    public event PropertyChangedEventHandler PropertyChanged;
   
    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}