【问题标题】:Why is the image converter not showing in namespace?为什么图像转换器没有显示在命名空间中?
【发布时间】:2020-08-31 16:00:09
【问题描述】:

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

但是,代码未编译,我得到“名称“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
}

【问题讨论】:

  • 尝试改用命名空间xmlns:c="clr-namespace:CoolApp" 吗?在您的情况下,&lt;c:ImageConverter x:Key="ImgConverter" /&gt; 被解释为 &lt;c:CoolApp.ImageConverter.ImageConverter x:Key="ImgConverter" /&gt;
  • 请注意,为了节省一些内存,您也可以在image.EndInit() 之后调用image.Freeze()。

标签: c# wpf xml-namespaces ivalueconverter


【解决方案1】:

当您告诉编译器在 CoolApp.ImageConverter 命名空间中查找 ImageConverter 时,您的 ImageConverter 是在 CoolApp 命名空间中定义的,因为您的 xaml 中的命名空间映射错误。 你的命名空间映射应该是这样的:

xmlns:c="clr-namespace:CoolApp"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-25
    • 2021-08-03
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2015-09-24
    • 2011-10-17
    • 2013-07-21
    相关资源
    最近更新 更多