2011-07-28 2 views
0

검색 한 데이터를 정렬하려고합니다. 두 개의 데이터 시간과 첨부 된 제목이 있습니다. 나는 시간을 분류 할 수 있었다. 그러나 첨부 된 메모는 정렬 된 정렬과 조정되지 않았습니다. 예를 들어Windows 전화 배열 배열 7

, 시간 -> 오전 10시 45분 첨부 제목 -> 숙제 오전 8시 45분 -

을 작업>는 시간 오전 8시 45분 숙제 오전 10시 50분 작업 시간에

을 정렬 보호 무효 OnNavigatedTo (System.Windows.Navigation.NavigationEventArgs 전자)

{ 
     base.OnNavigatedTo(e); 
     selectedFolderName = ""; 

     if (NavigationContext.QueryString.TryGetValue("selectedFolderName", out selectedFolderName)) 
      selectedFolderName1 = selectedFolderName; 



     IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication(); 
     //For time 
     try 
     { 
      StreamReader readFileTime = new StreamReader(new IsolatedStorageFileStream(selectedFolderName1 + "\\time.Schedule", FileMode.Open, myStore)); 
      //For title 
      StreamReader readFileTitle = new StreamReader(new IsolatedStorageFileStream(selectedFolderName1 + "\\title.Schedule", FileMode.Open, myStore)); 
      //For category 
      StreamReader readFileCategory = new StreamReader(new IsolatedStorageFileStream(selectedFolderName1 + "\\category.Schedule", FileMode.Open, myStore)); 


      String timeText = readFileTime.ReadLine(); 
      timeSplit = timeText.Split(new char[] { '^' }); 
      Array.Sort(timeSplit); 

      String titleText = readFileTitle.ReadLine(); 
      titleSplit = titleText.Split(new char[] { '^' }); 


      String categoryText = readFileCategory.ReadLine(); 
      categorySplit = categoryText.Split(new char[] { '^' }); 

     } 
     catch (Exception) 
     { 
      // noScheduleTxt.Visibility = Visibility.Visible; 
     } 



     if (scheduleListBox.Items.Count == 0) 
     { 

      for (int i = 0; i < timeSplit.Length; i++) 
      { 
       string timeList = timeSplit[i]; 
       string titleList = titleSplit[i]; 
       string categoryList = categorySplit[i]; 

       //Define grid column, size 
       Grid schedule = new Grid(); 
       //Column to hold the time of the schedule 
       ColumnDefinition timeColumn = new ColumnDefinition(); 
       GridLength timeGrid = new GridLength(110); 
       timeColumn.Width = timeGrid; 
       schedule.ColumnDefinitions.Add(timeColumn); 


       //Text block that show the time of the schedule 
       TextBlock timeTxtBlock = new TextBlock(); 
       timeTxtBlock.Text = timeList; 
       //Set the alarm label text block properties - margin, fontsize 
       timeTxtBlock.FontSize = 28; 
       timeTxtBlock.Margin = new Thickness(0, 20, 0, 0); 
       //Set the column that will hold the time of the schedule 
       Grid.SetColumn(timeTxtBlock, 0); 
       schedule.Children.Add(timeTxtBlock); 



       //Column to hold the title of the schedule 
       ColumnDefinition titleColumn = new ColumnDefinition(); 
       GridLength titleGrid = new GridLength(300); 
       titleColumn.Width = titleGrid; 
       schedule.ColumnDefinitions.Add(titleColumn); 

       //Text block that show the title of the schedule 
       TextBlock titleTxtBlock = new TextBlock(); 
       titleTxtBlock.Text = titleSplit[i]; 

       if (titleSplit[i].Length > 15) 
       { 
        string strTitle = titleSplit[i].Substring(0, 15) + "...."; 
        titleTxtBlock.Text = strTitle; 
       } 
       else 
       { 
        titleTxtBlock.Text = titleSplit[i]; 
       } 
       //Set the alarm label text block properties - margin, fontsize 
       titleTxtBlock.FontSize = 28; 
       titleTxtBlock.Margin = new Thickness(20, 20, 0, 0); 
       //Set the column that will hold the title of the schedule 
       Grid.SetColumn(titleTxtBlock, 1); 
       schedule.Children.Add(titleTxtBlock); 



       //Column 3 to hold the image category of the schedule 
       ColumnDefinition categoryImageColumn = new ColumnDefinition(); 
       GridLength catImgnGrid = new GridLength(70); 
       categoryImageColumn.Width = catImgnGrid; 
       schedule.ColumnDefinitions.Add(categoryImageColumn); 

       //Text block that show the category of the schedule 
       TextBlock categoryTxtBlock = new TextBlock(); 
       categoryTxtBlock.Text = categorySplit[i]; 

       //set the category image and its properties - margin, width, height, name, background, font size 
       Image categoryImage = new Image(); 
       categoryImage.Margin = new Thickness(-20, 15, 0, 0); 
       categoryImage.Width = 50; 
       categoryImage.Height = 50; 
       if (categoryTxtBlock.Text == "Priority") 
       { 
        categoryImage.Source = new BitmapImage(new Uri("/AlarmClock;component/Images/exclamination_mark.png", UriKind.Relative)); 
       } 
       else 
        if (categoryTxtBlock.Text == "Favourite") 
        { 
         categoryImage.Source = new BitmapImage(new Uri("/AlarmClock;component/Images/star_full.png", UriKind.Relative)); 
        } 


       Grid.SetColumn(categoryImage, 2); 
       schedule.Children.Add(categoryImage); 

       scheduleListBox.Items.Add(schedule); 
      } 


     } 
    } 

답변

3

문제가를 오버라이드 (override), 당신은 당신의 TI를 당신의 시간 배열을 정렬하는 것이 아니라, 배열. 시간, 제목 및 범주라는 세 가지 속성으로 새 클래스 "ScheduleItem"을 만듭니다. 그런 다음 strongtyped List를 작성하고 목록에서 정렬하십시오.

클래스는 파일에서 값을 읽고이 같은 목록을 구축 할 수 있습니다 그 후이

public class ScheduleItem : IComparable<ScheduleItem> 
{ 
    public DateTime Time { get; set; } 
    public string Title { get; set; } 
    public string Category { get; set; } 


    public int CompareTo(ScheduleItem item) 
    { 
     return Title.CompareTo(item.Title); 
    } 
} 

처럼 보일 것입니다.

if (scheduleListBox.Items.Count == 0) 
{ 

    List<ScheduleItem> scheduleItems = new List<ScheduleItem>(); 
    for (int i = 0; i < timeSplit.Length; i++) 
    { 
     string timeList = timeSplit[i]; 
     string titleList = titleSplit[i]; 
     string categoryList = categorySplit[i]; 

     ScheduleItem item = new ScheduleItem 
           { 
            Time = DateTime.Parse(timeList), 
            Title = titleList, 
            Category = categoryList 
           }; 
     scheduleItems.Add(item); 

    } 

    scheduleItems.Sort(); 
} 

이제 ScheduleItems 목록을 반복하여 컨트롤을 작성하십시오. 코드는 다음과 같이 표시되어야합니다.

if (scheduleListBox.Items.Count == 0) 
{ 

    List<ScheduleItem> scheduleItems = new List<ScheduleItem>(); 
    for (int i = 0; i < timeSplit.Length; i++) 
    { 
     string timeList = timeSplit[i]; 
     string titleList = titleSplit[i]; 
     string categoryList = categorySplit[i]; 

     ScheduleItem item = new ScheduleItem 
           { 
            Time = DateTime.Parse(timeList), 
            Title = titleList, 
            Category = categoryList 
           }; 
     scheduleItems.Add(item); 

    } 

    scheduleItems.Sort(); 

    foreach (ScheduleItem item in scheduleItems) 
    { 

     //Define grid column, size 
     Grid schedule = new Grid(); 
     //Column to hold the time of the schedule 
     ColumnDefinition timeColumn = new ColumnDefinition(); 
     GridLength timeGrid = new GridLength(110); 
     timeColumn.Width = timeGrid; 
     schedule.ColumnDefinitions.Add(timeColumn); 


     //Text block that show the time of the schedule 
     TextBlock timeTxtBlock = new TextBlock(); 
     timeTxtBlock.Text = item.Time; 
     //Set the alarm label text block properties - margin, fontsize 
     timeTxtBlock.FontSize = 28; 
     timeTxtBlock.Margin = new Thickness(0, 20, 0, 0); 
     //Set the column that will hold the time of the schedule 
     Grid.SetColumn(timeTxtBlock, 0); 
     schedule.Children.Add(timeTxtBlock); 



     //Column to hold the title of the schedule 
     ColumnDefinition titleColumn = new ColumnDefinition(); 
     GridLength titleGrid = new GridLength(300); 
     titleColumn.Width = titleGrid; 
     schedule.ColumnDefinitions.Add(titleColumn); 

     //Text block that show the title of the schedule 
     TextBlock titleTxtBlock = new TextBlock(); 
     titleTxtBlock.Text = item.Title; 

     if (item.Title.Length > 15) 
     { 
      string strTitle = item.Title.Substring(0, 15) + "...."; 
      titleTxtBlock.Text = strTitle; 
     } 
     else 
     { 
      titleTxtBlock.Text = item.Title; 
     } 
     //Set the alarm label text block properties - margin, fontsize 
     titleTxtBlock.FontSize = 28; 
     titleTxtBlock.Margin = new Thickness(20, 20, 0, 0); 
     //Set the column that will hold the title of the schedule 
     Grid.SetColumn(titleTxtBlock, 1); 
     schedule.Children.Add(titleTxtBlock); 



     //Column 3 to hold the image category of the schedule 
     ColumnDefinition categoryImageColumn = new ColumnDefinition(); 
     GridLength catImgnGrid = new GridLength(70); 
     categoryImageColumn.Width = catImgnGrid; 
     schedule.ColumnDefinitions.Add(categoryImageColumn); 

     //Text block that show the category of the schedule 
     TextBlock categoryTxtBlock = new TextBlock(); 
     categoryTxtBlock.Text = item.Category; 

     //set the category image and its properties - margin, width, height, name, background, font size 
     Image categoryImage = new Image(); 
     categoryImage.Margin = new Thickness(-20, 15, 0, 0); 
     categoryImage.Width = 50; 
     categoryImage.Height = 50; 
     if (categoryTxtBlock.Text == "Priority") 
     { 
      categoryImage.Source = new BitmapImage(new Uri("/AlarmClock;component/Images/exclamination_mark.png", UriKind.Relative)); 
     } 
     else 
      if (categoryTxtBlock.Text == "Favourite") 
      { 
       categoryImage.Source = new BitmapImage(new Uri("/AlarmClock;component/Images/star_full.png", UriKind.Relative)); 
      } 


     Grid.SetColumn(categoryImage, 2); 
     schedule.Children.Add(categoryImage); 
    } 

} 
+0

죄송합니다. 무슨 뜻입니까? 나는 그 모범을 보여줄 수 있는가? –

+0

내 게시물을 편집하기 만하면됩니다. 희망은 도움이 – Mariusz

+0

나는 내 코드를 편집하고 위에 게시했습니다. 내가 편집 한 것은 맞습니까? –