为什么图像转换器未在命名空间中显示?

问题描述 投票:0回答:1

我试图在不锁定图像文件的情况下在列表视图中加载几百个图像。我希望能够在程序运行时删除文件。我创建了一个自定义图像转换器并将其绑定到listview。

但是,代码未编译,并且我得到'名称“ ImageConverter”在命名空间“ clr-namespace:CoolApp.ImageConverter'中不存在”

我在做什么错?

XAML:

<Window x:Class="CoolApp.MainWindow"
                        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                        xmlns:local="clr-namespace:CoolApp"
                        xmlns:c="clr-namespace:CoolApp.ImageConverter"
                        mc:Ignorable="d"
                        Title="MainWindow" MinHeight="600" Height="600" MinWidth="800" Width="800" 
                       Background="#FF222431" WindowStartupLocation="CenterScreen">
                    <Window.Resources>
                        <c:ImageConverter x:Key="ImgConverter" />
                    </Window.Resources>


    <Grid Name="ThumbGrid" Grid.Row="1" Margin="10,10,0,0" >
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>

                        <ListView x:Name="Thumbview" Grid.Row="0" BorderThickness="1" IsTextSearchEnabled="True" TextSearch.TextPath="{Binding title}" VirtualizingPanel.IsVirtualizingWhenGrouping="True" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Background="{x:Null}" BorderBrush="{x:Null}" >
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="*"/>
                                            <RowDefinition Height="*"/>
                                            <RowDefinition />
                                        </Grid.RowDefinitions>
                                        <Image Name="Thumbnails" Grid.Row="0" Source="{Binding picPath, Converter={StaticResource ImgConverter}, IsAsync=True}" Stretch="Uniform" Height="255" Width="170" ToolTip="{Binding title}" HorizontalAlignment="Center" VerticalAlignment="Center">
                                        </Image>
                                    </Grid>               
                                </DataTemplate>
                            </ListView.ItemTemplate>
                            <ListView.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <WrapPanel Orientation="Horizontal"/>
                                </ItemsPanelTemplate>
                            </ListView.ItemsPanel>
                        </ListView>
    </Grid>

</Window>

C#:

namespace CoolApp
{


    public class ImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {

                var image = new BitmapImage();
                image.BeginInit();
                image.UriSource = value is Uri ? (Uri)value : new Uri(value.ToString());
                image.CacheOption = BitmapCacheOption.OnLoad;
                image.EndInit();
                return image;

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public partial class MainWindow : Window
    {


        public MainWindow()
        {
            InitializeComponent();

        }

//code to add images to ItemsSource
}
c# wpf xml-namespaces ivalueconverter
1个回答
0
投票

您的ImageConverter是在CoolApp名称空间中定义的,当您尝试通过名称空间CoolApp.ImageConverter访问它时,因为在xaml中使用了错误的名称空间映射。您的名称空间映射应如下所示:

xmlns:c="clr-namespace:CoolApp"
© www.soinside.com 2019 - 2024. All rights reserved.