为什么图像转换器没有显示在名称空间中?
Why is the image converter not showing in namespace?
我正在尝试在不锁定图像文件的情况下在列表视图中加载几百张图像。我希望能够在程序 运行 时删除文件。我创建了一个自定义图像转换器并将其绑定到列表视图。
但是,代码没有编译,我得到 'The name "ImageConverter" does not exist in the namespace "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
}
您的 ImageConverter
是在 CoolApp
命名空间内定义的,而您告诉编译器在 CoolApp.ImageConverter
命名空间内查找 ImageConverter
,因为您的命名空间映射错误xaml。
您的命名空间映射应该是这样的:
xmlns:c="clr-namespace:CoolApp"
我正在尝试在不锁定图像文件的情况下在列表视图中加载几百张图像。我希望能够在程序 运行 时删除文件。我创建了一个自定义图像转换器并将其绑定到列表视图。
但是,代码没有编译,我得到 'The name "ImageConverter" does not exist in the namespace "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
}
您的 ImageConverter
是在 CoolApp
命名空间内定义的,而您告诉编译器在 CoolApp.ImageConverter
命名空间内查找 ImageConverter
,因为您的命名空间映射错误xaml。
您的命名空间映射应该是这样的:
xmlns:c="clr-namespace:CoolApp"