0

我正在创建一个简单的 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>

问题在于<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();
            }
        }
    }
}

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

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

它类似于this question,但不涉及迁移。我从一开始就在 WP8 上进行开发。

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

4

2 回答 2

6

是类的VisibilityFormatter内部Formatter类。您实际上并不需要Formatter该类,只需创建VisibilityFormatter一个顶级类,XAML 解析器就会找到它。

此外,转换器的一般命名约定是XXXConverterand not XXXFormatter,但这不是规则。

于 2013-08-21T11:23:04.807 回答
1

MyProjectName.VisibilityFormatter的项目中没有,你有

MyProjectName.Formatter.VisibilityFormatter

您应该删除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();
            }
    }
于 2013-08-21T11:29:55.527 回答