私は 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 { public 部分クラス LeagueDetail : PhoneApplicationPage { プライベート文字列 json = ""; プライベート LeagueDetailApi.LeagueName[] Leaguedetail { get; 設定; }
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();
}
}
}