我遇到了listpicker的问题。我有一个设置页面,其中有一个名为“Decimals”的选项,您可以在其中切换“Toggleswitch”,如果打开切换按钮,则会启用listpicker。现在,当您单击listpicker时,您可以选择1到5作为小数位数。如果您在listpicker中选择数字“3”,它将被保存到具有值3的隔离存储上的密钥,如果您转到MainPage,它将检查Isolatedstorage并且它是否包含值“3”,它将设置一个文本块使用3位小数。如果我再次转到我的“设置”页面,应用程序崩溃并显示消息“SelectedIndex必须始终设置为有效值”。它应该显示在listpicker
中选择的正确值以下是我使用的代码:
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 { public partial class设置: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设置为有效值。
我真的希望有人可以帮我解决这个问题。
答案 0 :(得分:1)
我认为ItemSource需要时间来分配给listpicker,因为它是在xaml中分配的,OnNavigatedTo有时会在InitializeComponent()之前运行;加载xaml的方法,并且在OnNavigatedTo中分配了Selected Index属性,这是导致错误的原因。
从.xaml中删除itemsource的赋值,并在所有编码内容之前尝试在OnNavigatedTo中自行分配。
像这样: -
protected override void OnNavigatedTo(NavigationEventArgs e)
{
tglDecimals.ItemSource=Your List;
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);
}
这会有帮助
答案 1 :(得分:0)
首先有一个选定索引的属性,只是为了这样检查 -
int lpkSelectedIndex=0;
public int LpkSelectedIndex
{
get
{
return this.lpkSelectedIndex;
}
set
{
this.lpkSelectedIndex= value;
}
}
之后在xaml中将此属性绑定为this.And记住在Itemsource属性之后绑定它并尝试它。
<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" SelectedIndex="{Binding LpkSelectedIndex, Mode=TwoWay}"></toolkit:ListPicker>