拨打wp7的联系电话

时间:2012-06-17 14:01:29

标签: c# windows-phone-7

在我的联系人应用程序中(对于wp7),我根本无法纠正此错误。我还在下面添加了一张图片。当我点击联系电话时,我无法拨打该号码。我收到以下错误 - NullReferenceException 。我也使用过PhoneCallTask​​。

enter image description here

在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; }
    }

有人可以帮我这个吗?提前感谢您的辛勤工作!

1 个答案:

答案 0 :(得分:1)

我很确定SelectedItem属性不是SampleData类型,因此强制转换会失败并返回null:

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

因此,此行抛出空引用异常,因为data为null:

PhoneTask.PhoneNumber = data.PhoneNumbers;

使用调试器,应该很容易确认这个结论 - 如果你不习惯使用调试器来解决问题,我真诚地敦促你开始使用它;-)