XAML没有看到我的IValueConverter类

How*_*wie 0 c# xaml windows-phone-8

我正在创建一个简单的WP8,但我在控件上隐藏(更改Visibility属性)时遇到麻烦.

在XAML中,我添加了xmlns:local="clr-namespace:MyProjectName"(我也尝试过using).然后,XAML的结构如下:

<phone:PhoneApplicationPage
x:Class="MyProjectName.Pages.Main"
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:local="clr-namespace:MyProjectName"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True" Margin="0,4,0,-4" Background="#FFBD3F3F">

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneBackgroundBrush}" >
    <Grid x:Name="ContentPanel" Grid.Row="1" >
        <Grid.Resources>
            <local:VisibilityFormatter x:Key="FormatConverter" />
        </Grid.Resources>

        <phone:LongListSelector Grid.Row="4">
            <phone:LongListSelector.ItemTemplate>
                <DataTemplate>
                     <TextBlock Text="{Binding Obj}" 
                                     Visibility="{Binding ObjVisibility, 
                                     Mode=OneWay, 
                                     Converter={StaticResource FormatConverter}}" />
                </DataTemplate>
            </phone:LongListSelector.ItemTemplate>
        </phone:LongListSelector>
    </Grid>
</Grid>
Run Code Online (Sandbox Code Playgroud)

问题在于<local:...>:The name "VisibilityFormatter" does not exist in the namespace "clr-namespace:MyProjectName".

该类定义如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace MyProjectName
{
    public class Formatter
    {
        public class VisibilityFormatter : IValueConverter
        {
            // Retrieve the format string and use it to format the value.
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                var visibility = parameter as bool?;
                return visibility.HasValue && visibility.Value ? Visibility.Visible : Visibility.Collapsed;
            }

            // No need to implement converting back on a one-way binding 
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

该类ObjInfo是一个简单的公共类,具有两个属性:

namespace MyProjectName.Models
{
    public class ObjInfo
    {
        public bool ObjVisibility { get; set; }
        public string Obj { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

它与此问题类似,但不涉及迁移.我从一开始就开发WP8.

我想要实现的目标是什么?好.我正在存储控件是否应该在该bool属性中可见.由于XAML控件的属性只是grokks Visibility enum,但不是bool,我需要将其转换为enum.

Pat*_*ick 6

这VisibilityFormatter是班级的内部Formatter阶级.你真的不需要这个Formatter类,只需要创建VisibilityFormatter一个顶级类,XAML解析器就能找到它.

另外,转换器一般的命名规则是XXXConverter,不XXXFormatter,但这是没有规则.