2012-03-13 22 views
0

私はいくつかのタスクのIDと各タスク(選択)の詳細を示す詳細ページを表示するために、最初のページの1つのリストボックスで構成されるWP7アプリケーションを構築しています。ListBoxのSelectionChangedイベントです。ページ間のパラメータを渡す

最初のページのタスクIDを2番目のページに渡す必要があります。私はそれがリストボックスのSelectionChangedイベントによって実行されることを知っています。

これは私のXAMLコードです:

<phone:PhoneApplicationPage 
    x:Class="TaskListAlpha03.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="696" 
    FontFamily="{StaticResource PhoneFontFamilyNormal}" 
    FontSize="{StaticResource PhoneFontSizeNormal}" 
    Foreground="{StaticResource PhoneForegroundBrush}" 
    SupportedOrientations="Portrait" Orientation="Portrait" 
    shell:SystemTray.IsVisible="True"  
    xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"> 

    <phone:PhoneApplicationPage.Resources> 
     <DataTemplate x:Key="TaskListListBoxTemplate">    
      <StackPanel Orientation="Vertical" Margin="0,0,0,20">     
       <TextBlock Text="{Binding Crm_object_id}" FontSize="32" FontFamily="Segoe WP Bold" Foreground="Gray"/> 
       <!--<TextBlock Text="{Binding Comment}" Margin="10,0,0,0"/>    --> 
      </StackPanel>    
     </DataTemplate> 
    </phone:PhoneApplicationPage.Resources> 

     <!--LayoutRoot is the root grid where all page content is placed--> 
    <Grid x:Name="LayoutRoot" Background="Transparent"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 

     <!--TitlePanel contains the name of the application and page title--> 
     <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,40"> 
      <TextBlock x:Name="ApplicationTitle" Text="TASK LIST ALPHA" Style="{StaticResource PhoneTextNormalStyle}"/> 
      <TextBlock x:Name="PageTitle" Text="tasks" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
     </StackPanel> 

     <!--ContentPanel - place additional content here--> 
     <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
      <ListBox 
         x:Name="allTaskListTasksListBox" 
         ItemsSource="{Binding AllTaskListTasks}" 
         ItemTemplate="{StaticResource TaskListListBoxTemplate}" 
         SelectionChanged="allTaskListTasksListBox_SelectionChanged" /> 
     </Grid> 
    </Grid> 


</phone:PhoneApplicationPage> 

そして、これは、CSコードです:

protected override void OnNavigatedTo 
     (System.Windows.Navigation.NavigationEventArgs e) 
     { 
      base.OnNavigatedTo(e); 
      string msg = ""; 
      if (NavigationContext.QueryString.TryGetValue("msg", out msg)) 
       PageTitle.Text = msg; 
     } 
:私はそれが動作するかどうかを確認するために、このサンプルコードを持って、2ページ目で

private void allTaskListTasksListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    {    
     ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem); 
     NavigationService.Navigate(new Uri("/View/Details.xaml?msg=" + lbi.Content.ToString(), UriKind.RelativeOrAbsolute)); 
    } 

私はアプリケーションを実行すると "NullReferenceException"が発生します。

私の英語のために申し訳ありません:Sとありがとう。

答えて

0

ここでのポイントのカップル...
1)私はアイテムではなく、選択が変更されたイベントにタップイベントを使用しますような何かを行います。誤ったナビゲーションを防ぐのに役立ちます。
2)を実行する場合、choosechangedイベントを使用するには、次のコードを試してアイテムを入手してください。アイテムがリスト内で選択解除されていると、現在のコードが実行を試みます。

private void allTaskListTasksListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    if(e.AddedItems.Count > 0) // the items that were added to the "selected" collection 
    { 
     var mySelectedItem = e.AddedItems[0] as myItemType; 
     if(null != mySelectedItem) // prevents errors if casting fails 
     { 
      NavigationService.Navigate(
       new Uri("/View/Details.xaml?msg=" + mySelectedItem.Crm_object_id, 
         UriKind.RelativeOrAbsolute) 
      ); 
     } 
    } 
} 
+0

はどうもありがとうございました。) – javiazo

+0

私はあなたの第二の先端を使用し、正常に動作している、タンク – javiazo

0

問題はListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);です。あなたはそれをListBoxItemにキャストしていますが、これは実際にはListBoxにバインドするカスタム項目です。

var item = ((sender as ListBox).SelectedItem as YourItem);

+0

あまりにもあなたに感謝;) – javiazo

関連する問題