虽然代码有效,但Visual Studio 2017 Designer的“无效标记”错误

时间:2019-02-14 13:42:55

标签: c# visual-studio xaml designer

我复制了以下YouTube视频中的项目:https://www.youtube.com/watch?v=6OwyNiLPDNw&t=3602s

但是,尽管代码可以正常工作并且构建成功,但设计器仍显示“无效标记”。 (Youtube视频中的那个人遇到了同样的问题(在59:43-59:56中,但是以某种方式解决了。我仍然有同样的问题。)

XAML在以下位置带有蓝色下划线:Source =“ {Binding,显示错误:名称空间” clr-namespace:WpfTreeView“中不存在名称” HeaderToImageConverter“。程序仍然可以正常运行,我只是不这样做。看不到设计师。

我试图通过尝试以下方法来解决此问题:

  1. 在Debug / Release和x64 / x84平台之间进行配置
  2. 删除.bin .obj和.vs文件夹
  3. 删除ShadowCache文件夹
  4. 多次清洁,重建和构建解决方案
  5. 重置Visual Studio设置并重新启动计算机

XAML:

<Window x:Class="WpfTreeView.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:WpfTreeView"
        Loaded="Window_Loaded"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

        <TreeView x:Name="FolderView">
            <TreeView.Resources>

                <Style TargetType="{x:Type TreeViewItem}">
                    <Setter Property="HeaderTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <Image Width="20" Margin="3" 
                                        Source="{Binding
                                            RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}},
                                            Path=Tag,
                                            Converter={x:Static local:HeaderToImageConverter.Instance}}" />
                                    <TextBlock VerticalAlignment="Center" Text="{Binding}" />
                                </StackPanel>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </TreeView.Resources>
        </TreeView>
    </Grid>
</Window>

C#(HeaderToImageConverter类):

using System;
using System.Globalization;
using System.IO;
using System.Windows.Data;
using System.Windows.Media.Imaging;

namespace WpfTreeView
{
    /// <summary>
    /// Converts a full path to a specific image type of a drive, folder or file
    /// </summary>
    [ValueConversion(typeof(string), targetType: typeof(BitmapImage))]
    public class HeaderToImageConverter : IValueConverter
    {
        public static HeaderToImageConverter Instance = new HeaderToImageConverter();

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Get the full path
            var path = (string)value;

            // If the path is null, ignore
            if (path == null)
                return null;

            // Get the name of the file/folder
            var name = MainWindow.GetFileFolderName(path);

            // By default, we presume an image
            var image = "Images/Checkmark.png";

            // If the name is blank, we presume it's a drive as we cannot have a blank file or folder name
            if (string.IsNullOrEmpty(name))
                image = "Images/Arrow.png";
            else if (new FileInfo(path).Attributes.HasFlag(FileAttributes.Directory))
                image = "Images/Checkmark.png";

            return new BitmapImage(new Uri($"pack://application:,,,/{image}"));
        }

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

1 个答案:

答案 0 :(得分:0)

我个人认为Converter={x:Static local:HeaderToImageConverter.Instance}}" />是解决转换器的一种奇怪方法。 您还可以在

之类的app.xaml中添加转换器。
<Application x:Class="WpfTreeView.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:wpftreeview="clr-namespace:WpfTreeView"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>


        <wpftreeview:HeaderToImageConverter  
            x:Key="HeaderToImageConverter "/>

    </ResourceDictionary>
</Application.Resources>

接下来,用

引用您的转换器
Source="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}, Path=Tag, Converter={StaticResource HeaderToImageConverter}}" />

编辑:

您可以尝试将程序集添加到名称空间声明中吗? xmlns:local="clr-namespace:WpfTreeView;assembly=MyAssembly