2010-08-05 3 views

답변

12

Microsoft Silverlight Analytics Framework의 WP7DataCollector.GetAppAttribute()는 the source code입니다. GetAppAttribute ("Title")이 실행합니다.

/// <summary> 
    /// Gets an attribute from the Windows Phone App Manifest App element 
    /// </summary> 
    /// <param name="attributeName">the attribute name</param> 
    /// <returns>the attribute value</returns> 
    private static string GetAppAttribute(string attributeName) 
    { 
     string appManifestName = "WMAppManifest.xml"; 
     string appNodeName = "App"; 

     var settings = new XmlReaderSettings(); 
     settings.XmlResolver = new XmlXapResolver(); 

     using (XmlReader rdr = XmlReader.Create(appManifestName, settings)) 
     { 
      rdr.ReadToDescendant(appNodeName); 
      if (!rdr.IsStartElement()) 
      { 
       throw new System.FormatException(appManifestName + " is missing " + appNodeName); 
      } 

      return rdr.GetAttribute(attributeName); 
     } 
    } 
+1

지역화 된 리소스를 사용하는 경우이 솔루션은 작동하지 않습니다 : @ AppResLib.dll, -101. –

1

이 마지막 대답은 나에게 너무 복잡해 보인다. 당신은 단순히 뭔가를 할 수 있었다 :

내가 완벽하게 작동하는 코드 샘플에 해결하는 마이클 S. Scherotter 그의 뛰어난 코드 샘플을 사용했다
   string name = ""; 
       var executingAssembly = System.Reflection.Assembly.GetExecutingAssembly(); 
       var customAttributes = executingAssembly.GetCustomAttributes(typeof(System.Reflection.AssemblyTitleAttribute), false); 
       if (customAttributes != null) 
       { 
        var assemblyName = customAttributes[0] as System.Reflection.AssemblyTitleAttribute; 
        name = assemblyName.Title; 
       } 
1

: 만 처음 두 답변은

using System.Xml; 

namespace KoenZomers.WinPhone.Samples 
{ 
    /// <summary> 
    /// Allows application information to be retrieved 
    /// </summary> 
    public static class ApplicationInfo 
    { 
     #region Constants 

     /// <summary> 
     /// Filename of the application manifest contained within the XAP file 
     /// </summary> 
     private const string AppManifestName = "WMAppManifest.xml"; 

     /// <summary> 
     /// Name of the XML element containing the application information 
     /// </summary> 
     private const string AppNodeName = "App"; 

     #endregion 

     #region Properties 

     /// <summary> 
     /// Gets the application title 
     /// </summary> 
     public static string Title 
     { 
      get { return GetAppAttribute("Title"); } 
     } 

     /// <summary> 
     /// Gets the application description 
     /// </summary> 
     public static string Description 
     { 
      get { return GetAppAttribute("Description"); } 
     } 

     /// <summary> 
     /// Gets the application version 
     /// </summary> 
     public static string Version 
     { 
      get { return GetAppAttribute("Version"); } 
     } 

     /// <summary> 
     /// Gets the application publisher 
     /// </summary> 
     public static string Publisher 
     { 
      get { return GetAppAttribute("Publisher"); } 
     } 

     /// <summary> 
     /// Gets the application author 
     /// </summary> 
     public static string Author 
     { 
      get { return GetAppAttribute("Author"); } 
     } 

     #endregion 

     #region Methods   

     /// <summary> 
     /// Gets an attribute from the Windows Phone App Manifest App element 
     /// </summary> 
     /// <param name="attributeName">the attribute name</param> 
     /// <returns>the attribute value</returns> 
     private static string GetAppAttribute(string attributeName) 
     { 
      var settings = new XmlReaderSettings {XmlResolver = new XmlXapResolver()}; 

      using (var rdr = XmlReader.Create(AppManifestName, settings)) 
      { 
       rdr.ReadToDescendant(AppNodeName); 

       // Return the value of the requested XML attribute if found or NULL if the XML element with the application information was not found in the application manifest 
       return !rdr.IsStartElement() ? null : rdr.GetAttribute(attributeName); 
      } 
     } 

     #endregion 
    } 
} 
1

원래 질문의 범위에서 정답. 그리고 두 번째는 분명히 복잡하지 않습니다. 가능한 각 속성에 대해 클래스로 도우미 메서드를 래핑하면 좋은 객체 지향 개발과 Microsoft가 프레임 워크 전체에서 수행하는 작업을 정확하게 수행 할 수 있습니다. Visual Studio에서 생성 한 디자이너 파일을 설정합니다.

하나의 특정 속성 만 원할 경우 첫 번째 속성을 사용하고 두 번째 속성 속성을 원할 경우 두 번째 속성을 사용하는 것이 좋습니다. 정말 SDK의 일부가되어야합니다. 우리는 AssemblyInfo가 아닌 WMAppManifest.xml을 읽으려고합니다. 따라서 표준 어셈블리 리플렉션 메타 데이터는 좋지 않습니다.

그런데 실제로 어셈블리 속성 (WPAppManifest.xml 아님)에서 제품 이름을 가져 오려면 마지막 샘플이 잘못된 특성을 읽는 중입니다. AssemblyTitleAttribute가 아닌 AssemblyProductAttribute를 사용하십시오. 어셈블리 제목은 실제로 어셈블리 파일 이름 (예 : MyCompany.MyProduct.WinPhone7App)과 동일한 파일 제목입니다. 제품은 일반적으로 저장소에있는 응용 프로그램의 올바른 형식의 "제목"과 유사합니다 (예 : "My 생성물"). VS 속성 페이지를 사용한 후에는 최신 정보가 아닐 수도 있으므로 확인해야합니다.

다른 모든 응용 프로그램 유형에 대해 AssemblyInfo 리플렉션을 사용하여 공식 페이지에 공식 제품 이름과 빌드 버전을 표시하면 확실히 맞습니다. 그러나 이러한 특수한 전화 앱 유형의 경우 상점 매니페스트가 더 중요하고 다른 특성이 필요할 수 있습니다.

0

모든 답변의 문제점은 파일에 액세스 할 때마다 파일을 읽어야한다는 것입니다. 자주 사용하면 배터리 문제가 있으므로 성능에는 좋지 않습니다. Koen은 적절한 솔루션에 가까웠지만, 당신이 그 값에 접근하기를 원할 때마다 그의 디자인은 여전히 ​​파일로 되돌아갔습니다.

아래 해결책은 파일을 한 번 읽고 읽는 것입니다. 변경 될 가능성이 없기 때문에 계속해서 돌아갈 이유가 없습니다. 속성은 정적 클래스가 초기화 될 때 읽 힙니다.

I created this Gist to demonstrate.

HTH!

관련 문제