列表选取器集选定索引在异步调用后不会刷新 WP7 中的 UI
本文关键字:刷新 WP7 UI 中的 调用 异步 选取 索引 列表 | 更新日期: 2023-09-27 18:37:22
我的Windows Phone 7中有两个Silverlight listpicker控件。
这是我的 XAML。
国家名称的第一个列表选择器
<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>
现在的情况是,如果用户选择国家名称,那么它也将自动设置该国家的国家/地区代码,反之亦然。
对于这件事,我正在为两个列表使用列表选择器选择更改事件。
这是我的 C# 代码。
首先,我将我的列表选择器与此方法中的国家/地区集合绑定。
/// <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;
}
仍然我的代码工作正常,没有任何错误。现在来了我被困住的棘手和困难的部分。我正在调用一项Google服务并传递用户的纬度,它返回我用户国家/地区,我想将该国家/地区设置为我的列表选择器。
这是我的代码
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;
}));
}
仍然没有例外,一切都很顺利,它根据我的国家/地区设置我的列表选择器选择索引,但它不会更新我的列表选择器的 UI 并将它们设置为空白,或者您可以说为空。但是当我在后端点击我的列表选择器时,我想要的国家已经设置好了。但未更新或卡在 UI 线程中。
So problem is UI is not updated properly
=== 更新 ===
我的示例代码,其中问题正在重现
当索引高于 38 时,我的发现是在我附加的项目中以选定的索引方法。它将变为空白。我不知道为什么它的行为是这样的。
我已经实现了您的整个代码(除了我用其他服务替换的谷歌服务),它运行良好,UI 更新良好。我在代码中看不到任何问题。检查我的代码 这里
所以。我建议您交叉检查您的"国家指数"。创建另一个普通文本块,并将此国家/地区索引分配给该文本块及其值。
还可以将我的代码与您的代码进行比较,以便您可能会获得一些线索。
我在 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 个项目是可以的(或者可能不是?
因此,我认为这可能是ListPicker本身的问题。
3 天的挣扎,我已经解决了这个问题。早些时候,我认为这是我的UI线程中的问题,并且没有刷新。我专注于这一部分,因为我假设这就是问题。但是在第 3 天,我注意到这可能是列表选择器控制中的错误。我研究了代码复合体。有些人也面临这个问题。但是我如何纠正让我告诉你。我做了四个步骤
我从项目中删除了 silverlight 工具包的所有引用并清理了解决方案。
我从 PC 安装了 silverlight 工具包,然后安装了 2011 年 11 月的稳定版本并重新启动了 PC,并从这个新安装中引用了我的项目中的 dll。
我将选定的索引与我的列表选取器控件绑定在一起。
<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>