XAML编辑器一直显示:“未识别成员或不可访问”

4
我创建了一个用户控件:一个文本框,在左侧显示一个图标。我的代码似乎可以工作,但是下面的错误一直出现,并使XAML编辑器将其标记为错误。

成员未被识别或无法访问。

我在属性中也没有获得自动完成,我认为这是因为出现了错误?我不太熟悉WPF用户控件。
我已经多次清理和重新构建/重启了我的项目,但都没有帮助,XAML编辑器仍然在每个自定义属性上显示此错误。
我正在使用.NET 4.6.1。
LoginWindow.xaml:
<Window x:Class="SMSBrowser.LoginWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:control="clr-namespace:SMSBrowser.Controls"
    Title="SMS Browser - Login" SizeToContent="WidthAndHeight" ResizeMode="NoResize" Background="DimGray">
<Grid>
    <StackPanel Margin="30" HorizontalAlignment="Center" VerticalAlignment="Center">
        <TextBlock Text="Test" TextAlignment="Center" FontSize="32" FontWeight="Bold" Foreground="White" Margin="0,0,0,25" />
        <control:IconTextBox CustomBackground="Yellow" CustomIconSource="..\Resources\Icons\User.ico" Height="25" Margin="0,0,0,4" />
        <control:IconTextBox CustomBackground="Red" CustomIconSource="..\Resources\Icons\Key.ico" Height="25" Margin="0,4,0,4" />
        <Button Content="Login" Height="25" Width="400" Margin="0,4,0,0" />
    </StackPanel>
</Grid>

IconTextBox.xaml

<UserControl x:Class="SMSBrowser.Controls.IconTextBox"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         mc:Ignorable="d"
         d:DesignHeight="25" d:DesignWidth="300"
         x:Name="LayoutRoot">
<Border BorderBrush="{Binding Path=CustomBorderBrush, ElementName=LayoutRoot}" BorderThickness="{Binding Path=CustomBorderThickness, ElementName=LayoutRoot}">
    <DockPanel Background="{Binding Path=CustomBackground, ElementName=LayoutRoot}">
        <Image Source="{Binding Path=CustomIconSource, ElementName=LayoutRoot}" Margin="{Binding Path=CustomIconMargin, ElementName=LayoutRoot}" DockPanel.Dock="Left" />
        <TextBox Text="{Binding Path=CustomText, ElementName=LayoutRoot}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" VerticalContentAlignment="Center" BorderThickness="0" />
    </DockPanel>
</Border>

IconTextBox.cs

namespace SMSBrowser.Controls
{
/// <summary>
/// Interaction logic for IconTextBox.xaml
/// </summary>
public partial class IconTextBox : UserControl
{
    public IconTextBox()
    {
        InitializeComponent();
        DataContext = LayoutRoot;
    }

    public string CustomBackground
    {
        get { return (string)GetValue(CustomBackgroundProperty); }
        set { SetValue(CustomBackgroundProperty, value); }
    }

    public static readonly DependencyProperty CustomBackgroundProperty =
        DependencyProperty.Register("CustomBackground", typeof(string), typeof(IconTextBox));

    public string CustomBorderBrush
    {
        get { return (string)GetValue(CustomBorderBrushProperty); }
        set { SetValue(CustomBorderBrushProperty, value); }
    }

    public static readonly DependencyProperty CustomBorderBrushProperty =
        DependencyProperty.Register("CustomBorderBrush", typeof(string), typeof(IconTextBox), new PropertyMetadata(""));

    public string CustomBorderThickness
    {
        get { return (string)GetValue(CustomBorderThicknessProperty); }
        set { SetValue(CustomBorderThicknessProperty, value); }
    }

    public static readonly DependencyProperty CustomBorderThicknessProperty =
        DependencyProperty.Register("CustomBorderThickness", typeof(string), typeof(IconTextBox), new PropertyMetadata(""));

    public string CustomIconMargin
    {
        get { return (string)GetValue(CustomIconMarginProperty); }
        set { SetValue(CustomIconMarginProperty, value); }
    }

    public static readonly DependencyProperty CustomIconMarginProperty =
        DependencyProperty.Register("CustomIconMargin", typeof(string), typeof(IconTextBox), new PropertyMetadata(""));

    public string CustomIconSource
    {
        get { return (string)GetValue(CustomIconSourceProperty); }
        set { SetValue(CustomIconSourceProperty, value); }
    }

    public static readonly DependencyProperty CustomIconSourceProperty =
        DependencyProperty.Register("CustomIconSource", typeof(string), typeof(IconTextBox), new PropertyMetadata(""));

    public string CustomText
    {
        get { return (string)GetValue(CustomTextProperty); }
        set { SetValue(CustomTextProperty, value); }
    }

    public static readonly DependencyProperty CustomTextProperty =
        DependencyProperty.Register("CustomText", typeof(string), typeof(IconTextBox), new PropertyMetadata(""));
}
}

截图: 错误

2
你的用户控件的“DataContext”设置了吗? - mrsargent
在 IconTextBox 的构造函数中,它被设置为 LayoutRoot,我希望这是正确的。 - HitomiTenshi
你的意思是我应该将 public string CustomBackground 和它的 setter 设置为返回 Brush 而不是 string 吗?我已经这样做了,但是错误仍然存在,包括所有其他值。如果我也将 DP 值更改为 typeof(Brush),它会抛出一个错误。System.ArgumentException: Default value type does not match type of property 'CustomBorderBrush'. - HitomiTenshi
我今天尝试复制了你的问题,但是你的代码对我来说完全正常。我正在使用带有.NET 4.6.1的VS2015专业版。现在不确定该告诉你什么。 - mrsargent
2
我添加了一张截图,但仍然出现错误。:( 编译工作正常,但XAML编辑器出现错误。 - HitomiTenshi
显示剩余2条评论
3个回答

11

我也曾经遇到过这个问题。最后只好关闭Visual Studio并重新启动。重启后,XAML设计时编辑器又能正常工作了。


这对我也起作用了,所以从0点赞回来! - John
是的,我在XAML中引用枚举作为DataTrigger值时也遇到了这个问题。+1 - JMIII

7

我能够重现你的问题并找到了解决办法。
你需要在CustomBackgroundProperty中添加PropertyMetadata的默认值。

尝试以下代码:

 public string CustomBackground {
        get { return (string)GetValue(CustomBackgroundProperty); }
        set { SetValue(CustomBackgroundProperty, value); }
 }

 public static readonly DependencyProperty CustomBackgroundProperty =  
        DependencyProperty.Register("CustomBackground", typeof(string),
        typeof(IconTextBox),new PropertyMetadata(null));

0
在哪一行出现了错误?我使用vs2010尝试了你的代码,没有任何错误。

如果我声明自定义控件时没有包含自定义属性,不会显示任何错误。但是如果我开始使用自定义属性,该属性将被标记为红色并显示错误。尽管编译和运行没有错误。我正在使用带有所有最新更新的vs2013。 - HitomiTenshi
好的,我已经尝试将用户控件模板中文本框的背景设置为透明,并且它成功地工作而没有显示任何异常(自定义背景属性)。我相信原因是我正在使用较旧的.Net版本,希望有人能提出新的想法。 - Ahmad Alloush

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接