简体   繁体   English

XAML没有看到我的IValueConverter类

[英]XAML doesn't see my IValueConverter class

I'm creating a simple WP8, but I'm having troubles hiding (changing the Visibility property) on a control. 我正在创建一个简单的WP8,但是在控件上隐藏(更改Visibility属性)时遇到了麻烦。

In XAML I've added xmlns:local="clr-namespace:MyProjectName" (I've also tried with using ). 在XAML中,我添加了xmlns:local="clr-namespace:MyProjectName" (我也尝试过using )。 The XAML is then structured as follows: 然后,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>

The problem is at the <local:...> line: The name "VisibilityFormatter" does not exist in the namespace "clr-namespace:MyProjectName". 问题出在<local:...>行: The name "VisibilityFormatter" does not exist in the namespace "clr-namespace:MyProjectName".

The class is defined as follows: 该类的定义如下:

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();
            }
        }
    }
}

The class ObjInfo is a simple public class with two properties: ObjInfo类是具有两个属性的简单公共类:

namespace MyProjectName.Models
{
    public class ObjInfo
    {
        public bool ObjVisibility { get; set; }
        public string Obj { get; set; }
    }
}

It's similar to this question, but no migrating is involved. 问题类似,但是不涉及迁移。 I'm developing on WP8 from the get-go. 我从一开始就在WP8上进行开发。

What am I trying to achieve? 我想达到什么目的? Well. 好。 I'm storing whether the control should be visible or not in that bool property. 我在该bool属性中存储控件是否应该可见。 Since the XAML control's property only grokks the Visibility enum , but not bool , I need to convert it to that enum . 由于XAML控件的属性仅会影响Visibility enum ,而不是bool ,因此我需要将其转换为该enum

The VisibilityFormatter is an inner class of the Formatter class. VisibilityFormatterFormatter类的内部类。 You don't really need the Formatter class, just make the VisibilityFormatter a top class, and the XAML parser will find it. 您实际上并不需要Formatter类,只需将VisibilityFormatter顶级类,然后XAML解析器将找到它。

Also, the general naming convention for converters is XXXConverter and not XXXFormatter , but that's no rule. 另外,转换器的通用命名约定是XXXConverter而不是XXXFormatter ,但这不是规则。

You don't have MyProjectName.VisibilityFormatter in your project, you have 您的项目中没有MyProjectName.VisibilityFormatter

MyProjectName.Formatter.VisibilityFormatter

You should remove Formatter class and leave only: 您应该删除Formatter类并仅保留:

namespace MyProjectName
{
        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();
            }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM