2017-12-16 46 views
0

안녕하세요 처음 게시자 너무 실수해서 미안 해요. 내 UWP 앱에 문제가있어서 즐겨 찾기 페이지는 사용자가 페이지의 데이터를 재정렬하고 저장할 수있게합니다. 난 개체 참조가 개체 참조가 요구되는 비 정적 필드, 메소드 또는 속성 'Group.Title'CS0120 t 비 정적 필드, 메서드 또는 속성 'Group.Title'에 대한 개체 참조가 필요합니다.

CS0120 필요 CS0120 두번

에서 동일한 오류가있어 비 정적 필드, 메소드 또는 속성 'Group.Items'

아무에게도 이것이 왜 일어나는지 설명 할 수 있습니까? 감사합니다.

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; } 
} 
} 
당신의 foreach에
+0

또 다른 문제는 foreach 반복 변수의 값을 설정하려고하는 것입니다. – Sefe

답변

0

foreach 루프에서 문제가 발생합니다.

  1. 당신은 가능하지만, 클래스 이름 Group과 혼란을 초래 반복 변수로 Group 사용 : 사실, 두 가지 문제가있다. 나는 소문자로 루프 변수 Group을 변경

    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"); 
        } 
    } 
    

    : new ItemData를 인스턴스화 할 때

  2. , 당신은 갑자기 그래서 루프의 수정 된 버전은 다음과 같이 수 대신 Item.Title

Group.Title를 사용 group. 그리고 두 번째 루프 변수 Item을 소문자로 변경했습니다.이 경우에는 필요하지 않지만 C# naming conventions을 더 잘 준수합니다.

+0

안녕하세요, 'group.items'에서 여전히 오류가 발생하는 것을 제외한 모든 부분을 수정했습니다. – Chuggingtonss

+0

@Chuggingtonss Group.Items가 아닌'group.Items'가 있어야한다는 것을 알고 계셨습니까? 케이싱에 유의하십시오. –

+0

@Chuggingtonss 그리고 저는 여러분이 똑같은 문제로 두 번째 루프를 가지고있는 것을 보았습니다. 내 설명은 당신도 그 루프를 해결하는 데 도움이 될 것 같아요 :) –

0

는 클래스의 이름과 충돌 때문에, 대신 그룹의 다른 이름을 사용해야합니다 . 소문자 그룹이 작동합니다.