Program,Life,Society.....

导航

统计

公告

Silverlight 操作技巧

1.Emumeration转化为List

   1:  using System;
   2:  using System.Linq;
   3:  using System.Collections.Generic;
   4:  using System.Reflection;
   5:   
   6:      /// <summary>  
   7:      /// Helper class to allow users to get the values of a enumeration  
   8:      /// </summary>  
   9:      public static class EnumHelper  
  10:      {  
  11:          /// <summary>  
  12:          /// Get values of an enumeration of type T  
  13:          /// </summary>  
  14:          /// <typeparam name="T">Enumeration type</typeparam>  
  15:          /// <returns>List of types in a enumeration</returns>  
  16:          public static T[] GetValues<T>()  
  17:          {  
  18:              Type enumType = typeof(T);  
  19:    
  20:              if (!enumType.IsEnum)  
  21:              {  
  22:                  throw new ArgumentException("Type '" + enumType.Name + "' is not an enum");  
  23:              }  
  24:    
  25:              List<T> values = new List<T>();  
  26:    
  27:              var fields = from field in enumType.GetFields()  
  28:                           where field.IsLiteral  
  29:                           select field;  
  30:    
  31:              foreach (FieldInfo field in fields)  
  32:              {  
  33:                  object value = field.GetValue(enumType);  
  34:                  values.Add((T)value);  
  35:              }  
  36:    
  37:              return values.ToArray();  
  38:          }  
  39:    
  40:          /// <summary>  
  41:          /// Get all the values of an enumeration of Type enumType  
  42:          /// </summary>  
  43:          /// <param name="enumType">Enumeration type</param>  
  44:          /// <returns>Array of objects</returns>  
  45:          public static object[] GetValues(Type enumType)  
  46:          {  
  47:              if (!enumType.IsEnum)  
  48:              {  
  49:                  throw new ArgumentException("Type '" + enumType.Name + "' is not an enum");  
  50:              }  
  51:    
  52:              List<object> values = new List<object>();  
  53:    
  54:              var fields = from field in enumType.GetFields()  
  55:                           where field.IsLiteral  
  56:                           select field;  
  57:    
  58:              foreach (FieldInfo field in fields)  
  59:              {  
  60:                  object value = field.GetValue(enumType); //field.Name 可以得到把这个函数改为GetNames
  61:                  values.Add(value);  
  62:              }  
  63:    
  64:              return values.ToArray();  
  65:          }  
  66:      }  

2.How to use String Resources

To start, open up your App.xaml file and add a reference to the MSCORLIB library as seen highlighted here.

<Application xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
xmlns:clr="clr-namespace:System;assembly=mscorlib"
x:Class="TestApp.App">

One place you can add all your strings is under the Application.Resources section in your App.xaml. To illustrate this, I have added one string resource that can be referenced through the key “MyName”.

<Application.Resources>
<clr:String x:Key="MyName">Mike Snow</clr:String>
</Application.Resources>

Finally, open up Page.xaml and add a TextBlock. Set the text of this TextBlock to point to the string resource we just added. This can be done by using the syntax Text=”{StaticResource MyName}” as seen here:

<TextBlock Text="{StaticResource MyName}"></TextBlock>

 

3 url string resolution to resources in the hosting web application.

namespace SilverTools.Utilities.Misc
{
    public static class UriHelper
    {
        public static Uri ConvertToUri(this string partialPath,UriKind uriKind)
        {
            return new Uri(partialPath, uriKind);
           
        }
        public static Uri ConvertToUri(this string partialPath)
        {
            return new Uri(Application.Current.Host.Source, partialPath);
        }
        public static string ConvertToUrl(this string partialPath)
        {
            return new Uri(Application.Current.Host.Source, partialPath).AbsoluteUri;
        }
    }

 

 

posted on 2010-01-14 11:28 rippleyong 阅读(...) 评论(...) 编辑 收藏