视频地址:在XAML中引用类库及代码后置
- 首先完成一个WPF用户控件库项目,得到.dll类型的库文件,以下例子创建一个简单的button控件。
<UserControl x:Class="WpfControlLibrary.MyUserControl"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Button x:Name="button" Content="I'M LIBRARY Example" Width="200" Height="30" Background="AliceBlue"/>
</Grid>
</UserControl>
- 创建WPF应用程序,并添加对库文件的引用
- 在项目中引用库文件之后,在.xaml文件中引用库文件的命名空间,本例中为“WpfControlLibrary”,其中controls为对命名空间的标记名称。
xmlns:controls="clr-namespace:WpfControlLibrary;assembly=WpfControlLibrary"
- 实例化控件对象:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions >
<RowDefinition/>
<RowDefinition />
</Grid.RowDefinitions>
<controls:MyUserControl Grid.Row="0" Grid.Column="0"/>
<controls:MyUserControl Grid.Row="0" Grid.Column="1"/>
<controls:MyUserControl Grid.Row="1" Grid.Column="0"/>
<controls:MyUserControl Grid.Row="1" Grid.Column="1"/>
</Grid>