【问题标题】:How do I create a dynamic table with rows and columns using the windows 7 platform如何使用 windows 7 平台创建具有行和列的动态表
【发布时间】:2012-05-11 04:09:25
【问题描述】:

我正在为轮滑德比制作一个应用程序,该应用程序将获取诸如比赛、球队名称和统计数据等信息。信息以 JSON 格式存储。当我最终获取并解析数据时,我想以类似于以下格式排列:Team1 Team2 Venue Date Result。行数应该是每个回合列表的长度。我无法在 C# 代码中找到执行此操作的方法。谁能指出我正确的方向。

编辑:我已经尝试实现一个列表框,我正在尝试按照此处找到的教程http://msdn.microsoft.com/en-us/library/cc265158%28v=vs.95%29.aspx

当我运行程序时,我收到此错误消息:错误 1 ​​事件处理程序 'PhoneApplicationPage_Loaded' not found on class 'PhoneApp2.Page1' c:\users\lewis\documents\visual studio 2010\Projects\PhoneApp2\PhoneApp2\Page1 .xaml Roller Derby 应用程序

这是我的第 1 页 Xaml

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock x:Name="PageTitle" Text="Upcoming Bouts" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" Width="297" TextWrapping="NoWrap" FontSize="40" HorizontalAlignment="Left" />
        <Button Name="Home" Content="Home" Width="310" HorizontalContentAlignment="Center" HorizontalAlignment="Left" Click="Home_Click" />
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ListBox Height="100" HorizontalAlignment="Left" Margin="544,177,0,0" Name="ListofBouts" VerticalAlignment="Top" Width="460" Loaded="ListofBouts_Loaded">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Padding="5,0,5,0"
                     Text="{Binding Venue}" />
                        <TextBlock Text="{Binding Team1}" />
                        <TextBlock Text="{Binding Team2}"/>
                        <TextBlock Text="{Binding Date}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>

            </ListBox>

    </Grid>
</Grid>

<!--Sample code showing usage of ApplicationBar-->
<!--<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
        <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
        <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
        <shell:ApplicationBar.MenuItems>
            <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
            <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
        </shell:ApplicationBar.MenuItems>
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>-->

这里是 C#

namespace PhoneApp2
{
  public partial class Page1 : PhoneApplicationPage
  {
    public Page1()
    {
        InitializeComponent();
    }

    private void Home_Click(object sender, RoutedEventArgs e)
    {
        NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
    }

    private void ListofBouts_Loaded(object sender, RoutedEventArgs e)
    {
        InitializeComponent();
    }
    public class Bouts
    {
        public string Venue { get; set; }
        public string Team1 { get; set; }
        public string Team2 { get; set; }
        public string Date { get; set; }

        public Bouts(String venue, String team1, String team2, String date)
        {

            this.Venue = venue;
            this.Team1 = team1;
            this.Team2 = team2;
            this.Date = date;

        }
    }


    public class bouts : ObservableCollection<Bouts>
    {
        public bouts()
        {
            Add(new Bouts("Yankee Sadium", "Yankees", "Mets", "March 3"));
            Add(new Bouts("Yankee Sadium", "Yankees", "Mets", "March 3"));
            Add(new Bouts("Yankee Sadium", "Yankees", "Mets", "March 3"));
            Add(new Bouts("Yankee Sadium", "Yankees", "Mets", "March 3"));

        }
    }

  }
}

【问题讨论】:

  • 你使用反序列化吗?喜欢在这里找到:stackoverflow.com/questions/1207731/…
  • 感谢您提供非常有用的链接。但是,我实际上想将虚拟图放入列表框中。当我运行时,我收到错误消息:错误 1 ​​事件处理程序 'PhoneApplicationPage_Loaded' not found on class 'PhoneApp2.Page1' c:\users\lewis\documents\visual studio 2010\Projects\PhoneApp2\PhoneApp2\Page1.xaml Roller Derby应用

标签: c# .net silverlight windows-phone-7 wpfdatagrid


【解决方案1】:

错误是说有一个事件为Page1 定义了Loaded 事件。由于事件未在代码隐藏中定义,因此必须在 XAML 中声明:

<phone:PhoneApplicationPage
  x:Class="PhoneApp2.Pag1"
  Loaded="PhoneApplicationPage_Loaded"
  OtherStuff...
  >
  <RestOfXaml>

删除 Loaded="PhoneApplicationPage_Loaded",错误就会消失。或者在后面的代码中定义事件。两者都应该工作。

【讨论】:

  • 抱歉回复晚了。我在发布编辑后发现这是问题所在。现在我只需要 JSON 来检索数据。
  • 如果这个答案解决了原来的问题,请标记为答案。欢迎来到 S.O.!
猜你喜欢
  • 2021-03-14
  • 2018-07-22
  • 1970-01-01
  • 1970-01-01
  • 2018-12-02
  • 2018-02-10
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
相关资源
最近更新 更多