0

私の連絡先アプリ(wp7用)では、このエラーをまったく修正できません。下に画像も追加しました。連絡先の番号をタップすると、その番号に電話をかけることができません。次のエラーが発生しています - NullReferenceException。PhoneCallTask​​ も使用しました。

ここに画像の説明を入力

xamlで-

<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

        <TextBlock Text="{Binding Path=DisplayName, Mode=OneWay}" Foreground="{StaticResource PhoneAccentBrush}" FontSize="{StaticResource PhoneFontSizeExtraLarge}" />

        <Border BorderThickness="2" HorizontalAlignment="Left" BorderBrush="{StaticResource PhoneAccentBrush}" >
            <Image Name="Picture" Height="175" Width="175" HorizontalAlignment="Left" />
        </Border>
        <TextBlock Height="50" Name="textBlock1" Text="call mobile" FontSize="40" Margin="0,30,0,0"/>
        <ListBox x:Name="ListBox" ItemsSource="{Binding Path=PhoneNumbers}" FontSize="64" Height="100"  Margin="0,0,0,0" SelectionChanged="ListBox_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <!--TextBlock Grid.Column="0" Text="{Binding Path=Kind, Mode=OneWay}" />
                        <TextBlock Grid.Column="1" Text=":  " /-->
                        <TextBlock Grid.Column="2" Text="{Binding Path=PhoneNumber, Mode=OneWay}" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>

xaml.cs-

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        //Set the data context for this page to the selected contact
        this.DataContext = App.con;

        try
        {
            //Try to get a picture of the contact
            BitmapImage img = new BitmapImage();
            img.SetSource(App.con.GetPicture());
            Picture.Source = img;
        }
        catch (Exception)
        {
            //can't get a picture of the contact
        }
    }

    private void ListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        SampleData data = (sender as ListBox).SelectedItem as SampleData;
        ListBoxItem selectedItem = this.ListBox.ItemContainerGenerator.ContainerFromItem(data) as ListBoxItem;
        PhoneCallTask PhoneTask = new PhoneCallTask();
        PhoneTask.PhoneNumber = data.PhoneNumbers;
        PhoneTask.Show();
    }

    public class SampleData
    {
        public string PhoneNumbers { get; set; }
    }

誰でもこれで私を助けることができますか? お疲れ様でした!

4

1 に答える 1

1

SelectedItemプロパティが type ではないことは確かSampleDataなので、キャストは失敗し、null が返されます。

SampleData data = (sender as ListBox).SelectedItem as SampleData;

したがって、次の行dataは null であるため、null 参照例外をスローします。

PhoneTask.PhoneNumber = data.PhoneNumbers;

デバッガーを使用すると、この結論を簡単に確認できるはずです。問題を解決するためにデバッガーを使用する習慣がない場合は、使用を開始することを心からお勧めします ;-)

于 2012-06-17T15:42:53.750 に答える