Windows Phone 7 に 2 つの Silverlight リストピッカー コントロールがあります。
これがそのための私の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;
}
それでも、私のコードはエラーなしで正常に動作します。今、私が立ち往生しているトリッキーで難しい部分が来ます. 1 つの 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 を超えると、選択したインデックス メソッドの添付プロジェクトにあります。空白になります。なぜこのように振る舞うのかわかりません..