【问题标题】:IValueConverter resource, namespace issueIValueConverter 资源,命名空间问题
【发布时间】:2013-07-14 11:23:10
【问题描述】:

我尝试实现 IValueConverter 类并将其映射为 xaml 文件中的资源。由于某种原因,我总是收到错误“命名空间 clr-namespace:MyApp 中不存在名称 TypeConverter”

但我找不到那里的问题,我的转换器类已正确设置命名空间等。

我的 xml 文件

<phone:PhoneApplicationPage
    x:Class="MyApp.Page"
    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:MyApp"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True">

...

<ListBox ItemsSource="{Binding items}" ScrollViewer.VerticalScrollBarVisibility="Disabled">

    <ListBox.Resources>
        <local:TypeConverter x:Name="TypeConverter"/>
    </ListBox.Resources>

    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>

    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding type, Converter={StaticResource TypeConverter}}"  Margin="0,0,12,0" />
        </DataTemplate>
    </ListBox.ItemTemplate>

</ListBox>

我的转换器类

namespace MyApp
{
    public class TypeConverter: IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, String culture)
        {
            return "-";
        }

        public object ConvertBack(object value, Type targetType, object parameter, String culture)
        {
            return null;
        }
    }
}

【问题讨论】:

  • 错误出现在设计器或编译时?

标签: c# namespaces windows-phone-8 ivalueconverter


【解决方案1】:

自己发现问题,convert方法参数错误,应该是:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

修复此问题后,编译器(和)设计器不再给出命名空间错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    相关资源
    最近更新 更多