0

所以我有一个列表框,我试图为“成就页面”制作一个列表框。如果我使用列表,一切都很好,但是当我将它切换到列表时,没有显示任何内容,甚至从 xaml...

public partial class achievementPage : PhoneApplicationPage
{
    public string Description { get; set; }
    public string Type { get; set; }
    public achievementPage()
    {
        InitializeComponent();
        loadListbox();
    }
    public achievementPage(string achievementGet, string d1)
    {  
    }
    public void loadListbox()
    {
        achievementStoreage.loadData();
        List<achievementPage> achievementList = new List<achievementPage>();
        achievementList.Add(new achievementPage(achievementStoreage.achievement1, "This is a        test"));
        achievementList.Add(new achievementPage(achievementStoreage.achievement2, "This is    another test")); 
        //List<string> achievementList = new List<string>();
        //achievementList.Add("Sup");
        achievementListBox.ItemsSource = achievementList;
    }
}

<ListBox Name="achievementListBox" Margin="0,0,0,0" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Button Width="776" Height="120" BorderBrush="Black">
                <Button.Content>
                    <StackPanel Orientation="Horizontal" Height="50">
                        <StackPanel Orientation="Horizontal" Height="40">
                            <TextBlock Width="150" Foreground="Black" FontSize="22" Text="Description:" Height="40"/>
                        </StackPanel>
                    </StackPanel>
                </Button.Content>
            </Button>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我得到的只是一个空白页。PS不要担心成就存储,它工作正常。(只是我获取存储数据的地方)

4

2 回答 2

1

天啊。为什么要创建一个achievementPages列表?在您的成就页面上,您将需要一个ListBox带有类型的项目,例如、、、、AchievementItem等。CompletedAchievementNotCompletedAchievement

目前,什么都没有显示,因为您的代码可能会抛出 StackoverflowException(这里不是开玩笑:))。看:你的成就页面构造函数调用 loadListBox 创建两个成就页面并将它们添加到列表中。但是创建两个成就页面会再次导致它们的构造函数被调用两次,这会调用 loadListBox 两次,依此类推。

-- 编辑:好的,没有stackoverflow,我刚刚注意到第二个构造函数。你应该坚持用你知道的大写字母来命名类 :) 无论如何,把 aPage作为 a 的数据项ListBox放在 a 上Page是个坏主意

你想得到的应该看起来更像:

public partial class AchievementPage : PhoneApplicationPage
{
    public string Description { get; set; }
    public string Type { get; set; }

    public AchievementPage()
    {
        InitializeComponent();
        loadListbox();
    }

    public void loadListbox()
    {
        var theList = new List<Achievement>();
        theList.Add(new Achievement{ AchvCount=3, AchvName="medals" });
        theList.Add(new Achievement{ AchvCount=2, AchvName="badges"  }); 
        theList.Add(new Achievement{ AchvCount=6, AchvName="zonks"  }); 

        achievementListBox.ItemsSource = achievementList;
    }
}

public class Achievement : DependencyObject
{
    public int AchvCount {get; set;}
    public string AchvName {get; set;}
}

<ListBox Name="achievementListBox" Margin="0,0,0,0">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="50" />
                    <ColumnDefinition Width="50" />
                    <ColumnDefinition Width="50" />
                </Grid.ColumnDefinitions>

                <TextBlock Grid.Column="0" Text="You've got:" />
                <TextBlock Grid.Column="0" Text="{Binding AchvCount}" />
                <TextBlock Grid.Column="0" Text="{Binding AchvName}" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
于 2012-08-10T23:03:43.360 回答
1

坦率地说,您似乎根本不允许将 ItemTemplates 与 UIElements 一起用作项目的 DataContexts。我试过:

achievementListBox.ItemsSource = new [] {"a","b"};

并且这两个项目都是可见的并且打印了虚拟的“描述”文本,但是我尝试过的以下几行都没有呈现任何内容:

achievementListBox.ItemsSource = new [] { new TextBlock(), new TextBlock()};
achievementListBox.ItemsSource = new [] { new Rectangle(), new Rectangle()};
achievementListBox.ItemsSource = new [] { new Grid(), new Grid()};

尝试使用您的自定义页面 - 相同。没有显示项目。

这是非常具有误导性的。项目显示,但请看上面的行:控件被创建为empty,没有设置内容!。

事实证明,如果 ListBox 检测到 Item 是 UIElement,那么它不会使用 ItemTemplate,而是直接呈现该 UIElement!

achievementListBox.ItemsSource = new[] { new TextBlock() { Text = "bbb" }, new TextBlock() { Text = "eee" } };
achievementListBox.ItemsSource = new[] { new Rectangle() { Fill = new SolidColorBrush(Colors.Red), Width = 30, Height = 10 }, new Rectangle() { Fill = new SolidColorBrush(Colors.Green), Width = 30, Height = 10 } };

var gridA = new Grid() { Width = 110, Height = 40 }; gridA.Children.Add(new Rectangle() { Fill = new SolidColorBrush(Colors.Red) });
var gridB = new Grid() { Width = 110, Height = 40 }; gridB.Children.Add(new Rectangle() { Fill = new SolidColorBrush(Colors.Green) });
achievementListBox.ItemsSource = new[] { gridA, gridB };

以上三个示例都完全忽略了 ListBox.ItemTemplate,而是直接显示两个项目:两个文本框、两个矩形、两个更大的矩形(在一个网格中)。

回到您的案例:这意味着使用您的原始设置,ListBox 也会尝试直接显示项目 - 因为您的自定义页面是 UIElement。它确实做到了!但是您的页面是……空的。在重载的构造函数中,您InitializeComponent();通过读取 XAML 代码省略了构造视图的 。这是一个更正的示例,它显示了三次“Hello”:一次只是因为它位于页面上,接下来两次是因为 ListBox 行设置为同一页。

请原谅我重命名了这些类,我只是开始了一个新项目,而不是粘贴您的代码。
请注意,我必须向 XAML 添加一些其他控件,因为用作数据项的页面将显示为空,因为它们没有设置项

public partial class MainPage : PhoneApplicationPage
{
    public string Description { get; set; }
    public string Type { get; set; }

    public MainPage()
    {
        InitializeComponent();
        loadListbox();
    }

    public MainPage(string achievementGet, string d1)
    {
        InitializeComponent();
        someText.Text = d1;
    }

    public void loadListbox()
    {
        achievementListBox.ItemsSource = new[] { new MainPage(null, "ddd"), new MainPage(null, "ccc") };
    }
}

<StackPanel>
    <TextBlock>
        <Run Text="Hello" />
        <Run Text=" " />
        <Run x:Name="someText" />
    </TextBlock>

    <ListBox Name="achievementListBox" Margin="0,0,0,0">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Width="150" Foreground="White"
                           FontSize="22" Text="This DataTemplate is IGNORED because the Item is UIElement"
                           Height="40"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>

我尝试以与您类似的方式来塑造代码,只是删除了一些与问题无关的行。我希望这能解释你现在的一切:)

于 2012-08-11T14:23:56.333 回答