簡體   English   中英

Windows Phone:ListBox無法顯示添加的項目

[英]Windows Phone: ListBox cannot show added items

我試圖在ObservableCollection中顯示添加的元素,以顯示在頁面(MenuPage)的ListBox中。

此集合由另一個稱為AddActivityAdvancedPage的頁面提供。 在AddActivityAdvancedPage中,用戶填寫該表單並將我將其作為對象(pmaActivity)發送給MenuPage的信息保存。 MenuPage接收對象並添加到ObservableCollection上。

問題是我的ObservableCollection不保存添加的itens! 項目不會顯示在ListBox上。

我調試代碼,並且每當應用程序到達ListActivitiesAdvanced.Add(pmaActivity);我就進行調試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並將ax:Name添加到具有相同名稱的ListBox元素:ListActivitiesAdvanced:

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

但是在這種情況下,問題在於此列表不包含先前添加的itens! 每次我添加一個項目時,只有最后添加的項目才會顯示在ObservableCollection上。

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

如果要保留項目列表,那么為什么不將整個列表置於應用程序狀態呢?

//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);
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM