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は、次の 2 つのプロパティを持つ単純なパブリック クラスです。

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

この質問に似ていますが、移行は含まれていません。私は最初からWP8で開発しています。

私は何を達成しようとしていますか?良い。そのプロパティにコントロールを表示するかどうかを格納していboolます。XAML コントロールのプロパティは のみを認識し、 を認識しVisibility enumないboolため、それをその に変換する必要がありenumます。

4

2 に答える 2

6

クラスのVisibilityFormatter内部クラスですFormatter。このクラスは実際には必要ありません。最上位のクラスにFormatterするだけVisibilityFormatterで、XAML パーサーがそれを見つけます。

また、コンバーターの一般的な命名規則はXXXConverterand notXXXFormatterですが、それは規則ではありません。

于 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 に答える