2014-12-12 160 views
1

我想在一個的ObservableCollection顯示添加的元素顯示在頁面(MenuPage)列表框。的Windows Phone:列表框不能顯示添加的項目

這個系列是由另一頁,名爲AddActivityAdvancedPage餵養。在AddActivityAdvancedPage中,用戶填寫表單並保存我將其作爲對象(pmaActivity)發送給MenuPage的信息。 MenuPage接收對象並添加到ObservableCollection上。

的問題是,我的ObservableCollection不要將增加itens!列表框中不顯示itens。

我調試代碼,每次應用打線ListActivitiesAdvanced.Add(pmaActivity);上MenuPage,該ListActivitiesAdvanced是空的。我需要以某種方式將ListActivitiesAdvanced設置爲靜態,但我不知道如何以正確的方式來執行此操作。

AddActivityAdvancedPage類:

public partial class AddActivityAdvancedPage : PhoneApplicationPage 
{ 

    //method called to pass the object pmaActivity as parameter to the MenuPage 
    private void btnSave_Click(object sender, EventArgs e) 
    { 
     Dispatcher.BeginInvoke(() => 
       { 
        PhoneApplicationService.Current.State.Remove("pmaActivity"); 
        PhoneApplicationService.Current.State["pmaActivity"] = pmaActivity; 
        NavigationService.Navigate(new Uri("/MenuPage.xaml", UriKind.Relative)); 
       }); 
    } 

} 

MenuPage類:

public partial class MenuPage : PhoneApplicationPage 
    { 
     public ObservableCollection<PmaActivity> ListActivitiesAdvanced { get; set; } 

     public MenuPage() 
     { 
      InitializeComponent(); 
      ListActivitiesAdvanced = new ObservableCollection<PmaActivity>(); 
     } 

     //Method called to receive the pmaActivity and add in the collection 
     protected override void OnNavigatedTo(NavigationEventArgs e) 
     { 
      if (PhoneApplicationService.Current.State.ContainsKey("pmaActivity")) 
      { 
       PmaActivity pmaActivity = PhoneApplicationService.Current.State["pmaActivity"] as PmaActivity; 
       PhoneApplicationService.Current.State.Remove("pmaActivity"); 
       ListActivitiesAdvanced.Add(pmaActivity); 
      } 
     } 
    } 

列表框在MenuPage:

<ListBox ItemsSource="{Binding ListActivitiesAdvanced}" Margin="0,0,12,0"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal" Height="105" > 
       <Border BorderThickness="1" Width="73" Height="73" BorderBrush="#FF005DFF" Background="#FF005DFF" Margin="0,10,8,0" VerticalAlignment="Top"/> 
       <StackPanel Width="370"> 
        <TextBlock Text="{Binding clientName}" TextWrapping="NoWrap" 
         Margin="12,0,0,0" Style="{StaticResource PhoneTextLargeStyle}"/> 
        <TextBlock Text="{Binding projectName}" TextWrapping="NoWrap" 
         Margin="12,-6,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/> 
       </StackPanel> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

我嘗試從MenuPage刪除ListActivitiesAdvanced並添加斧:名稱來具有相同名稱的ListBox元素:ListActivitiesAdvanced:

<ListBox x:Name="ListActivitiesAdvanced" Margin="0,0,12,0"/> 

但在這種情況下,問題是這個列表不能保存以前添加itens!每次添加項目時,只有最後添加的項目顯示在ObservableCollection上。

感謝您的幫助!我真的有問題,有很多方法來綁定ListBox中的列表(如StaticResource,Source,Binding,List,ObservableCollection,IEnumerable ...),我無法理解所有的差異。

回答

1

如果你想堅持的項目列表中,那麼爲什麼不乾脆把完整列表到應用程序的狀態?

//method called to pass the object pmaActivity as parameter to the MenuPage 
private void btnSave_Click(object sender, EventArgs e) 
{ 
    Dispatcher.BeginInvoke(() => 
    { 
     List<PmaActivity> activities; 
     if (PhoneApplicationService.Current.State.ContainsKey("pmaActivities")) 
      activities = PhoneApplicationService.Current.State["pmaActivities"]; 
     else 
      activities = new List<PmaActivity>(); 
     activities.Add(pmaActivity); 
     PhoneApplicationService.Current.State["pmaActivities"] = pmaActivities; 
     NavigationService.Navigate(new Uri("/MenuPage.xaml", UriKind.Relative)); 
    }); 
} 

然後在主頁上,從列表填充:

//Method called to receive the pmaActivity and add in the collection 
protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    if (PhoneApplicationService.Current.State.ContainsKey("pmaActivity")) 
    { 
     if (PhoneApplicationService.Current.State.ContainsKey("pmaActivities")) 
     { 
      var pmaActivities = PhoneApplicationService.Current.State["pmaActivities"] as List<PmaActivity>; 
      foreach (var activity in pmaActivities) 
       ListActivitiesAdvanced.Add(activity); 
     } 
    } 
+0

謝謝!我想過這個,但對我來說似乎很奇怪。這是通常的方式來做這個wpf? – Dherik 2014-12-13 01:04:47

+1

@Dherik說實話,我不知道。由於您在頁面之間來回導航(尤其是如果您需要在應用程序退出時將列表保存到存儲空間),因此看起來像是一個好選擇。 – McGarnagle 2014-12-13 01:14:48

相關問題