Search code examples
c#wpfuser-controls.net-assembly

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


I have solution with one wpf application assembly MyApp and one .NET class library assembly CutomUC. The CustomUC class library is supposed to hold all of my custom user controls which are used from wpf application.

I have written simple ImageButton UserControl. Whenever I set some dependency property of my custom control from wpf application, designer wont render, and I get the following warning (or error, it is shown as blue underline in xaml):

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

However, application builds and runs just fine.

Here is my custom UserControl, from CustomUC assembly:

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);
        }
    }
}

Here is 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>

And here is MainWindow.xaml from MyApp assembly:

<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>

Text, Image and Orientation all show the same warning. I want to be able to access dependency properties as if the control was in the same assembly (I want designer to render correctly, and autocomplete feature). Is that possible, am I missing something?


Solution

  • Turns out everything was fine. Once I got back to the computer, turned it and VS on everything was working as expected. My best guess would be that VS didn't load something that was needed yesterday.

    If you are having the same problem, try restarting your VS.