2013-05-27 46 views
3

我有windows phone應用程序;當我運行該應用程序我得到這個例外,它沒有再跑錯誤:在System.Windows.ni.dll中發生類型'System.Windows.Markup.XamlParseException'的第一個機會異常

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Windows.ni.dll 

在App.xaml文件發生在intializecomponent誤差();方法

public App() 
    { 
     // Global handler for uncaught exceptions. 
     UnhandledException += Application_UnhandledException; 

     // Standard XAML initialization 
     InitializeComponent(); 

     // Phone-specific initialization 
     InitializePhoneApplication(); 

因爲當我在應用程序資源

<converter:RssTextTrimmer xmlns:converter="clr-namespace:HomePage" x:Key="RssTextTrimmer" /> 

添加當我刪除它的應用程序工作做好發生的錯誤。

下面是完整的代碼:

<Application 
x:Class="HomePage.App" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"> 

<!--Application Resources--> 
<Application.Resources> 
    <local:LocalizedStrings xmlns:local="clr-namespace:HomePage" x:Key="LocalizedStrings"/> 
    <converter:RssTextTrimmer xmlns:converter="clr-namespace:HomePage" x:Key="RssTextTrimmer" /> 

</Application.Resources> 

<Application.ApplicationLifetimeObjects> 
    <!--Required object that handles lifetime events for the application--> 
    <shell:PhoneApplicationService 
     Launching="Application_Launching" Closing="Application_Closing" 
     Activated="Application_Activated" Deactivated="Application_Deactivated"/> 
</Application.ApplicationLifetimeObjects> 

的轉換代碼

命名空間首頁 { 類RssTextTrimmer:的IValueConverter {

// Clean up text fields from each SyndicationItem. 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (value == null) return null; 

     int maxLength = 200; 
     int strLength = 0; 
     string fixedString = ""; 

     // Remove HTML tags and newline characters from the text, and decodes HTML encoded characters. 
     // This is a basic method. Additional code would be needed to more thoroughly 
     // remove certain elements, such as embedded Javascript. 

     // Remove HTML tags. 
     fixedString = Regex.Replace(value.ToString(), "<[^>]+>", string.Empty); 

     // Remove newline characters 
     fixedString = fixedString.Replace("\r", "").Replace("\n", ""); 

     // Remove encoded HTML characters 
     fixedString = HttpUtility.HtmlDecode(fixedString); 

     strLength = fixedString.ToString().Length; 



     // Some feed management tools include an image tag in the Description field of an RSS feed, 
     // so even if the Description field (and thus, the Summary property) is not populated, it could still contain HTML. 
     // Due to this, after we strip tags from the string, we should return null if there is nothing left in the resulting string. 
     if (strLength == 0) 
     { 
      return null; 
     } 

     // Truncate the text if it is too long. 
     else if (strLength >= maxLength) 
     { 
      fixedString = fixedString.Substring(0, maxLength); 

      // Unless we take the next step, the string truncation could occur in the middle of a word. 
      // Using LastIndexOf we can find the last space character in the string and truncate there. 
      fixedString = fixedString.Substring(0, fixedString.LastIndexOf(" ")); 
     } 

     fixedString += "..."; 

     return fixedString; 
    } 

    // This code sample does not use TwoWay binding and thus, we do not need to flesh out ConvertBack. 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

}

+0

顯示的XAML代碼,因爲它是一個XamlParseException,錯誤應該在那裏。 – anderZubi

+0

我更新它... – Sally

+0

顯示您的轉換器代碼 –

回答

7

在你RssTextTrimmer.cs文件,確保類是公共 「公共類RssTextTrimmer:的IValueConverter」 不 「類RssTextTrimmer:的IValueConverter」 這就是我的雞姦了

+0

完美答案,無法創建實例,因爲它的默認保護,所以使它public'll工作,感謝戰利品 –

相關問題