我有一个wpf应用程序集的解决方案
MyApp
还有一个。NET类库程序集
CutomUC
. 这个
CustomUC
类库应该保存从wpf应用程序使用的所有自定义用户控件。
我写的很简单
ImageButton
UserControl
. 每当我从wpf应用程序中设置自定义控件的某些依赖项属性时,设计器都不会呈现,我会收到以下警告(或错误,在xaml中显示为蓝色下划线):
The member "[member name]" is not recognized or is not accessible
然而,应用程序构建和运行都很好。
这是我的习惯
用户控件
从…起
自定义UC
组件:
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>
Text
,
Image
和
Orientation
都显示相同的警告。我希望能够像控件位于同一程序集中一样访问依赖项属性(我希望设计器能够正确呈现,并具有自动完成功能)。有没有可能,我错过了什么?