转换器类的 XAML 命名空间错误

Posted

技术标签:

【中文标题】转换器类的 XAML 命名空间错误【英文标题】:XAML namespace error for converter class 【发布时间】:2015-10-29 02:20:04 【问题描述】:

我收到一个错误:名称“local:ThumbnailConverter”在 using.Notes 的命名空间中不存在

所有文件都在命名空间Notes中

Converters.Cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media.Imaging;

namespace Notes

    public class ThumbnailConverter : IValueConverter
    
        public object Convert(object value, Type targetType, object parameter, string culture)
        
            if (value != null)
            
                var thumbnailStream = (IRandomAccessStream)value;
                var image = new BitmapImage();
                image.SetSource(thumbnailStream);
                return image;
            
            return DependencyProperty.UnsetValue;
        
        public object convertBack(object value, Type targetType, object parameter, string culture)
        
            throw new NotImplementedException();
        
    

MainPage.xaml

<Page
    x:Class="Notes.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Notes"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <UserControl.Resources>
        <local:ThumbnailConverter x:Name="thumbnailConverter" />
            <DataTemplate x:Key="Custom190x130ItemTemplate">
                <Grid Width="190" Height="130">
                    <Border Background="StaticResource ListViewItemPlaceholderBackgroundThemeBrush" Width="190" Height="130">
                        <Image Source="Binding Path=Thumbnail, Converter=StaticResource thumbnailConverter" Width="190" Height="130"/>
                    </Border>
                </Grid>
            </DataTemplate>

        <CollectionViewSource x:Name="itemViewSource" />
    </UserControl.Resources>

    <GridView x:Name="itemGridView"
          AutomationProperties.AutomationId="ItemGridView"
          AutomationProperties.Name="Items"
          Grid.Row="1"
           ItemsSource="Binding Source=StaticResource itemsViewSource"
    ItemTemplate="StaticResource Custom190x130ItemTemplate"
    SelectionMode="None" />


</Page>

现在是 MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage.BulkAccess;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace Notes

    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    
        public MainPage()
        
            this.InitializeComponent();

            //query jisse data retrieve karenge
            var filterType = new string[]  ".jpg", ".png" ;
            var options = new Windows.Storage.Search.QueryOptions(Windows.Storage.Search.CommonFileQuery.OrderByDate, filterType);
            options.ApplicationSearchFilter = "System.Photo.DateTaken:>System.StructuredQueryType.DateTime#LastMonth";
            var fileQuery = Windows.Storage.KnownFolders.PicturesLibrary.CreateFileQueryWithOptions(options);


            //creating the data source
            var fileInformationFactory = new FileInformationFactory(fileQuery,Windows.Storage.FileProperties.ThumbnailMode.PicturesView,190,Windows.Storage.FileProperties.ThumbnailOptions.UseCurrentScale,true);



        
    



请帮助解决可能的解决方案!提前致谢

【问题讨论】:

你应该写 而不是 但也许这不是问题... 【参考方案1】:

在 XAML 中声明命名空间的正确方法如下:

xmlns:MyNamespace="clr-namespace:Your.Namespace.Here"

所以在你的情况下,它看起来像这样:

xmlns:local="clr-namespace:Notes"

【讨论】:

非常感谢! :) 没问题 :-) 如果这回答了您的问题,请不要忘记通过单击左侧的勾号将其标记为已回答。谢谢。 请回答第二个问题

以上是关于转换器类的 XAML 命名空间错误的主要内容,如果未能解决你的问题,请参考以下文章

Powershell 中的自定义 XAML 命名空间

这个命名空间冲突是由于 XAML 到 .NET 代码生成中的错误吗?

无法在WPF中创建继承的usercontrol,本地命名空间中的基本控件“不存在”

一个wpf命名空间问题

Silverlight XAML 命名空间范围

“clr-namespace” URI 指的是程序集中未包含的命名空间