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



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

我写了简单的ImageButton UserControl.每当我从 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。

相关内容

最新更新