wpf XAML 编辑器一直显示:该成员无法识别或不可访问

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35065133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:39:05  来源:igfitidea点击:

XAML editor just keeps showing: The member is not recognized or is not accessible

c#wpfxamluser-controls

提问by HitomiTenshi

I created an UserControl: A TextBox that shows an icon on the left side. My code seems to work but the following error keeps showing and makes the XAML editor mark it as an error.

我创建了一个 UserControl:一个在左侧显示一个图标的 TextBox。我的代码似乎可以工作,但以下错误不断显示并使 XAML 编辑器将其标记为错误。

The member is not recognized or is not accessible.

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

I also don't get autocompletion inside my properties at all, I assume it is because of the error? I'm not that familiar with WPF UserControls.

我的属性中也根本没有自动完成功能,我认为这是因为错误?我对 WPF UserControls 不太熟悉。

I have already cleaned and rebuilt / restarted my project multiple times. That didn't help, the XAML editor just keeps showing this error on every custom property.

我已经多次清理和重建/重新启动我的项目。这没有帮助,XAML 编辑器只是在每个自定义属性上不断显示此错误。

I'm using .NET 4.6.1.

我正在使用 .NET 4.6.1。

LoginWindow.xaml:

登录窗口.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

图标文本框.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

图标文本框.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(""));
}
}

Screenshot:

截屏:

Errors

错误

回答by mrsargent

I was able to replicate your problem and found a solution.
You need to add a default value for PropertyMetadatain CustomBackgroundProperty.

我能够复制您的问题并找到解决方案。
您需要为PropertyMetadatain添加一个默认值CustomBackgroundProperty

Try this

尝试这个

 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));

回答by C Johnson

I had this problem. I eventually had to shutdown visual studio and restart it. Once I did that the XAML design time editor worked again.

我有这个问题。我最终不得不关闭 Visual Studio 并重新启动它。一旦我这样做了,XAML 设计时编辑器又可以工作了。

回答by Ahmad Alloush

On which line does the error show? I have tried your code using vs2010 and it worked without any error.

错误显示在哪一行?我已经使用 vs2010 尝试过你的代码,它没有任何错误。