zoukankan      html  css  js  c++  java
  • WPF添加类库并引用

    源码地址:https://github.com/lizhiqiang0204/-WpfApp2.git

    首先利用WPF向导创建一个空的项目

    using System.Windows;
    
    namespace WpfApp2
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
        }
    }
    <Window x:Class="WpfApp2.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:local="clr-namespace:WpfApp2"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Grid>
            
        </Grid>
    </Window>

    右击解决方案->添加->新建项

    找到类库(.NET Framework)(我这个VS是2019,不同版本的VS这个添加界面有所不同)

    然后点击下一步,创建

    把生成的Class1.cs修改一下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ClassLibrary1
    {
        public class Calculate
        {
            public int sum(int a, int b)//
            {
                return a + b;
            }
    
            public int sub(int a, int b)//
            {
                return a - b;
            }
        }
    }

    右击ClassLibrary1项目,生成库文件DLL

    生成成功后,后显示已生成dll文件,此时MainWindow.xaml.cs就可以引用这个库文件了

    修改MainWindow.xaml.cs文件如下:

    using ClassLibrary1;//引用自己创建的类库
    using System.Windows;
    
    namespace WpfApp2
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            Calculate calculate = new Calculate();//实例化类库里的类
            int sum = 0;
            public MainWindow()
            {
                InitializeComponent();
                sum = calculate.sum(1, 2);
                MessageBox.Show("算术和为:" + sum.ToString());
            }
        }
    }

    运行结果

  • 相关阅读:
    二分图 洛谷P2055 [ZJOI2009]假期的宿舍
    并查集 洛谷P1640 [SCOI2010]连续攻击游戏
    贪心 洛谷P2870 Best Cow Line, Gold
    贪心 NOIP2013 积木大赛
    快速幂 NOIP2013 转圈游戏
    倍增LCA NOIP2013 货车运输
    树形DP 洛谷P2014 选课
    KMP UVA1328 Period
    动态规划入门 BZOJ 1270 雷涛的小猫
    KMP POJ 2752Seek the Name, Seek the Fame
  • 原文地址:https://www.cnblogs.com/lizhiqiang0204/p/10898246.html
Copyright © 2011-2022 走看看