"The member is not recognized or is not accessible" 当使用来自不同程序集的自定义 UserControl 时

"The member is not recognized or is not accessible" when using custom UserControl from different assembly

我有一个 wpf 应用程序程序集 MyApp 和一个 .NET class 库程序集 CutomUC 的解决方案。 CustomUC class 库应该包含我在 wpf 应用程序中使用的所有自定义用户控件。

我写的很简单ImageButtonUserControl。每当我从 wpf 应用程序设置我的自定义控件的某些依赖项 属性 时,设计器将不会呈现,并且我收到以下警告(或错误,它在 xaml 中显示为蓝色下划线):

The member "[member name]" is not recognized or is not accessible

但是,应用程序构建和运行都很好。

这是我的自定义 UserControl,来自 CustomUC 程序集:

public partial class ImageButton : UserControl
    {
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(ImageButton), new UIPropertyMetadata(OnArrangementChanged));
        public static readonly DependencyProperty ImageProperty =
            DependencyProperty.Register("Image", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata(null));
        public static readonly DependencyProperty ImageMarginProperty =
            DependencyProperty.Register("ImageMargin", typeof(string), typeof(ImageButton), new UIPropertyMetadata("0, 0, 0, 0"));
        public static readonly DependencyProperty OrientationProperty =
            DependencyProperty.Register("Orientation", typeof(Orientation), typeof(ImageButton), new UIPropertyMetadata(new PropertyChangedCallback(OnArrangementChanged)));

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
    public ImageSource Image
    {
        get { return (ImageSource)GetValue(ImageProperty); }
        set { SetValue(ImageProperty, value); }
    }
    public string ImageMargin
    {
        get { return (string)GetValue(ImageMarginProperty); }
        set { SetValue(ImageMarginProperty, value); }
    }
    public Orientation Orientation
    {
        get { return (Orientation)GetValue(OrientationProperty); }
        set { SetValue(OrientationProperty, value); }
    }

    private static void OnArrangementChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        var myUserControl = sender as ImageButton;
        myUserControl.RearangeElements();
    }

    public ImageButton()
    {
        InitializeComponent();
        RearangeElements();
    }

    private void RearangeElements()
    {
        if (String.IsNullOrEmpty(Text))
        {
            Grid.SetRow(ButtonImage, 0);
            Grid.SetColumn(ButtonImage, 0);
            Grid.SetRowSpan(ButtonImage, 2);
            Grid.SetColumnSpan(ButtonImage, 2);
        }
        else if (this.Orientation == Orientation.Horizontal)
        {
            Grid.SetColumnSpan(ButtonImage, 1);
            Grid.SetColumnSpan(ButtonTextBlock, 1);
            Grid.SetRowSpan(ButtonTextBlock, 2);
            Grid.SetRowSpan(ButtonImage, 2);
            Grid.SetColumn(ButtonImage, 0);
            Grid.SetColumn(ButtonTextBlock, 1);
            Grid.SetRow(ButtonImage, 0);
            Grid.SetRow(ButtonTextBlock, 0);
        }
        else if (this.Orientation == Orientation.Vertical)
        {
            Grid.SetColumnSpan(ButtonTextBlock, 2);
            Grid.SetColumnSpan(ButtonImage, 2);
            Grid.SetRowSpan(ButtonTextBlock, 1);
            Grid.SetRowSpan(ButtonImage, 1);
            Grid.SetRow(ButtonImage, 0);
            Grid.SetRow(ButtonTextBlock, 1);
            Grid.SetColumn(ButtonImage, 0);
            Grid.SetColumn(ButtonTextBlock, 0);
        }
    }
}

这里是ImageButton.xaml:

<UserControl x:Class="CustomUC.UserControls.ImageButton"
            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" 
             xmlns:local="clr-namespace:CustomUC.UserControls"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             Name="ImageButton"
             Background="Transparent"
             BorderBrush="Transparent"
             BorderThickness="0">
    <Button Name="ButtonElement"
        Height="{Binding ElementName=ImageButton, Path=Height}"
        Width="{Binding ElementName=ImageButton, Path=Width}">
        <Button.Content>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="1*"/>
                    <RowDefinition Height="1*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*"/>
                    <ColumnDefinition Width="2*"/>
                </Grid.ColumnDefinitions>
                <Image Name="ButtonImage" Source="{Binding ElementName=ImageButton, Path=Image}" Margin="{Binding ElementName=ImageButton, Path=ImageMargin}"/>
                <TextBlock Name="ButtonTextBlock" Text="{Binding ElementName=ImageButton, Path=Text}" TextWrapping="Wrap" TextAlignment="Center" VerticalAlignment="Center"/>
            </Grid>
        </Button.Content>
    </Button>
</UserControl>

这是来自 MyApp 程序集的 MainWindow.xaml

<Window x:Class="MyApp.View.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:resx="clr-namespace:MyApp.localization"
        xmlns:local="clr-namespace:MyApp.View"
        xmlns:customuc="clr-namespace:CustomUC.UserControls;assembly=CustomUC"
        mc:Ignorable="d"
        Title="MainWindow" Height="720" Width="1280">
    <Grid>
        <customuc:ImageButton Width="100" Height="100" Text="Hello!" Image="/MyApp;component/Assets/352456 - drive file insert.ico" Orientation="Vertical" Margin="840,103,332,486"/>
    </Grid>
</Window>

TextImageOrientation 都显示相同的警告。我希望能够访问依赖属性,就像控件在同一个程序集中一样(我希望设计器能够正确呈现和自动完成功能)。这可能吗,我错过了什么吗?

原来一切都很好。一旦我回到电脑前,打开它,然后打开 VS,一切都按预期工作。我最好的猜测是 VS 没有加载昨天需要的东西。

如果您遇到同样的问题,请尝试重新启动您的 VS。