c# – 如何在Windows Phone 8中使用ValueConverter作为StaticResource

前端之家收集整理的这篇文章主要介绍了c# – 如何在Windows Phone 8中使用ValueConverter作为StaticResource前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
以下是我的App.xaml
  1. <Application
  2. x:Class="SpinrWindowsMobile.App"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
  6. xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
  7. >
  8.  
  9. <!--Application Resources-->
  10. <Application.Resources >
  11. <ResourceDictionary>
  12.  
  13. <local:LocalizedStrings xmlns:local="clr-namespace:SpinrWindowsMobile" x:Key="LocalizedStrings"/>
  14. <converter:TextColorConverter xmlns:converter="clr-namespace:SpinrWindowsMobile.Common" x:Key="TextColorConverter"></converter:TextColorConverter>
  15.  
  16. </ResourceDictionary>
  17. </Application.Resources>
  18.  
  19. ....
  20. </Application>

我在NameSpace SpinrWindowsMo​​bile.Common中编写了TextColorConverter.cs
在启动应用程序时它给我异常无法创建类型SpinrWindowsMo​​bile.Common.TextColorConverter的实例.我不知道我在哪里错过.
下面是我的TextColorConverter.cs类

  1. class TextColorConverter : IValueConverter
  2. {
  3. public object Convert(object value,Type targetType,object parameter,System.Globalization.CultureInfo culture)
  4. {
  5. // some code
  6. }
  7.  
  8. public object ConvertBack(object value,System.Globalization.CultureInfo culture)
  9. {
  10. // some code
  11. }
  12. }

我正在使用Microsoft Visual Studio 2012 Windows Phone作为我的开发工具.
还有一件事我想分享我没有在System.Windows.Data命名空间中获取ValueConverstionAttribute类.
任何人都可以指导我错在哪里.

解决方法

您将使您的课程成为一个公共课程(默认情况下,它将是内部的).否则不能实例化.

public class TextColorConverter:IValueConverter

猜你在找的C#相关文章