2016-12-10 2 views
-1

Unten ist der Code , die C# Programmierer seltsam aussieht.Wie schreibe ich Equivalent Java Enum Code in C# in Android Xamarin App

public static enum Font { 
     GROBOLD(FontLoader.GROBOLD, "fonts/grobold.ttf"); 

     private int val; 
     private String path; 

     private Font(int val, String path) { 
      this.val = val; 
      this.path = path; 
     } 

     public static String getByVal(int val) { 
      for (Font font : values()) { 
       if (font.val == val) { 
        return font.path; 
       } 
      } 
      return null; 
     } 
    } 

ist die Datei https://github.com/abhiongithub/memory-game/blob/master/app/src/main/java/com/snatik/matches/utils/FontLoader.java

Ich bin nicht immer, wie entsprechenden Code in C# zu schreiben.

Antwort

0

schrieb ich so etwas in meiner App für jetzt als C# -Code

public class FontLoader 
    { 
     private static SparseArray<Typeface> fonts = new SparseArray<Typeface>(); 
     private static bool fontsLoaded = false; 

     public enum Font 
     { 
      GROBOLD 
     } 

     public static void LoadFonts() 
     { 
      fonts.Put(Convert.ToInt32(Font.GROBOLD), Typeface.CreateFromAsset(Application.Context.Assets, "fonts/grobold.ttf")); 
      fontsLoaded = true; 
     } 

     public static Typeface GetTypeface(Font font) 
     { 
      if (!fontsLoaded) 
      { 
       LoadFonts(); 
      } 
      return fonts.Get(Convert.ToInt32(font)); 
     } 

     public static void SetTypeface(TextView[] textViews, Font font) 
     { 
      SetTypeFaceToTextViews(textViews, font, TypefaceStyle.Normal); 
     } 

     public static void setBoldTypeface(TextView[] textViews, Font font) 
     { 
      SetTypeFaceToTextViews(textViews, font, TypefaceStyle.Bold); 
     } 

     private static void SetTypeFaceToTextViews(TextView[] textViews, Font font,TypefaceStyle fontStyle) 
     { 
      if (!fontsLoaded) 
      { 
       LoadFonts(); 
      } 
      Typeface currentFont = fonts.Get(Convert.ToInt32(font)); 

      for (int i = 0; i < textViews.Length; i++) 
      { 
       if (textViews[i] != null) 
        textViews[i].SetTypeface(currentFont, fontStyle); 
      } 
     } 
    } 
Verwandte Themen