HandyControl 第三库的使用
- 第一步:在自己所创建的项目右键—>管理NuGet程序包—>点击浏览---->在搜索框输入handycontrol点击下载—>等待安装好
- 第二步:回到资源管理器—>点击App.xaml文件—>进去后赋值下面这段代码并保存
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml"/>
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
- 第三步:回到xaml文件 添加命名控件---->引入完第三方库----->重新启动VS或者重新生成项目这样就可以使用对控件的使用
xmlns:hc="https://handyorg.github.io/handycontrol"
小例子的运用
第一次运用的点:变量&命名&赋值&加号&Convert转换 占位符 转义符号
- 步骤思路:建一个项目,添加一个按钮(计算和)和两个文本框,点击按钮后,用两个变量存一下两个文本框的值 弹出两个变量的和
- 再添加一个按钮打印文字,点击按钮后弹出输入文本框的内容,使用占位符。
首先界面的设计
- TextBox控件在同一个StackPanel堆叠面板:由于应用的第三方库这个控件就直接是居中位置不需改动
- 对TextBox控件的使用第三方库用一些属性进行改变;
hc:InfoElement.Title="请输入内容" Style="{StaticResource TextBoxExtend}" Foreground="MediumPurple" />
这是样式的改变。 - 同理Button控件也是这样
<Grid>
<StackPanel Margin="0,0,-0.4,0">
<TextBox Name="TestOne"
Width="260"
hc:InfoElement.Title="请输入内容"
Background="Pink"
FontFamily="宋体"
FontWeight="Bold"
Foreground="MediumPurple"
Style="{StaticResource TextBoxExtend}" />
<TextBox Name="TestTwo"
Width="260"
Margin="15"
Style="{StaticResource TextBoxExtendBaseStyle}" />
</StackPanel>
<StackPanel Margin="268,128,255.6,237" Orientation="Horizontal">
<Button Width="120"
Height="35"
Click="Button_Click"
Content="计算"
FontSize="15"
FontWeight="Bold"
Style="{StaticResource ButtonInfo}" />
<Button Width="120"
Height="35"
Click="Button_Click_1"
Content="文本"
FontSize="15"
FontWeight="Bold"
Style="{StaticResource ResourceKey=ButtonWarning}" />
</StackPanel>
</Grid>
界面如下
后台代码的梳理
- 计算这个button
- 注意的点是;(1)对输入文本框的内容做一个是否输入的数字做一个区分,(2)对输入的内容做一个转int类型的转换用到Convert.ToInt32
private void Button_Click(object sender, RoutedEventArgs e) {
try {
int NumberOne = Convert.ToInt32(TestOne.Text);
int NumberTwo = Convert.ToInt32(TestTwo.Text);
int Sum = NumberOne + NumberTwo;
MessageBox.Show(NumberOne.ToString() + "加" + NumberTwo.ToString() + "的总和是" + Sum.ToString());
}
catch {
MessageBox.Show("输入有误请重新输入!!!");
}
}
- 文本这个button
- 注意的点:(1)定义输入的内容为string类型并且要转换为文本类型,(2)在wpf后台如何使用占位符
private void Button_Click_1(object sender, RoutedEventArgs e) {
string s1 = TestOne.Text;
string s2 = TestTwo.Text;
MessageBox.Show(string.Format("第一个文本的内容是:{0}\n第二个文本的内容是:{1}", s1, s2));
}