首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >XAML编辑器只是一直显示:成员无法识别或不可访问

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

Stack Overflow用户
提问于 2016-01-28 23:11:57
回答 3查看 11.1K关注 0票数 4

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

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

我也没有在我的属性中得到自动补全,我想是因为这个错误吧?我对WPF UserControls不是很熟悉。

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

我使用的是.NET 4.6.1。

LoginWindow.xaml:

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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

代码语言:javascript
复制
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(""));
}
}

截图:

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-01-30 00:51:09

我复制了你的问题并找到了解决方案。

您需要在CustomBackgroundProperty中为PropertyMetadata添加一个默认值。

尝尝这个

代码语言:javascript
复制
 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));
票数 7
EN

Stack Overflow用户

发布于 2018-07-25 05:01:26

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

票数 11
EN

Stack Overflow用户

发布于 2016-01-28 23:37:22

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35065133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档