"该成员未被识别或无法访问"从不同的程序集使用自定义UserControl时

时间:2018-01-25 12:27:46

标签: c# wpf user-controls .net-assembly

我有一个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>

以下是来自MainWindow.xaml汇编的MyApp

<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都显示相同的警告。我希望能够访问依赖项属性,就像控件在同一个程序集中一样(我希望设计器能够正确渲染,并且自动完成功能)。这可能吗,我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

原来一切都很好。一旦我回到计算机,转动它,VS上的一切都按预期工作。我最好的猜测是VS并没有加载昨天所需的东西。

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