繁体   English   中英

Windows Phone 8.1将数据从数据源绑定到XAML(HUB模板)

[英]Windows phone 8.1 binding data to XAML from datasource (HUB template)

我需要帮助...

所以从本质上讲,我一直在看WP 8.1开发教程,并且有一个使用标准HUB模板的教程。

因此,我尝试配置此模板供自己使用。 我有一个JSON文件,其中包含我想在页面上列出的所有我需要的数据(公寓)。 我“重新创建了” SampleDataSource,它在我的应用程序中只是DataSource。 我认为数据一切正常,现在出现了翻译数据并将其绑定到XAML的问题。 我真的不知道为什么它不起作用,因为我刚刚尝试更改模板中有效的参数(由SDK提供)。 你们可以帮我还是给我一些指导,在哪里可以找到这样做的例子。

如果有人愿意帮助我或向我提供Skype / TW,只是为了向我展示结构和我必须做的事情,我将不胜感激,因为我那时会知道它的基本翻译和工作原理,因此我可以从事其他方面的工作。项目! 有比特币作为奖励! :)

提前谢谢。

编辑:添加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>

在此处输入图片说明

这里有一些问题:

  • 您的ApartmentData类具有一些名为Cover_photo属性。 这通常不是C#使用的样式约定,因此我将它们重命名为CoverPhoto
  • 在同一主题上,您的ApartmentData属性的名称类似于Cover_photo但在JSON(这是典型的JSON样式)中,它们的名称类似于cover_photo 您正在像这样访问JSON键:

     apartmentObject["Cover_photo"].GetString() // KeyNotFoundException 

    但应该是:

     apartmentObject["cover_photo"].GetString() 

    这将在运行时工作, 设计人员将不会执行此代码。 设计器将读取JSON文件,并尝试将JSON文件中的键与您指定的类型的属性进行匹配,但是由于键和属性不匹配,因此无法执行。 所以你要么:

    1. 修改JSON,以使键匹配诸如CoverPhoto之类的属性。
    2. 使用DataContractAttribute注释您的类型。 这使您可以使用DataMemberAttribute指定JSON和.NET类型之间的映射,但这主要是因为您获得了JSON序列化/反序列化!

我向您发送了包含这些更改的请求请求。 此外,您的设计数据非常庞大(〜1000个项目!)。 它只能包含约5个项目,因为无论如何您将无法在设计器中看到所有项目。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM