我需要帮助......
基本上我一直在看WP 8.1开发教程,并且有一个标准HUB模板的教程。
所以我尝试配置这个模板供我自己使用。我有一个JSON文件,其中包含我需要的所有数据(公寓),我想在我的页面上列出。我“重新创建”SampleDataSource,它在我的应用程序中只是DataSource。我认为数据一切正常,现在出现了翻译这些数据并将其绑定到XAML的问题。我真的不知道为什么它不起作用,因为我刚刚尝试更改模板中的参数(由SDK提供)。你能帮助我或给我一些指示,我可以在哪里找到这样做的例子。
如果有人愿意帮我或Skype跟我一起只是为了告诉我结构和我要做的事情,我会很感激,因为我会知道这基本上是如何翻译和工作所以我可以在其他工作项目的各个部分!有比特币作为奖励! :)
提前谢谢。编辑:添加Github回购:https://github.com/lklancir/zimmerfrei_wp/tree/master/ZimmerFrei_v0.1
以下是DataSource.cs :
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Data.Json;
using Windows.Storage;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
命名空间ZimmerFrei.Data {
public class ApartmentData
{
public ApartmentData(String id, String name, String description, String capacity, String stars, String address, String email, String phone, String phone2, String rating, String lat, String lng, String price, String cover_photo, String owner_id, String type_id, String city_id)
{
this.Id = id;
this.Name = name;
this.Description = description;
this.Capacity = capacity;
this.Stars = stars;
this.Address = address;
this.Email = email;
this.Phone = phone;
this.Phone2 = phone2;
this.Rating = rating;
this.Lat = lat;
this.Lng = lng;
this.Price = price;
this.Cover_photo = cover_photo;
this.Owner_id = owner_id;
this.Type_id = type_id;
this.City_id = city_id;
}
public string Id { get; private set; }
public string Name { get; private set; }
public string Description { get; private set; }
public string Capacity { get; private set; }
public string Stars { get; private set; }
public string Address { get; private set; }
public string Email { get; private set; }
public string Phone { get; private set; }
public string Phone2 { get; private set; }
public string Rating { get; private set; }
public string Lat { get; private set; }
public string Lng { get; private set; }
public string Price { get; private set; }
public string Cover_photo { get; private set; }
public string Owner_id { get; private set; }
public string Type_id { get; private set; }
public string City_id { get; private set; }
public override string ToString()
{
return this.Name;
}
}
public sealed class DataSource
{
private static DataSource _dataSource = new DataSource();
private ObservableCollection<ApartmentData> _apartments = new ObservableCollection<ApartmentData>();
public ObservableCollection<ApartmentData> Apartments
{
get { return this._apartments; }
}
public static async Task<IEnumerable<ApartmentData>> GetApartmentsAsync()
{
await _dataSource.GetDataAsync();
return _dataSource.Apartments;
}
public static async Task<ApartmentData> GetApartmentAsync(string id)
{
await _dataSource.GetDataAsync();
var matches = _dataSource.Apartments.Where((apartment) => apartment.Id.Equals(id));
if (matches.Count() == 1) return matches.First();
return null;
}
private async Task GetDataAsync()
{
if (this._apartments.Count != 0)
return;
Uri dataUri = new Uri("ms-appx:///DataModel/Apartments.json");
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);
string jsonText = await FileIO.ReadTextAsync(file);
JsonObject jsonObject = JsonObject.Parse(jsonText);
JsonArray jsonArray = jsonObject["apartments"].GetArray();
foreach (JsonValue apartmentValue in jsonArray)
{
JsonObject apartmentObject = apartmentValue.GetObject();
ApartmentData apartment = new ApartmentData(apartmentObject["Id"].GetString(),
apartmentObject["Name"].GetString(),
apartmentObject["Description"].GetString(),
apartmentObject["Capacity"].GetString(),
apartmentObject["Stars"].GetString(),
apartmentObject["Address"].GetString(),
apartmentObject["Email"].GetString(),
apartmentObject["Phone"].GetString(),
apartmentObject["Phone2"].GetString(),
apartmentObject["Rating"].GetString(),
apartmentObject["Lat"].GetString(),
apartmentObject["Lng"].GetString(),
apartmentObject["Price"].GetString(),
apartmentObject["Cover_photo"].GetString(),
apartmentObject["Owner_id"].GetString(),
apartmentObject["Type_id"].GetString(),
apartmentObject["City_id"].GetString());
this.Apartments.Add(apartment);
}
}
}
}
这是 ListPage.xaml (我要写出我的数据)
<Page
x:Class="ZimmerFrei_v0._1.ListPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ZimmerFrei_v0._1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:data="using:ZimmerFrei.Data"
DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
d:DataContext="{Binding Source={d:DesignData Source=/DataModel/Apartments.json, Type=data:DataSource}}"
mc:Ignorable="d" FontFamily="Global User Interface">
<Page.Resources>
<DataTemplate x:Key="StandardTripleLineItemTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Background="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}" Margin="0,9.5,0,0" Grid.Column="0" HorizontalAlignment="Left">
<Image Source="{Binding Cover_photo}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}" Height="79" Width="79"/>
</Border>
<StackPanel Grid.Column="1" Margin="14.5,0,0,0">
<TextBlock Text="{Binding Name}" Style="{ThemeResource ListViewItemTextBlockStyle}" FontFamily="Global User Interface"/>
<TextBlock Text="{Binding Description}" Style="{ThemeResource ListViewItemContentTextBlockStyle}" Foreground="{ThemeResource PhoneMidBrush}" FontFamily="Global User Interface" />
<TextBlock Text="{Binding Stars}" Style="{ThemeResource ListViewItemSubheaderTextBlockStyle}" />
</StackPanel>
</Grid>
</DataTemplate>
</Page.Resources>
<Grid x:Name="LayoutRoot">
<Hub x:Name="Hub" x:Uid="Hub" Header="ZimmerFrei">
<HubSection x:Uid="HubSection" Header="APARTMENTS"
DataContext="{Binding Apartments}" >
<DataTemplate>
<ListView
AutomationProperties.AutomationId="ItemListViewSection3"
AutomationProperties.Name="Items In Group"
SelectionMode="None"
IsItemClickEnabled="True"
ItemsSource="{Binding apartment}"
ItemTemplate="{StaticResource StandardTripleLineItemTemplate}"
ItemClick="ItemView_ItemClick"
ContinuumNavigationTransitionInfo.ExitElementContainer="True">
</ListView>
</DataTemplate>
</HubSection>
</Hub>
</Grid>
答案 0 :(得分:1)
这里有一些问题:
ApartmentData
类有一些名为Cover_photo
的属性。这通常不是C#使用的样式约定,因此我将它们重命名为CoverPhoto
。在同一主题上,您的ApartmentData
属性的名称与Cover_photo
类似,但它们的名称与JSON中的cover_photo
相同(这是一种典型的JSON样式)。你正在访问这样的JSON密钥:
apartmentObject["Cover_photo"].GetString() // KeyNotFoundException
但它应该是:
apartmentObject["cover_photo"].GetString()
这将在运行时运行,但设计器不会执行此代码。设计器读取JSON文件并尝试将JSON文件中的键与您指定类型的属性进行匹配,但由于键和属性不匹配,因此无法进行匹配。所以要么你:
CoverPhoto
等属性匹配。DataContractAttribute
为您的类型添加注释。这允许您使用DataMemberAttribute
指定JSON和.NET类型之间的映射,但主要是因为您获得了JSON序列化/反序列化!我发送了一条带有这些更改的拉取请求。此外,您的设计数据是巨大的(约1000项!)。它应该只包含~5个项目,因为无论如何你都无法看到设计师中的所有项目。