如何保持工具包:listPicker selectedItem在新页面加载WP8

时间:2016-05-06 11:39:58

标签: windows-phone-8 indexing selecteditem listpicker

我正在使用toolkit:listPicker从列表Collection中选择车辆,但当我转到另一个页面并返回到使用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事件来获取所选车辆并在listBox工具中显示所有加油:

        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辆车。 Index'0'= BMW E46和Index'1'= Opel Astra。我选择了欧宝Astra(Index'1'),但是当我转到另一个页面然后回到这个页面时,索引又回到了'0'。

Image_1

1 个答案:

答案 0 :(得分:0)

ItemsSource的{​​{1}}设置在页面的Loaded事件中。

ListPicker

当您返回此页面时,将再次触发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(); } 将被重置,从而更改所选项目。

根据您的要求,您可以移动代码以将ItemsSource设置为其他位置,也可以存储ItemSource的{​​{1}}并在返回时重新分配。