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

时间:2014-12-12 23:50:21

标签: c# windows-phone-8 listbox

我正在尝试在ObservableCollection中显示添加的元素,以显示在Page(MenuPage)中的ListBox上。

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

问题是我的ObservableCollection没有保留添加的itens!列表框中没有显示itens。

我调试代码,每次应用程序点击MenuPage上的行ListActivitiesAdvanced.Add(pmaActivity);时,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:

<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!每次添加项目时,只有最后添加的项目会显示在ObservableCollection上。

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

1 个答案:

答案 0 :(得分: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);
        }
    }