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

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

我复制了这个Youtube视频制作的项目:https://www.youtube.com/watch?v=6OwyNiLPDNw&t=3602s

但是,设计师显示“无效标记”,尽管代码工作正常并且构建成功。 (Youtube视频中的那个人遇到同样的问题(59:43-59:56),但不知怎的解决了。我还有同样的问题。)

XAML给出了一个蓝色下划线:Source =“{Binding,显示错误:名称”HeaderToImageConverter“在名称空间”clr-namespace:WpfTreeView“中不存在。程序运行正常,我只是看不到设计师。

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

  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();
        }
    }
}
c# visual-studio xaml designer
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.