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

    运行结果

  • 相关阅读:
    数据结构小练习
    【BZOJ 3652】大新闻 数位dp+期望概率dp
    【BZOJ 3326】[Scoi2013]数数 数位dp+矩阵乘法优化
    【Codeforces 506E】Mr.Kitayuta’s Gift&&【BZOJ 4214】黄昏下的礼物 dp转有限状态自动机+矩阵乘法优化
    【BZOJ 4455】 [Zjoi2016]小星星 容斥计数
    凸包小结
    Matrix-Tree定理题表
    PLAN OF HEOI(unfinished)
    (ex)BSGS题表
    exBSGS板子
  • 原文地址:https://www.cnblogs.com/lizhiqiang0204/p/10898246.html
Copyright © 2011-2022 走看看