WPF笔记1-App.xaml & MainWindow.xaml

本文介绍了WPF应用程序的基本结构,包括App.xaml和MainWindow.xaml的作用。App.xaml定义了应用启动窗口,而MainWindow.xaml则展示了窗口布局及元素,如包含了一个Button控件。在程序执行过程中,App类构造函数完成后会转向主窗口。在编译时,Xaml文件转换为g.cs文件,最终与cs文件一起生成可执行程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  •       WPF程序结构

           在Visual Studio中创建WPF应用程序,项目包括App和MainWindow两组文件。每组文件包括Xaml文件和cs文件,其中App.xaml文件的内容如下。

<Application x:Class="WpfApplication2.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
         
    </Application.Resources>
</Application>


  •         XAML文件内容                                             &nbs
WPF中,`App.xaml`文件通常用于全局配置,包括窗体模板(Template)其他静态资源。如果你想要在`App.xaml`中配置按钮(Button)的模板,你可以定义一个新的`ControlTemplate`来替换默认的模板。下面是一个例子: ```xml &lt;Window x:Class=&quot;YourNamespace.App&quot; xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot; xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot; Title=&quot;MainWindow&quot; StartupUri=&quot;MainWindow.xaml&quot;&gt; &lt;!-- App.xaml 文件配置 Button 的模板 --&gt; &lt;Application.Resources&gt; &lt;ControlTemplate x:Key=&quot;CustomButtonTemplate&quot;&gt; &lt;!-- 模板内容可以包括控件布局 --&gt; &lt;Border Background=&quot;{TemplateBinding Background}&quot; BorderBrush=&quot;{TemplateBinding BorderBrush}&quot; BorderThickness=&quot;{TemplateBinding BorderThickness}&quot;&gt; &lt;ContentPresenter HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;/&gt; &lt;/Border&gt; &lt;!-- 可能包含的状态触发器,例如鼠标悬停 --&gt; &lt;ControlTemplate.Triggers&gt; &lt;Trigger Property=&quot;IsMouseOver&quot; Value=&quot;True&quot;&gt; &lt;Setter Property=&quot;BorderBrush&quot; Value=&quot;Blue&quot;/&gt; &lt;/Trigger&gt; &lt;/ControlTemplate.Triggers&gt; &lt;/ControlTemplate&gt; &lt;/Application.Resources&gt; &lt;!-- 主窗口内容 --&gt; &lt;Grid&gt; &lt;Button Content=&quot;Click me&quot; Template=&quot;{StaticResource CustomButtonTemplate}&quot;/&gt; &lt;/Grid&gt; &lt;/Window&gt; ``` 在这个例子中,我们创建了一个名为`CustomButtonTemplate`的新`ControlTemplate`,它定义了一个带有一个内嵌`ContentPresenter`的`Border`,并设置了鼠标悬停时的边框颜色。然后在按钮元素上,我们通过`Template`属性将这个自定义模板应用到了每个按钮上。 如果你想要所有按钮都自动采用这个新模板,你可以在`Button`的`xaml`声明里省略掉`Template`属性,因为模板已经作为静态资源存在。这样,只要你在`App.xaml`中配置了这个模板,所有新建的按钮就会按照这个模板显示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值