如何在新页面加载时保持 toolkit:listPicker selectedItem WP8

how to keep toolkit:listPicker selectedItem at a new page load WP8

我正在使用 toolkit:listPicker 从列表集合中选择车辆,但是当我转到另一个页面并返回到使用 listPicker 工具的页面时,所选索引更改为“0”(当列表为空时,它默认为“-1”)。 请看下面我的方法:

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
                ReadAllVehicleList dbVehicles = new ReadAllVehicleList();
                DB_VehicleList = dbVehicles.GetAllVehicles();
                pickVehicle.ItemsSource = DB_VehicleList.OrderByDescending(i => i.Id).ToList();
    }

这位正在调用以下内容:

    public class ReadAllVehicleList
{
    DatabaseHelperClass Db_Helper = new DatabaseHelperClass();
    public ObservableCollection<Vehicle> GetAllVehicles()
    {
        return Db_Helper.ReadVehicle();
    }
}

    //This class for perform all database CRUID operations
    public class DatabaseHelperClass
{
        SQLiteConnection dbConn;

        public ObservableCollection<Vehicle> ReadVehicle(){
        using (var dbConn = new SQLiteConnection(App.DB_PATH))
        {
            List<Vehicle> myCollection = dbConn.Table<Vehicle>().ToList<Vehicle>();
            ObservableCollection<Vehicle> VehicleList = new ObservableCollection<Vehicle>(myCollection);
            return VehicleList;
        }
}

还有一个 selectionChanged 事件,用于获取选定的车辆并在列表框工具中显示其所有加油:

        private void pickVehicle_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
            if (pickVehicle.SelectedIndex != -1)
            {
            VehiclesPage.selectedVehicle = pickVehicle.SelectedItem as Vehicle;//selectedVehicle присвоява избраното превозно средство.
            ReadAllRefuelingList dbRefuelings = new ReadAllRefuelingList();
            DB_RefuelingList = dbRefuelings.GetAllRefuelings();//Извличане на всички записи за зареждане на гориво от базата данни
            listBoxobj.ItemsSource = DB_RefuelingList.OrderByDescending(i => i.Id).ToList();//Показване на записите в обратен ред (descending) - най-новото ID се показва най-отгоре
            }
    }

XAML:

        <toolkit:ListPicker x:Name="pickVehicle"     HorizontalAlignment="Center" Margin="0" VerticalAlignment="Top" Grid.Row="1" Width="480" SelectionChanged="pickVehicle_SelectionChanged" Background="Gray" BorderBrush="Orange" Foreground="White">
        <toolkit:ListPicker.ItemTemplate>
            <DataTemplate>
                <TextBlock HorizontalAlignment="Center"> 
                                <Run Text="{Binding Make}"/>
                                <Run Text="{Binding Model}"/>
                </TextBlock>
            </DataTemplate>
        </toolkit:ListPicker.ItemTemplate>
        </toolkit:ListPicker>

这是问题的一个例子。 Database/listPicker 我有 2 辆车。索引“0”= BMW E46,索引“1”=欧宝雅特。我选择了 Opel Astra(索引'1'),但是当我转到另一页然后返回到这一页时,索引又回到了'0'。

Image_1

ListPickerItemsSource设置在页面的Loaded事件中。

   private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {       
             ReadAllVehicleList dbVehicles = new ReadAllVehicleList();
             DB_VehicleList = dbVehicles.GetAllVehicles();           
             pickVehicle.ItemsSource = DB_VehicleList.OrderByDescending(i => i.Id).ToList();
        }

当您返回此页面时,将再次触发 Loaded 事件,因此 ItemsSource 将被重置,从而更改所选项目。

根据您的要求,您可以将设置 ItemSource 的代码转移到其他地方,或者可以存储 ListPickerSelectedIndex 并在返回时重新分配。