在Windows Phone 8应用程序中将转换器绑定到XAML

时间:2013-10-22 06:28:59

标签: c# wpf xaml windows-phone-8 windows-phone

我的.xaml网页代码

<phone:PhoneApplicationPage
    x:Class="MVVM1Test.MainPage"
    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"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:converter="clr-namespace:MVVM1Test.Resources.Converters"
    mc:Ignorable="d"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <phone:PhoneApplicationPage.Resources>
        <converter:NotConverter x:Name="not"></converter:NotConverter>
    </phone:PhoneApplicationPage.Resources>

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Gray">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
    </Grid>

</phone:PhoneApplicationPage>

notconverter.cs

namespace MVVM1Test.Resources.Converters
{
    public class NotConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return !(bool)value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return !(bool)value;
        }
    }
}

显示错误

  

“命名空间clr-namespace中不存在名称NotConverter:MVVM1Test.Resources.Converters”

但我已将转换器类添加到解决方案中。

3 个答案:

答案 0 :(得分:2)

将项目放入资源字典时,必须为其提供密钥。 x:Name属性是多余的(除非您需要从后面的代码访问对象)。

<converter:NotConverter x:Key="not" />

此外,您遇到的错误可能是设计时问题。尝试在我建议的更改后编译并运行。

答案 1 :(得分:0)

这可能对您有所帮助

的xmlns:MyAppConverters = “CLR-名称空间:MyApp.Converters”

<phone:PhoneApplicationPage.Resources>
    <MyAppConverters:CustomConverter x:Key="customConverter"/>    
</phone:PhoneApplicationPage.Resources>

我已举例说明你可以改变你的班级名称

答案 2 :(得分:0)

我有类似的名字空间问题。对我来说,重启visual studio似乎解决了这个问题。当然,您必须确保在IValueConverter类中正确定义了命名空间 例如,命名空间MVVM1Test.Resources.Converters

保存并重新启动visual studio。