Lite development: curriculum and course lattice, lite curriculum

Source: Internet
Author: User

Lite development: curriculum and course lattice, lite curriculum

The last time we acquired course information by calling the Three-wing API, we now need to design controls to present data. Let's make a simple curriculum this time.

The effect is as follows:

There are many implementation methods. Let's use custom controls:

(1) create a custom control and compile the interface

Use the custom control. Here I separate the grid (Block) and Course table (CourseTable) to facilitate future creation. Therefore, we need to create two controls: BlockControl and CourseTableControl: right-click Project-add new item-user control and rename it (if multiple controls exist, we recommend that you put them in the same folder for convenient management)

First, BlockControl. xaml:

 
          
               
                    
                     
       
        
       
      
     
                
                
                 
       
      
     
    
   
 

This is relatively simple, just three lines of TextBlock in a Button, indicating the course name, class location, and teacher respectively.

Then CourseTable. xaml:

        
              
                   
                    
                     
       
        
         
          
           
            
           
          
         
        
       
      
     
    
               
                   
                    
                     
       
        
         
          
           
           
            
             
              
               
                
                 
                 
                  
                   
                    
                    
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
               
               
                
                 
       
        
         
          
           
            
           
             Early read
            
           
             1
            
           
             2
            
           
             3
            
           
             4
            
           
             Noon
            
           
             5
            
           
             6
            
           
             7
            
           
             8
            
           
             Evening
            
           
             9
            
           
             10
            
           
             11
            
           
             Night
            
           
             I
            
            
            
              II
             
             
             
               3.
              
              
              
                Thu
               
               
               
                 V.
                
                
                
                  Sat.
                 
                 
                 
                   Day
                  
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 

Here I used a simple and crude method to create a 12-row, 8-column table with a Grid. The StaticResource here is saved and can be defined according to your own ideas.

(2) obtain data in the background and place the grid

The above Code uses the x: Bind method to transmit data. Therefore, we can create a CourseMode. cs class to Bind data as a data template:

Namespace arch. models {internal class CourseModel {public int Id; public string Course; public string Location; public string Teacher; public int Day; public string Week; public int Start; public int End ;}}

Then add a CourseModel field to BlockControl. xaml. cs. I created a CourseInf:

Namespace arch. controls {public sealed partial class BlockControl: UserControl {public static BlockControl Current; internal CourseModel CourseInf = new CourseModel (); internal Brush Color; public BlockControl () {this. initializeComponent (); Current = this;} public BlockControl (CourseModel model, Brush color): this () // Note: If BlockControl () is not overloaded, the control is not initialized, the widget is not displayed. {CourseInf = model; Color = color ;}}}

Finally, assign a value to the grid and place the grid on CourseTable:

Namespace arch. controls {public sealed partial class CoursesTable: UserControl {static int ArraySize = 50; BlockControl [] BlockArray = new BlockControl [ArraySize]; // The course lattice array public static CoursesTable Current; int ThisWeekNum, weekNum; internal CourseTableDateModel TableDateModel = new CourseTableDateModel (); public CoursesTable () {this. initializeComponent (); Current = this; GetCourseBlocks (); SetBlockColor (); SetBlockVisibility (1 ); // here, the course of the first week is used as an example.} # Set private void GetCourseBlocks () in the region course lattice () // obtain the grid and pass the value {try {Brush [] Color = new Brush [] {new SolidColorBrush (Colors. chocolate), new SolidColorBrush (Colors. bluevilet), new SolidColorBrush (Colors. cornflowerBlue), new SolidColorBrush (Colors. cyan), new SolidColorBrush (Colors. deepPink), new SolidColorBrush (Colors. gray), new SolidColorBrush (Colors. mediumSeaGreen), new SolidColorBrush (Colors. mediumSlateBlue), new SolidColorBrush (Colors. magenta), new SolidColorBrush (Colors. oliveDrab)}; // 10 grid colors/* database-based course reading */AppDatabase database = new AppDatabase (); database. getCreateConn (); ObservableCollection
 
  
Courses = new ObservableCollection
  
   
(); Courses = database. readData_Courses (courses); // courses is the database data Random ran = new Random (); // Random color int I = 0; foreach (var course in courses) {BlockArray [I] = new BlockControl (course, Color [ran. next (0, 9)]); // input the grid information ClassGrid. children. add (BlockArray [I]); // place the grid on CourseTable/* place the grid to the correct position */if (course. start <= 4) {BlockArray [I]. setValue (Grid. rowProperty, course. start + 1); BlockArray [I]. setValue (Gri D. rowSpanProperty, course. end-course. start + 1);} else if (course. start> = 5 & course. start <= 8) {BlockArray [I]. setValue (Grid. rowProperty, course. start + 2); BlockArray [I]. setValue (Grid. rowSpanProperty, course. end-course. start + 1);} else if (course. start> = 9) {BlockArray [I]. setValue (Grid. rowProperty, course. start + 3); BlockArray [I]. setValue (Grid. rowSpanProperty, course. end-course. st Art + 1);} BlockArray [I]. setValue (Grid. columnProperty, course. day);/* place the grid to the correct position */I ++;} catch (Exception e) {// Debug operation, omitted} private void SetBlockVisibility (int weeknum) // set the course lattice visibility {for (int I = 0; BlockArray [I] according to the number of weeks! = Null; I ++) {BlockArray [I]. visibility = Visibility. collapsed; int [] weeks = WeekstringToint (BlockArray [I]. courseInf. week); // converts the string Week to "3, 4, 5 ,......, 12 "array to compare the number of weeks foreach (int weekin weeks) {if (week = weeknum) {BlockArray [I]. visibility = Visibility. visible; // the number of weeks is the same, set Visible }}} private void SetBlockColor () // the same course settings have the same color {for (int I = 0; BlockArray [I]! = Null; I ++) {for (int j = I + 1; BlockArray [j]! = Null; j ++) {if (string. equals (BlockArray [I]. courseInf. course, BlockArray [j]. courseInf. course) {BlockArray [j]. color = BlockArray [I]. color ;}}# endregion public int [] WeekstringToint (string WeekOri) // Week style: "3-12, 17-20", return the int array as "3, 4 ,......, 12, 17, 18, 19, 20 "{int [] weeks = new int [30]; string Week = WeekOri +" ("; int a = 0, B = 0, c = 0, d = 0; // a, B is used to count, and c and d are two numbers left and right of '-' while (a <Week. length) // traverse the entire string {if (int) Week [a]> = 48 & (int) Week [a] <= 57) // use the ASCII value of the value to determine whether it is a number {if (int) Week [a + 1]> = 48 & (int) week [a + 1] <= 57) // judge whether it is a double digit {c = (int) (Week [a])-48) * 10 + (int) week [a + 1]-48; // c is the converted number a + = 2; // directly judge the next character of the two digits continue ;} // one-digit else c = (int) Week [a]-48;} if (int) Week [a] = 44) // determine whether it is ', '{weeks [B] = c; while (weeks [B] <d) {B ++; weeks [B] = weeks [B-1] + 1; // fill in the number between c and d, and save it to weeks} c = 0; d = 0; a ++; B ++; // reset c, d value continue ;} if (int) Week [a] = 45) // judge whether it is '-' {if (int) week [a + 2]> = 48 & (int) Week [a + 2] <= 57) // judge whether it is a double digit {d = (int) (Week [a + 1])-48) * 10 + (int) Week [a + 2]-48; // d is the converted number a + = 3; // directly determine the next character of the two digits continue;} else d = (int) Week [++ a]-48; a ++; continue;} if (int) week [a] = 40) // determine whether it is '(' {weeks [B] = c; while (weeks [B] <d) {B ++; weeks [B] = weeks [B-1] + 1; // fill in the number between c and d and save it to weeks} c = 0; d = 0; a ++; B ++; // reset c, D value break; // judge completed, end} a ++;} return weeks ;}}}
  
 

Here, I create a control array to get multiple grids, and then operate on the grids conveniently, just operate on the corresponding array in the background.

In addition, I directly read the database. In fact, I can use the information saved in the previous article to CourseModel and assign a value to courses directly.

In this way, a simple course schedule is complete. Of course, you can add the week slider, click the grid to display details, and add an animation, please follow your own ideas.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.