1

我在 windows phone 8 项目上,我是这个平台的新手。

我在 xaml 方面做得很好,但我生成了另一个新的 web api 类,我无法从 ICollection 绑定任何项目。

这是我的 webapi.cs 类;

    public class General
            {
                public Team Team { get; set; }
                public int Position { get; set; }
                public int LastPosition { get; set; }
                public int Played { get; set; }
                public int Won { get; set; }
                public int Draw { get; set; }
                public int Lost { get; set; }
                public int Scored { get; set; }
                public int Against { get; set; }
                public int Average { get; set; }
                public double Points { get; set; }
                public int SetsWon { get; set; }
                public int SetsLost { get; set; }
                public int SetAverage { get; set; }
                public double WinningPercentage { get; set; }
                public string Description { get; set; }
                public double? GamesBehind { get; set; }
            }
 public class Team
        {
            public int Sport { get; set; }
            public int ID { get; set; }
            public string Name { get; set; }
            public string ShortName { get; set; }
            public bool HasLogo { get; set; }
        }
        public class LeagueName
                {
                    public Stage Stage { get; set; }
                    public List<General> General { get; set; }
                    public List<Home> Home { get; set; }
                    public List<Away> Away { get; set; }
                    public GroupedRows GroupedRows { get; set; }
                    public GroupedRowsHome GroupedRowsHome { get; set; }
                    public GroupedRowsAway GroupedRowsAway { get; set; }
                    public List<GroupType> GroupTypes { get; set; }
                    public List<string> Descriptions { get; set; }
                }

                public class RootObject
                {
                    public ICollection<LeagueName> LeagueNames { get; set; }
                }

这就是我试图绑定的方式;

<!--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="LeagueTable"  Grid.Row="0" Margin="12,17,0,28" DataContext="{Binding LeagueNames}">
            <TextBlock Text="{Binding General.Position}" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock Text="{Binding General.Played}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>
    </Grid>

但是在调试模式下,当我点击相关位置时它总是说这个错误;

System.Windows.Data Error: BindingExpression path error: 'General' property not found on 'System.Collections.Generic.List`1[WinPhone8try1.Apis.LeagueDetailApi+LeagueName]' 'System.Collections.Generic.List`1[WinPhone8try1.Apis.LeagueDetailApi+LeagueName]' (HashCode=17778899). BindingExpression: Path='General.Position' DataItem='System.Collections.Generic.List`1[WinPhone8try1.Apis.LeagueDetailApi+LeagueName]' (HashCode=17778899); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String')..

请帮助我的人。太感谢你了。

更新

    <phone:PhoneApplicationPage
    x:Class="WinPhone8try1.LeagueDetail"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True"
    DataContext="{Binding LeagueName[]}">

    <!--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-->
        <ListBox x:Name="LeagueTable"  Grid.Row="0" Margin="12,17,0,28">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <ListBox>
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <TextBlock Text="{Binding General.Position}" Style="{StaticResource PhoneTextNormalStyle}"/>
                                    <TextBlock Text="{Binding General.Played}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </DataTemplate>
            </ListBox.ItemTemplate>
          </ListBox >
    </Grid>

    <phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBarIconButton IconUri="Images/appbar_back.png" Text="Geri" Click="ApplicationBarIconButton_Click_1"/>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>

</phone:PhoneApplicationPage>

我做了这样的绑定。没有错误,但屏幕上没有显示任何内容。你能帮我解决这个问题吗?请我的朋友?

更新 2

<ListBox x:Name="LeagueTable"  Grid.Row="0" Margin="12,17,0,28">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <ListBox ItemsSource="{Binding General[]}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <TextBlock Text="{Binding Team.Name}" Style="{StaticResource PhoneTextNormalStyle}"/>
                                    <TextBlock Text="{Binding Team.Played}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </DataTemplate>
            </ListBox.ItemTemplate>
          </ListBox>

命名空间WinPhone8try1 {公共部分类LeagueDetail:PhoneApplicationPage {私有字符串json =“”;私人 LeagueDetailApi.LeagueName[] 联赛详情 { 获取;放; }

    public LeagueDetail()
    {
        InitializeComponent();
        DataContext = new LeagueDetailApi.LeagueName();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        string selectedItem = NavigationContext.QueryString["selectedIndex"];
        int index = int.Parse(selectedItem);
        string url = "MY API URL;
        HttpWebRequest hWebRequest = (HttpWebRequest) HttpWebRequest.Create(url);
        hWebRequest.Method = "GET";
        hWebRequest.BeginGetResponse(LeagueTable_Load_Completed, hWebRequest);
    }

    private void LeagueTable_Load_Completed(IAsyncResult ar)
    {
        HttpWebRequest request = (HttpWebRequest) ar.AsyncState;
        HttpWebResponse response = (HttpWebResponse) request.EndGetResponse(ar);
        using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
        {
            json = streamReader.ReadToEnd();
            leaguedetail = JsonConvert.DeserializeObject<LeagueDetailApi.LeagueName[]>(json);
        }
        Deployment.Current.Dispatcher.BeginInvoke(() =>
                                                      {
                                                          LeagueTable.DataContext =
                                                              leaguedetail.ToList();
                                                      });
    }

    private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
    {
        NavigationService.GoBack();
    }
 }

}

4

2 回答 2

0

您将 's 绑定StackPanelDataContext集合,LeagueName每个集合LeagueName都有. 因此,您必须再次绑定列表,或者选择其中一个实例,例如,如果您想要第一个将军:General

[编辑]

一开始我误读了这个问题。您不能将集合绑定到堆栈面板。您可以绑定DataContext(就像您所做的那样),但这对您没有帮助。您要么必须使用嵌套的类似列表的容器,要么选择其中一个LeagueName,与 for 相同General。所以,你需要嵌套列表。

对于所有这些,您可以使用例如ListBox

<ListBox x:Name="LeagueTable"  Grid.Row="0" Margin="12,17,0,28" ItemsSource="{Binding LeagueNames}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <ListBox ItemsSource="{Binding General}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding Position}" Style="{StaticResource PhoneTextNormalStyle}"/>
                            <TextBlock Text="{Binding Played}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>                    
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox >
于 2013-06-13T12:10:08.160 回答
0
DataContext="{Binding LeagueName}"

应该

DataContext="{Binding LeagueNames}"
于 2013-06-13T11:26:36.280 回答