[英]ListPicker Set Selected Index not refreshes the UI in WP7 after async call
我的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>
现在的场景是,如果用户选择了国家名称,那么它将自动设置该国家的国家代码,反之亦然。
为此,我正在为两个列表使用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;
}));
}
仍然没有例外,一切顺利,它按照我的国家/地区设置了我的listpicker选择的索引,但是它不会更新我的listpicker的UI并将其设置为空白,或者您可以说为空。 但是,当我在后端点击列表选择器时,所需的国家/地区已经设置好。 但未更新或卡在UI线程中。
So problem is UI is not updated properly
===更新===
当索引高于38时,我的发现是在我选择的索引方法的附加项目中。它将变为空白。 我不知道为什么它会这样
我已经实现了您的整个代码(除了Google服务,我已将其替换为其他服务),并且运行良好,UI可以正常更新。 我看不到代码中的任何问题。 在这里检查我的代码
所以。 我建议您交叉检查“ countryIndex”。 创建另一个普通文本块并将此countryIndex分配给该文本块及其值。
还要将“我的代码”与您的代码进行比较,以使您获得一些线索。
在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本身内部的问题。
经过三天的努力,我已经解决了该问题。 早些时候,我认为这是我的UI线程中的问题,并且没有刷新。 我专注于那部分,因为我假设这就是问题。 但是在第三天,我注意到在列表选择器控件中这可能是错误的。 我在Codeplex上学习。 有些人也面临这个问题。 但是我如何纠正我告诉你。 我做了四个步骤
我从项目中删除了Silverlight工具包的所有参考,并清理了解决方案。
我从pc安装了silverlight工具包,然后安装了2011年11月的稳定版本,然后重新启动了PC,并从新安装中引用了我项目中的dll。
我也将选择的索引与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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.