为什么在尝试将转换器添加到ResourceDictionary时出现此错误?

时间:2014-02-12 12:53:30

标签: xaml windows-phone-7 windows-phone-8 windows-phone converter

我尝试将转换器添加到ResourceDictionary,因为我需要使用它,但是当我添加声明时,当我运行应用程序时会发生错误。

这是添加转换器声明的方法:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
xmlns:Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP71"
xmlns:converters="clr-namespace:Sample.Utility.Converters">

<converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>

这是我收到的错误

{System.Windows.Markup.XamlParseException: The type 'BoolToVisibilityConverter' was not found. [Line: 17 Position: 44]
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at Sample.App.InitializeComponent()
   at Sample.App..ctor()}

我事件尝试添加转换器,但结果相同。

请问我有什么问题吗?


这是我的转换器的代码

public class BoolToVisibilityConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool visible = (bool)value;

        return (visible ? Visibility.Visible : Visibility.Collapsed);

    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Visibility vis = (Visibility)value;
        switch (vis)
        {
            case Visibility.Collapsed:
                return false;
            case Visibility.Visible:
                return true;
        }
        return null;
    }

    #endregion
}

1 个答案:

答案 0 :(得分:0)

要在您的应用中全局使用资源字典,您需要在App.xaml中将其合并为:

<Application x:Class="Sample.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Application.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/uri/to/resource_dictionary.xaml"/>
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </Application.Resources>
</Application>