簡體   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