2010-08-05 3 views
10

से विंडोज फोन 7 एप्लिकेशन शीर्षक प्राप्त करें मैं अपने व्यूमोडेल कोड से WMAppManifest.xml फ़ाइल में संग्रहीत शीर्षक मान को एक्सेस करना चाहता हूं। यह वही अनुप्रयोग शीर्षक है जो प्रोजेक्ट गुणों के माध्यम से सेट किया गया है।कोड

क्या App.Current जैसे कुछ का उपयोग कर कोड से इसे एक्सेस करने का कोई तरीका है?

उत्तर

12

Microsoft Silverlight Analytics Framework में WP7DataCollector.GetAppAttribute() के लिए the source code पर देखें। GetAppAttribute ("शीर्षक") यह करेगा।

/// <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

यह अंतिम उत्तर मुझे अत्यधिक जटिल लगता है; आप बस कुछ ऐसा कर सकता था:

   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

मैं माइकल एस Scherotter का इस्तेमाल किया है उनकी उत्कृष्ट कोड नमूना एक पूरी तरह से काम कर कोड नमूना करने के लिए इसे बाहर काम करने के:

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

केवल पहले दो जवाब हैं मूल प्रश्न के दायरे में सही है। और दूसरा निश्चित रूप से जटिल नहीं है। प्रत्येक संभावित विशेषता के लिए एक वर्ग के साथ सहायक विधि को लपेटना अच्छा ऑब्जेक्ट उन्मुख विकास है और वास्तव में माइक्रोसॉफ्ट ने सभी ढांचे पर क्या किया है, उदाहरण के लिए विजुअल स्टूडियो द्वारा उत्पन्न सेटिंग्स डिजाइनर फाइलें।

यदि आप सिर्फ एक विशिष्ट संपत्ति चाहते हैं तो दूसरा उपयोग करने की सलाह देंगे, दूसरा यदि आप और चाहते हैं। वास्तव में एसडीके का हिस्सा होना चाहिए। हम यहां WMAppManifest.xml को पढ़ने की कोशिश कर रहे हैं, असेंबलीइन्फो नहीं, इसलिए मानक असेंबली प्रतिबिंब मेटाडाटा कोई अच्छा नहीं है।

वैसे, यदि आप वास्तव में असेंबली विशेषताओं (WPAppManifest.xml नहीं) से उत्पाद का नाम प्राप्त करना चाहते हैं तो अंतिम नमूना गलत विशेषता पढ़ रहा था! Assembly प्रोडक्ट एट्रिब्यूट का उपयोग विधानसभा टाइटल एट्रिब्यूट नहीं करें। असेंबली शीर्षक वास्तव में फ़ाइल शीर्षक है, डिफ़ॉल्ट रूप से असेंबली फ़ाइल नाम (जैसे MyCompany.MyProduct.WinPhone7App) के समान होता है, जबकि उत्पाद आमतौर पर स्टोर में आपके ऐप के उचित स्वरूपित "शीर्षक" जैसा कुछ होगा (उदाहरण के लिए "मेरा उत्पाद ")। वीएस गुण पृष्ठ का उपयोग करने के बाद भी यह अद्यतित नहीं हो सकता है, इसलिए आपको यह जांचना चाहिए।

मैं आधिकारिक उत्पाद नाम दिखाने और पृष्ठ के बारे में संस्करण बनाने के लिए अन्य सभी एप्लिकेशन प्रकारों के लिए असेंबलीइन्फो प्रतिबिंब का उपयोग करता हूं, यह निश्चित रूप से इसके लिए सही है। लेकिन इन विशेष फोन एप प्रकारों के लिए स्टोर मेनिफेस्ट में अधिक महत्व है और अन्य विशेषताओं की आपको आवश्यकता हो सकती है।

0

उन सभी उत्तरों के साथ समस्या यह है कि उन्हें हर बार फ़ाइल को पढ़ने के लिए फ़ाइल को पढ़ना होता है। प्रदर्शन के लिए यह बुरा है क्योंकि बैटरी मुद्दों पर विचार करने के लिए यदि आप अक्सर इसका इस्तेमाल करते हैं। कोएन एक उचित समाधान के करीब था, लेकिन जब भी आप मूल्य तक पहुंचना चाहते थे तब भी उनका डिज़ाइन फ़ाइल पर वापस चला गया।

नीचे दिया गया समाधान फ़ाइल का एक-और-पूरा पढ़ा गया है। चूंकि इसे बदलने की संभावना नहीं है, इसलिए इसमें वापस जाने का कोई कारण नहीं है। गुणों को पढ़ा जाता है क्योंकि स्थिर वर्ग प्रारंभिक रूप से कम से कम झगड़ा होता है।

I created this Gist to demonstrate

एचटीएच!