2017-12-16 17 views
0

初めての投稿ですので、ご迷惑をおかけして申し訳ありません。私のUWPアプリケーションに問題があり、ページ上のデータを並べ替えて保存することができます。私は、オブジェクト参照がオブジェクト参照が要求される非静的フィールド、メソッド、またはプロパティ「Group.Title」CS0120 t非静的なフィールド、メソッド、またはプロパティ 'Group.Title'のオブジェクト参照が必要です

CS0120のために必要であるCS0120 2回

に同じエラーを取得しています非静的なフィールド、メソッド、またはプロパティ 'Group.Items'

私は誰にもこれが起こっている理由を説明することができました ありがとう。あなたのforeachの中

CSファイル


using App1.Common; 
using App1.Data; 
using Newtonsoft.Json; 
using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using Windows.Storage; 
using Windows.UI.Popups; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Navigation; 
using System.Linq; 
using System.Threading.Tasks; 
using Windows.Data.Json; 


namespace App1 
{ 

public sealed partial class test : Page 
{ 
    public ObservableCollection<ItemData> Items { get; set; } 

    private ObservableDictionary defaultViewModel = new 
    ObservableDictionary(); 
    private NavigationHelper navigationHelper; 
    private RootObject jsonLines; 
    private StorageFile fileFavourites; 
    private Dictionary<string, ItemData> ItemData = new Dictionary<string, 
    ItemData>(); 


    public test() 
    { 
    loadJson(); 
    getFavoritesFile(); 

    this.InitializeComponent(); 
    this.navigationHelper = new NavigationHelper(this); 
    this.navigationHelper.LoadState += navigationHelper_LoadState; 
    } 

    void ItemView_ItemClick(object sender, ItemClickEventArgs e) 
    { 
     // Navigate to the appropriate destination page, configuring the new 
     page 
     // by passing required information as a navigation parameter 
     var itemId = ((SampleDataItem)e.ClickedItem).UniqueId; 
     this.Frame.Navigate(typeof(GroupedItemsPage), itemId); 
    } 

    private void setupObservableCollection() 
     { 
      Items = new ObservableCollection<ItemData>(ItemData.Values); 
      itemGridView.ItemsSource = Items; 
     } 

     private async void loadJson() 
     { 
      var file = await StorageFile.GetFileFromApplicationUriAsync(new 
      Uri("ms-appx:///DataModel/SampleData.json")); 
      var lines = await FileIO.ReadTextAsync(file); 
      jsonLines = JsonConvert.DeserializeObject<RootObject>(lines); 
      feedItems(); 
     } 

     private async void getFavoritesFile() 
     { 
      Windows.Storage.StorageFolder storageFolder = 
      Windows.Storage.ApplicationData.Current.LocalFolder; 
      fileFavourites = await storageFolder.GetFileAsync("Fav.txt"); 
     } 

     private async void feedItems() 
     { 
      if (await FileIO.ReadTextAsync(fileFavourites) != "") 
      { 
       foreach (var line in await 
       FileIO.ReadLinesAsync(fileFavourites)) 
       { 
        foreach (var Group in jsonLines.Groups) 
        { 
        foreach (var Item in Group.Items) 
        { 
         if (Item.UniqueId == line) 
         { 
          var storage = new ItemData() 
          { 
           Title = Item.Title, 
           UniqueID = Item.UniqueId, 
           ImagePath = Item.ImagePath, 
           Group = Group.Title 
          }; 
          ItemData.Add(storage.UniqueID, storage); 
         } 
        } 
       } 
       } 
      } 
      else 
      {//if favourites file is empty, use? 
      foreach (var Group in jsonLines.Groups) ; 
      { 
       foreach (var Item in Group.Items) 
       { 
        var storage = new ItemData() 
        { 
         Title = Item.Title, 
         UniqueID = Item.UniqueId, 
         ImagePath = Item.ImagePath, 
         Group = Group.Title 
        }; 
        ItemData.Add(storage.UniqueID, storage); 
        await FileIO.AppendTextAsync(fileFavourites, 
        Item.UniqueId + "\r\n"); 
       } 
      } 
     } 


     setupObservableCollection(); 
     } 


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

     #region NavigationHelper loader 

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

     private async void MessageBox(string Message) 
     { 
      MessageDialog dialog = new MessageDialog(Message); 
      await dialog.ShowAsync(); 
     } 

     private async void navigationHelper_LoadState(object sender, 
     LoadStateEventArgs e) 
     { 
      var sampleDataGroups = await SampleDataSource.GetGroupsAsync(); 
      this.defaultViewModel["Groups"] = sampleDataGroups; 
     } 

     #endregion NavigationHelper loader 

     #region NavigationHelper registration 

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

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

     #endregion NavigationHelper registration 
    } 

    public class ItemData 
    { 
     public string UniqueID { get; set; } 
     public string Title { get; set; } 
     public string Group { get; set; } 
     public string ImagePath { get; set; } 
    } 

} 

RootObjectファイル

using App1.Data; 
using System.Collections; 
using System.Collections.Generic; 
using System.Threading.Tasks; 

namespace App1 
{ 

public class Item 
{ 
    public string UniqueId { get; set; } 
    public string Title { get; set; } 
    public string Subtitle { get; set; } 
    public string ImagePath { get; set; } 
    public string Description { get; set; } 
    public string Content { get; set; } 
} 

public class Group 
{ 
    public string UniqueId { get; set; } 
    public string Title { get; set; } 
    public string Subtitle { get; set; } 
    public string ImagePath { get; set; } 
    public string Description { get; set; } 
    public List<Item> Items { get; set; } 
} 

public class RootObject 
{ 
    public List<Group> Groups { get; set; } 
} 
} 

+0

あなたが直面するもう一つの問題は、foreach反復変数の値を設定しようとすることです。 – Sefe

答えて

0

foreachループで問題が発生します。

  1. あなたが可能である反復変数としてGroupを使用しますが、クラス名Groupとの混乱につながる:実際には、2つの問題があります。

    foreach (var group in jsonLines.Groups) ; 
    { 
        foreach (var item in group.Items) 
        { 
         var storage = new ItemData() 
         { 
          Title = item.Title, 
          UniqueID = item.UniqueId, 
          ImagePath = item.ImagePath, 
          Group = item.Title 
         }; 
         ItemData.Add(storage.UniqueID, storage); 
         await FileIO.AppendTextAsync(fileFavourites, 
           Item.UniqueId + "\r\n"); 
        } 
    } 
    

    私は小文字にループ変数Groupを変更:new ItemDataをインスタンス化する場合

  2. 、あなたは突然ので、ループの修正版は、次のようになります代わりにItem.Title

Group.Titleを使用しますgroup。そして、2番目のループ変数Itemも小文字に変更しました。これはあなたのケースでは必要ではありませんが、C# naming conventionsに準拠しています。

+0

これは私がまだ 'group.items'のエラーを受け取る以外はすべて修正します – Chuggingtonss

+0

@Chuggingtonss 'Group.Items'ではなく' group.Items'でなければならないことに注意しましたか?ケーシングに気をつけてください。 –

+0

@Chuggingtonssそして、私はあなたに同じ問題を持つ2番目のループがあることを見ました。私は私の説明があなたもそのループを修正するのに役立つはずだと思う:) –

0

それはクラスの名前と競合するため、あなたは、代わりに、グループの他の名前を使用する必要があります。小文字のグループが機能します。

関連する問題