【问题标题】:Windows phone listpicker set selectedIndexWindows 手机列表选择器设置 selectedIndex
【发布时间】: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


【解决方案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);
    }

这会有所帮助

【讨论】:

    【解决方案2】:

    首先有一个选定索引的属性,以便像这样检查--

    int lpkSelectedIndex=0;
    public int LpkSelectedIndex
    {
        get
        {
            return this.lpkSelectedIndex;
        }
        set
        {
            this.lpkSelectedIndex= value;
        }
    }
    

    然后像这样在xaml中绑定这个属性。记住在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>
    

    【讨论】:

    • 不,它不起作用,你能解释一下我将如何使用你发布的这段代码吗,名为“PickerSelectedIndex”的绑定我需要改变吗?它还说“lpkSelectedIndex " 已经存在错误
    • 非常抱歉,我错误地在 selectedindex 绑定中写了,现在我再次编辑了 selectedIndex 绑定属性。请检查它。您能否解释一下这个已经存在的错误,无论您使用的是任何其他名为 lpkSelectedIndex 的属性。
    • 你可以直接将这个属性(lpkselectedIndex)作为查询字符串传递,将在下一页检索
    • 私有 int lpkSelectedIndex = 0; public int lpkSelectedIndex //这里的错误是:“类型设置已经包含'lpkSelectedIndex' { get { return this.lpkDecimals; } set { this.lpkDecimals= value; } }
    • 编辑了一点,更改了属性名称。你能再试一次吗,但你明白了我想说的对..我的意思是把这个属性作为查询字符串传递到下一页.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    • 2014-08-12
    • 1970-01-01
    • 2016-08-04
    相关资源
    最近更新 更多