【发布时间】:2014-08-06 00:24:02
【问题描述】:
我遇到了列表选择器的问题。我有一个设置页面,其中包含一个名为“小数”的选项,您可以在其中切换“切换开关”,如果打开切换,则列表选择器将启用。现在,当您单击列表选择器时,您可以选择 1 到 5 作为小数位数。例如,如果您在列表选择器中选择数字“3”,它将被保存到值为 3 的独立存储上的键中,如果您转到 MainPage,它将检查独立存储,如果它包含值“3”,它将设置一个文本块以使用 3 位小数。如果我然后再次转到我的设置页面,应用程序崩溃并显示消息“SelectedIndex 必须始终设置为有效值”。它应该显示在列表选择器中选择的正确值
这是我使用的代码:
Settings.XAML:
<phone:PhoneApplicationPage
xmlns:local="clr-namespace:Vaterpas"
x:Class="Vaterpas.Settings"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">
<phone:PhoneApplicationPage.Resources>
<local:Decimals x:Key="Decimals"/>
<DataTemplate x:Name="lpkFullItemTemplate">
<TextBlock FontSize="36" Text="{Binding}" />
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="100"/>
<RowDefinition Height="120"/>
<RowDefinition Height="100"/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="285">
</ColumnDefinition>
<ColumnDefinition>
</ColumnDefinition>
</Grid.ColumnDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel Grid.ColumnSpan="2" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock Text="Indstillinger" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<Grid Grid.Column="2" Grid.Row="2">
<toolkit:ToggleSwitch Click="tglDecimals_Click" VerticalAlignment="Top" Height="90" Name="tglDecimals" Content="" Margin="0,10,10,0"></toolkit:ToggleSwitch>
<Grid DataContext="{StaticResource Decimals}">
<toolkit:ListPicker x:Name="lpkDecimals" ItemsSource="{Binding decimals}" FullModeHeader="Antal decimaler" FullModeItemTemplate="{StaticResource lpkFullItemTemplate}" ExpansionMode="FullScreenOnly" IsEnabled="False" Margin="130,60,20,0" SelectionChanged="lpkDecimals_SelectionChanged"></toolkit:ListPicker>
</Grid>
</Grid>
Settings.cs
命名空间 Vaterpas { 公共部分类设置:PhoneApplicationPage {
protected IsolatedStorageSettings m_Settings = IsolatedStorageSettings.ApplicationSettings;
protected const string TOGGLE_DECIMALS_SETTING_KEY = "ToggleDecimals";
protected const string DECIMALS_SETTING_KEY = "Decimals";
public Settings()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (m_Settings.Contains(TOGGLE_DECIMALS_SETTING_KEY))
{
string decimalsToggleValue = (string)m_Settings[TOGGLE_DECIMALS_SETTING_KEY];
string decimalsValue = (string)m_Settings[DECIMALS_SETTING_KEY];
if (decimalsToggleValue == "On")
{
lpkDecimals.IsEnabled = true;
tglDecimals.IsChecked = true;
tblDecimals.Text = "On";
if (decimalsValue == "1")
{
lpkDecimals.SelectedIndex = 0; // HERE IT CRASHES, WITH THE ERROR (SelectedIndex must always be set to a valid value.")
}
else if (decimalsValue == "2")
{
lpkDecimals.SelectedIndex = 1; // HERE IT CRASHES, WITH THE ERROR (SelectedIndex must always be set to a valid value.")
}
else if (decimalsValue == "3")
{
lpkDecimals.SelectedIndex = 2; // HERE IT CRASHES, WITH THE ERROR (SelectedIndex must always be set to a valid value.")
}
else if (decimalsValue == "4")
{
lpkDecimals.SelectedIndex = 3; // HERE IT CRASHES, WITH THE ERROR (SelectedIndex must always be set to a valid value.")
}
else if (decimalsValue == "5")
{
lpkDecimals.SelectedIndex = 4; // HERE IT CRASHES, WITH THE ERROR (SelectedIndex must always be set to a valid value.")
}
}
else
{
tglDecimals.IsChecked = false;
tblDecimals.Text = "Off";
lpkDecimals.IsEnabled = false;
}
}
else
{
tglDecimals.IsChecked = false;
tblDecimals.Text = "Off";
}
base.OnNavigatedTo(e);
}
private void tglDecimals_Click(object sender, RoutedEventArgs e)
{
if (tglDecimals.IsChecked == true)
{
lpkDecimals.IsEnabled = true;
tblDecimals.Text = "On";
m_Settings[TOGGLE_DECIMALS_SETTING_KEY] = "On";
lpkDecimals.SelectedIndex = 0;
m_Settings[DECIMALS_SETTING_KEY] = "1";
}
else
{
lpkDecimals.IsEnabled = false;
tblDecimals.Text = "Off";
m_Settings[TOGGLE_DECIMALS_SETTING_KEY] = "Off";
m_Settings[DECIMALS_SETTING_KEY] = "0";
}
}
private void lpkDecimals_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Make sure we don't handle the event during initiation.
if (e.RemovedItems != null && e.RemovedItems.Count > 0)
{
if (this.lpkDecimals.SelectedItem != null)
{
m_Settings[DECIMALS_SETTING_KEY] = lpkDecimals.SelectedItem.ToString();
}
}
}
}
public class Decimals
{
public IEnumerable<string> decimals { get { return "1,2,3,4,5".Split(','); } }
}
}
错误:SelectedIndex 必须始终设置为有效值。
我真的希望有人可以帮助我解决这个问题。
【问题讨论】:
-
似乎 Itemsource 需要时间来设置,并且对 SelectedIndex 的分配发生在它之前。您在哪里分配项目来源?
-
我只在我的 Settings.xaml 文件中分配项目源。
-
检查更新的答案
标签: c# windows-phone selectedindex listpicker