Windows Phone:ListBox 无法显示添加的项目

本文关键字:显示 添加 项目 Phone ListBox Windows | 更新日期: 2023-09-27 18:34:04

我正在尝试在 ObservableCollection 中显示添加的元素,以显示在页面 (MenuPage( 中的列表框上。

此集合由另一个页面提供,称为 AddActivityAdvancedPage。在AddActivityAdvancedPage中,用户填写表单并将我将其作为对象(pmaActivity(发送的信息保存到MenuPage。MenuPage 接收对象并添加到 ObservableCollection 上。

问题是我的可观察集合没有保存添加的它!itens 不会显示在列表框中。

我调试代码,每次应用程序点击菜单页面上ListActivitiesAdvanced.Add(pmaActivity);行时,ListActivitiesAdvanced 都是空的。我需要以某种方式将ListActivitiesAdvanced设置为静态,但我不知道如何正确执行此操作。

添加活动高级页面类:

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

菜单页面类:

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

菜单页中的列表框:

<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,并将 x:Name 添加到具有相同名称的 ListBox 元素:ListActivitiesAdvanced:

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

但在这种情况下,问题是这个列表没有保存之前添加的itens!每次我添加一个项目时,只有最后添加的项目会显示在可观察集合中。

感谢您的任何帮助!我真的有问题,有很多方法可以在ListBox中绑定列表(如StaticResource,Source,Binding,List,ObservableCollection,IEnumerable...(,我无法理解所有的差异。

Windows Phone:ListBox 无法显示添加的项目

如果要保留项目列表,那么为什么不将完整列表放入应用程序状态呢?

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