【发布时间】:2016-08-22 10:10:09
【问题描述】:
我有一个工作 GridView 的示例,但似乎无法将其移动到 HubSection 内工作。我真正的应用程序需要这样做,这个例子是我了解数据绑定需要如何工作的骡子。
HubSection 抱怨它不能有一个 GridView 作为一个孩子。
我当前的模型代码如下所示:
namespace Quickstart {
public class Recording {
public string ArtistName { get; set; }
public string CompositionName { get; set; }
public DateTime ReleaseDateTime { get; set; }
public Uri ImageUri { get; set; }
public Recording(string name, string composition, DateTime when, string prefixedFilename)
{
this.ArtistName = name;
this.CompositionName = composition;
this.ReleaseDateTime = when;
// string prefixedFilename = "ms-appx://Quickstart/Assets/" + filename;
ImageUri = new Uri(prefixedFilename);
}
public string OneLineSummary {
get
{
return $"{this.CompositionName} by {this.ArtistName}, released: "
+ this.ReleaseDateTime.ToString("d");
}
}
}
public class RecordingViewModel {
List<Recording> recordings;
public RecordingViewModel()
{
recordings = new List<Quickstart.Recording>();
recordings.Add(new Recording("Wolfgang Amadeus Mozart", "Andante in C for Piano", new DateTime(1761, 1, 1), "http://csimg.koopkeus.nl/srv/NL/29023839m56849/T/340x340/C/FFFFFF/url/mozart.jpg"));
recordings.Add(new Recording("Nickleback", "Gotta be Somebody", new DateTime(2003, 8, 21), "http://images4.fanpop.com/image/photos/16500000/n-nickelback-16579001-634-634.jpg"));
}
public List<Recording> RecordingList { get { return this.recordings; } }
}
}
和 xaml:
<Page
x:Class="Quickstart.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Quickstart"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<GridView ItemsSource="{x:Bind recordings}" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" x:Name="RecordingGrid">
<GridView.ItemTemplate>
<DataTemplate x:DataType="local:Recording">
<StackPanel>
<Image Source="{Binding ImageUri, Mode=TwoWay}" Height="100" Opacity="1" Stretch="Uniform"/>
<TextBlock Text="{x:Bind ArtistName}"/>
<TextBlock Text="{x:Bind CompositionName}"/>
<TextBlock Text="{x:Bind ReleaseDateTime}"/>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Page>
最后是 xaml.cs:
namespace Quickstart {
public sealed partial class MainPage : Page {
List<Recording> recordings;
public MainPage() {
this.InitializeComponent();
recordings = new RecordingViewModel().RecordingList;
}
}
}
如您所见,它非常简单!谢谢你的时间!
【问题讨论】:
-
到底是什么问题?我什至没有在您的代码中看到 HubSection。
-
请在中心部分发布代码。您还需要将
GridView放入Grid内HubSection
标签: c# windows data-binding win-universal-app