在 DataSource 命名空间中找不到方法

Cant find method in DataSource namespace

所以我有 2 个文件。一个是我获取数据的 DataSource,另一个是 WindowsPhone 中的 ListPage,我想在其中绑定数据...

我使用与 Windows Phone Hub 模板完全相同的方式进行此操作,只是根据我的需要对其进行调整..

问题发生在我的 ListPage.xaml.cs 上,我需要引用 中的 GetApartmentsAsync() 方法DataSource.cs。我只是不明白为什么我找不到它。我包含了命名空间,方法是 public 但它只是找不到它...

再一次,我在 Visual Studio 中与 HUB 模板并行执行此操作,在他们的版本中,通常可以调用该方法...

现在我将两个代码文件都贴上来,希望你们能以某种方式帮助我...

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;

namespace 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.cs

    using ZimmerFrei_v0._1.Common;
using ZimmerFrei.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.Resources;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Display;
using Windows.UI.Core;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;



namespace ZimmerFrei_v0._1
{

public sealed partial class ListPage : Page
{
    private NavigationHelper navigationHelper;
    private ObservableDictionary defaultViewModel = new ObservableDictionary();
    private readonly ResourceLoader resourceLoader = ResourceLoader.GetForCurrentView("Resources");

    public ListPage()
    {
        this.InitializeComponent();

        DisplayInformation.AutoRotationPreferences = DisplayOrientations.Portrait;

        this.NavigationCacheMode = NavigationCacheMode.Required;

        this.navigationHelper = new NavigationHelper(this);
        this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
        this.navigationHelper.SaveState += this.NavigationHelper_SaveState;
    }


    public NavigationHelper NavigationHelper
    {
        get { return this.navigationHelper; }
    }


    public ObservableDictionary DefaultViewModel
    {
        get { return this.defaultViewModel; }
    }


    private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
    {
        var apartmentData = await ApartmentData.GetApartmentsAsync();
        this.DefaultViewModel["apartments"] = apartmentData;

    }


    private void NavigationHelper_SaveState(object sender, SaveStateEventArgs e)
    {
    }

    #region NavigationHelper registration

    /// <summary>
    /// The methods provided in this section are simply used to allow
    /// NavigationHelper to respond to the page's navigation methods.
    /// <para>
    /// Page specific logic should be placed in event handlers for the  
    /// <see cref="NavigationHelper.LoadState"/>
    /// and <see cref="NavigationHelper.SaveState"/>.
    /// The navigation parameter is available in the LoadState method 
    /// in addition to page state preserved during an earlier session.
    /// </para>
    /// </summary>
    /// <param name="e">Provides data for navigation methods and event
    /// handlers that cannot cancel the navigation request.</param>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        this.navigationHelper.OnNavigatedTo(e);
    }

    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        this.navigationHelper.OnNavigatedFrom(e);
    }

    #endregion
}

}

class ApartmentData 没有名为 GetApartmentsAsync 的方法。该方法存在于密封 class DataSource.

您尝试调用的方法是在 DataSource 中实现的,而不是在 AppartmentData 中实现的。试试看

var apartmentData = await DataSource.GetApartmentsAsync();