ListPicker Set Selected Index在异步调用后不刷新WP7中的UI

时间:2012-06-15 09:13:11

标签: c# silverlight windows-phone-7 xaml silverlight-toolkit

我的Windows Phone 7中有两个silverlight listpicker控件。

这是我的XAML。

//国家/地区名称的第一个listpicker

    <toolkit:ListPicker x:Name="listPickerCountryLogin" SelectionChanged="listPickerCountryLogin_SelectionChanged" Height="72" HorizontalAlignment="Left" Margin="14,43,0,0" VerticalAlignment="Top" Width="436" FullModeHeader="Select Country" Background="White" BorderBrush="White" Foreground="{StaticResource listPickerBrush}">
                            <toolkit:ListPicker.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding Country}" Width="250" />
                                    </StackPanel>
                                </DataTemplate>
                            </toolkit:ListPicker.ItemTemplate>
                            <toolkit:ListPicker.FullModeItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding Country}" Width="300" Margin="0,0,0,20" FontSize="24"/>
                                    </StackPanel>
                                </DataTemplate>
                            </toolkit:ListPicker.FullModeItemTemplate>
                        </toolkit:ListPicker>

// and here is my second listpciker for country codes

                    <toolkit:ListPicker x:Name="listPickerCCLogin" SelectionChanged="listPickerCCLogin_SelectionChanged" Height="56.3" Width="80" HorizontalAlignment="Left" Margin="14,100,0,0"  VerticalAlignment="Top" FullModeHeader="Select Country" Background="White" BorderBrush="White" Foreground="{StaticResource listPickerBrush}">
                        <toolkit:ListPicker.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="lblCC" Text="{Binding CC}" Width="235" />
                                </StackPanel>
                            </DataTemplate>
                        </toolkit:ListPicker.ItemTemplate>
                        <toolkit:ListPicker.FullModeItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock  Text="{Binding Country}" Width="300" Margin="0,0,0,20" FontSize="24"/>
                                </StackPanel>
                            </DataTemplate>
                        </toolkit:ListPicker.FullModeItemTemplate>
                    </toolkit:ListPicker>

现在情形是,如果用户选择国家/地区名称,那么它将自动设置该国家/地区的国家/地区代码,反之亦然。

对于这件事我使用listpicker选择更改两个列表的事件。

这是我的C#代码。

首先,我将此listpickers与此方法中的国家/地区集合在一起。

/// <summary>
        /// Binding All Listpickers With Data
        /// </summary>
        protected void BindListPickers()
        {
            CountryListParser oCountryList = new CountryListParser();
            this.listPickerCountryLogin.ItemsSource = oCountryList.GetAllCountries();
            this.listPickerCCLogin.ItemsSource = oCountryList.GetAllCountries();
        }

这是列表选择器选择更改事件。

  /// <summary>
        /// Country List Picker Of Login Selection Change Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listPickerCountryLogin_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listPickerCountryLogin.SelectedIndex >= 0 && listPickerCountryLogin.SelectedIndex < listPickerCCLogin.Items.Count)
                listPickerCCLogin.SelectedIndex = listPickerCountryLogin.SelectedIndex;
        }

/// <summary>
/// Country Code List Picker Of Login Selection Change Event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listPickerCCLogin_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (listPickerCCLogin.SelectedIndex >= 0 && listPickerCCLogin.SelectedIndex < listPickerCountryLogin.Items.Count)
        listPickerCountryLogin.SelectedIndex = listPickerCCLogin.SelectedIndex;
}

仍然我的代码工作正常,没有任何错误。现在来到我被困的困难和困难的部分。我打电话给一个谷歌服务并通过lat长用户,它返回我的用户国家,我想将该国家设置为我的列表选择器。

这是我的代码

protected void OnLocationServiceResponseRecieved(string response)
        {
            JObject o = JObject.Parse(response);
            string Country = (string)o["countryname"];

            Dispatcher.BeginInvoke(new System.Action(delegate()
            {
                CountryListParser oCountryList = new CountryListParser();
                int countrytIndex = oCountryList.CountryIndexByName(Country);
                this.listPickerCountryLogin.SelectedIndex = countrytIndex;
                this.listPickerCCLogin.SelectedIndex = countrytIndex;
            }));
        }

仍然没有例外,一切顺利,它根据我的国家设置我的listpicker选择索引,但它不更新我的listpicker的UI,并将它们空白或你可以说空。但是当我在后端点击我的listpicker时,我所希望的国家已经设定好了。但没有更新或陷入UI线程。

So problem is UI is not updated properly

=== UPDATE ===

My Sample Code Where Issue Is Reproducing

当索引大于38时,我的选择是在我选择的索引方法的附加项目中。它将变为空白。我不知道为什么它表现得像这样......

3 个答案:

答案 0 :(得分:0)

我已经实现了您的整个代码(除了我用其他服务替换的谷歌服务),它工作得很好,UI正在更新。 我发现代码中没有任何问题。检查我的代码HERE

因此。我建议你交叉检查你的'countryIndex'。创建另一个普通文本块并将此countryIndex分配给该文本块及其值。

同时将我的代码与您的代码进行比较,以便您可以获得一些线索。

答案 1 :(得分:0)

在我的Panorama应用程序中使用ListPicker时,我也遇到了类似的问题。

我有一个从0到50的项目列表。

<toolkit:ListPicker x:Name="MyListPicker"  ItemsSource="{Binding MyList}" Width="174" Margin="10, 10, 0, 0" ItemTemplate="{StaticResource MyTemplate}">
</toolkit:ListPicker>

<DataTemplate x:Name="MyTemplate">
        <TextBlock Text="{Binding}" FontFamily="Calibri" FontSize="50" />
    </DataTemplate>

我注意到,对于大于38或40的索引(就像你一样),选择列表项后UI不会更新。它只显示一个空白的UI。

我也知道他们建议不要将ListPicker用于大型列表,但我猜51项是可以的(或者可能不是?)

我因此认为这可能是INSIDE ListPicker本身的问题。

答案 2 :(得分:0)

经过3天的努力,我已经解决了这个问题。早些时候我认为这是我的UI线程中的问题而且它没有让人耳目一新。当我假设这些问题时,我专注于那一部分。但是在第3天我注意到这可能是listpicker控件中的错误。我研究了codeplex。有些人也面临这个问题。但是我如何纠正让我告诉你。我做了四个步骤

  1. 我从项目中删除了silverlight工具包的所有引用并清理了解决方案。

  2. 我从pc安装了silverlight工具包,然后安装了2011年11月的稳定版本并重新启动了PC,并在这个新安装中引用了我的项目中的dll。

  3. 我也使用listpicker控件绑定了选定的索引。

                <toolkit:ListPicker x:Name="listPickerCountryLogin" SelectionChanged="listPickerCountryLogin_SelectionChanged" Height="72" HorizontalAlignment="Left" Margin="14,43,0,0" VerticalAlignment="Top" Width="436" FullModeHeader="Select Country" Background="White" BorderBrush="White" Foreground="{StaticResource listPickerBrush}">
                    <toolkit:ListPicker.Resources>
                        <Style TargetType="toolkit:ListPickerItem">
                            <Setter Property="Padding" Value="8 6"/>
                        </Style>
                    </toolkit:ListPicker.Resources>
                    <toolkit:ListPicker.Style>
                        <StaticResource ResourceKey="ListPickerStyle"/>
                    </toolkit:ListPicker.Style>
                    <toolkit:ListPicker.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Country}" Width="250" />
                            </StackPanel>
                        </DataTemplate>
                    </toolkit:ListPicker.ItemTemplate>
                    <toolkit:ListPicker.FullModeItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Country}" Width="300" Margin="0,0,0,20" FontSize="24"/>
                            </StackPanel>
                        </DataTemplate>
                    </toolkit:ListPicker.FullModeItemTemplate>
                </toolkit:ListPicker>