DLL 标签 标签

  • 一句话的事儿:将设计好的控件供第三方使用。

效果介绍

  • 将设计好的按钮控件封装成DLL,供其他开发者使用;
  • 调用dll

    逻辑介绍

  • 首先将控件封装,通过lib方式进行;

  • 创建调用控件的窗口;

    具体实现步骤

  1. 创建一个project(wpf library);
  2. 设计按钮组件

    1. <UserControl x:Class="FSButtonlib.UserControl1"
    2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    6. xmlns:local="clr-namespace:FSButtonlib"
    7. mc:Ignorable="d"
    8. d:DesignHeight="50" d:DesignWidth="150">
    9. <Grid>
    10. <Grid.Resources>
    11. <Style x:Key="beautifulBtn" TargetType="Button">
    12. <Setter Property="Width" Value="150"/>
    13. <Setter Property="Height" Value="50"/>
    14. <Setter Property="Template">
    15. <Setter.Value>
    16. <ControlTemplate>
    17. <Grid RenderTransformOrigin="0.5,0.5">
    18. <Grid.RenderTransform>
    19. <RotateTransform Angle="0" x:Name="gridRotate"/>
    20. </Grid.RenderTransform>
    21. <Grid.Triggers>
    22. <EventTrigger RoutedEvent="FrameworkElement.Loaded">
    23. <BeginStoryboard>
    24. <Storyboard>
    25. <DoubleAnimation Storyboard.TargetName="gridRotate" Storyboard.TargetProperty="Angle" To="360" Duration="0:0:2"/>
    26. </Storyboard>
    27. </BeginStoryboard>
    28. </EventTrigger>
    29. </Grid.Triggers>
    30. <TextBlock x:Name="txtBlock" Text="{TemplateBinding Button.Content}" TextAlignment="Center" VerticalAlignment="Center" FontSize="25" Foreground="Blue"/>
    31. <!--<Rectangle x:Name="rec" Width="100" Height="50" Fill="Violet" RadiusX="25" RadiusY="25" Opacity="0.5"></Rectangle>-->
    32. <Rectangle x:Name="rec" Width="100" Height="50" RadiusX="25" RadiusY="25" Opacity="0.5">
    33. <!--将填充部分单独写,为了后面单独调用做准备-->
    34. <Rectangle.Fill>
    35. <!--采用刷子来填充-->
    36. <SolidColorBrush x:Name="recBrush" Color="Blue"/>
    37. </Rectangle.Fill>
    38. <Rectangle.Triggers>
    39. <EventTrigger RoutedEvent="UIElement.MouseEnter">
    40. <BeginStoryboard>
    41. <Storyboard>
    42. <DoubleAnimation Storyboard.TargetName="txtBlock" Storyboard.TargetProperty="FontSize" To="30" Duration="0:0:0.5"/>
    43. <ColorAnimation Storyboard.TargetName="recBrush" Storyboard.TargetProperty="Color" To="Gray" Duration="0:0:1"/>
    44. </Storyboard>
    45. </BeginStoryboard>
    46. </EventTrigger>
    47. <EventTrigger RoutedEvent="UIElement.MouseLeave">
    48. <BeginStoryboard>
    49. <Storyboard>
    50. <DoubleAnimation Storyboard.TargetName="txtBlock" Storyboard.TargetProperty="FontSize" To="25" Duration="0:0:0.5"/>
    51. <DoubleAnimation Storyboard.TargetName="txtBlock" Storyboard.TargetProperty="FontSize" To="25" Duration="0:0:0.5"/>
    52. <ColorAnimation Storyboard.TargetName="recBrush" Storyboard.TargetProperty="Color" To="Blue" Duration="0:0:1"/>
    53. </Storyboard>
    54. </BeginStoryboard>
    55. </EventTrigger>
    56. </Rectangle.Triggers>
    57. </Rectangle>
    58. </Grid>
    59. </ControlTemplate>
    60. </Setter.Value>
    61. </Setter>
    62. </Style>
    63. </Grid.Resources>
    64. <Button x:Name="btn" Style="{StaticResource beautifulBtn}" Content="按钮模板" Height="50" Width="150" HorizontalAlignment="Left" VerticalAlignment="Top" />
    65. </Grid>
    66. </UserControl>

    设全局变量ButtonContent ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes;

namespace FSButtonlib { ///

/// Interaction logic for UserControl1.xaml /// public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); }

  1. private string buttonContent;
  2. public string ButtonContent
  3. {
  4. get { return buttonContent; }
  5. set { buttonContent = value;
  6. this.Dispatcher.Invoke(() =>
  7. {
  8. btn.Content= value;
  9. });
  10. }
  11. }
  12. }

}

  1. 3. 调用按钮组件
  2. 3. 调用按钮组件时,Content设为全局变量,从而方便随时更改
  3. ```xml
  4. <Window x:Class="WPFApp.MainWindow"
  5. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  6. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  7. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  8. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  9. xmlns:local="clr-namespace:WPFApp"
  10. xmlns:c="clr-namespace:FSButtonlib;assembly=FSButtonlib"
  11. mc:Ignorable="d"
  12. Title="MainWindow" Height="450" Width="800">
  13. <Grid>
  14. <c:UserControl1 Height="50" Width="150" ButtonContent="测试按钮内容" Margin="321,184.5,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
  15. </Grid>
  16. </Window>

  • 本文作者:GeekPower - Felix Sun
  • 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!