XAML编辑器只是一直显示:该成员未被识别或无法访问

Hit*_*shi 3 c# wpf xaml user-controls

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

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

我也根本没有在我的属性中获得自动完成,我认为这是因为错误?我对WPF UserControls并不熟悉.

我已经多次清理并重建/重新启动了我的项目.这没有帮助,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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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(""));
}
}
Run Code Online (Sandbox Code Playgroud)

截图:

错误

C J*_*son 11

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


mrs*_*ent 5

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

试试这个

 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));
Run Code Online (Sandbox Code Playgroud)