【问题标题】:XAML editor just keeps showing: The member is not recognized or is not accessibleXAML 编辑器一直显示:该成员无法识别或无法访问
【发布时间】:2016-01-28 15:11:56
【问题描述】:

我创建了一个用户控件:一个在左侧显示图标的文本框。我的代码似乎可以工作,但以下错误不断显示并使 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(""));
}
}

截图:

【问题讨论】:

  • 你有你的用户控制集DataContext吗?
  • IconTextBox的构造函数中设置为LayoutRoot,希望是对的。
  • 尝试将颜色的数据类型设置为 Brush 而不是 string
  • 你的意思是我应该设置public string CustomBackground 并且它的设置器是/返回一个画笔而不是一个字符串?我这样做了,并且该背景值(以及所有其他值)仍然存在错误。如果我还将 DP 值更改为 typeof(Brush) ,则会引发错误。 System.ArgumentException: Default value type does not match type of property 'CustomBorderBrush'.
  • 我添加了一个截图,我仍然得到错误。 :( 编译工作正常,但 XAML 编辑器出现错误。

标签: c# wpf xaml user-controls


【解决方案1】:

我遇到了这个问题。我最终不得不关闭视觉工作室并重新启动它。一旦我这样做了,XAML 设计时编辑器就会再次工作。

【讨论】:

  • 这对我也有效,所以从 0 开始支持!
  • 是的,这个问题在 xaml 中为数据触发器值 +1 引用枚举
【解决方案2】:

我能够复制您的问题并找到解决方案。
您需要在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));

【讨论】:

    【解决方案3】:

    错误显示在哪一行?我已经使用 vs2010 尝试了您的代码,并且没有任何错误。

    【讨论】:

    • 如果我在没有自定义属性的情况下声明自定义控件,则不会显示错误。但是,如果我开始使用我的自定义属性,该属性会被标记为红色并显示错误。编译和运行虽然没有错误。我正在使用带有所有最新更新的 vs2013。
    • 好的,我尝试将用户控件模板内的文本框的背景设置为透明,并且它没有显示任何异常(custombackground 属性),我相信原因是我使用的是较旧的 .Net 版本希望有人带着新的 idia 来
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-07
    • 1970-01-01
    • 2012-11-09
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    相关资源
    最近更新 更多