使用代码和未经编译的XAML创建WPF应用程序

这篇博客介绍了如何在WPF应用中不使用APP.XAML,而是通过创建自定义的Program类来加载并显示位于相对路径的XAML文件。示例代码展示了如何在MainWindow中动态加载指定路径的XAML内容,并且处理其中的元素事件,如按钮点击事件。
摘要由CSDN通过智能技术生成

 首先由一个未加载的XAML类,需要把这个文件放到bin/debug文件夹下要不会找不到,或者传递参数带上路径,下面的例子是使用相对路径传的参数例如:

<DockPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Button Name="button1" Margin="60">Please click me.</Button>    
</DockPanel>

Mainwindow中

<Window x:Class="WpfApplication3.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:WpfApplication3"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        
    </Grid>
</Window>

把WPF自带的APP.XAML 删除了,新创建一个Progream类引导启动

Progream.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApplication3
{
    class Progream : Application
    {
        [STAThread()]
        static void Main()
        {
            Progream app = new Progream();
            app.MainWindow = new MainWindow("../../123/Window1.xaml");
            app.MainWindow.ShowDialog();
        }
    }
}

MainWindow.xaml.cs 重写一个MainWindow具体代码:

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;
using System.IO;
using System.Windows.Markup;

namespace WpfApplication3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Button myButton;
        public MainWindow()
        {
            InitializeComponent();
        }
        public MainWindow(string xamlFile)
        {
            //设置窗体
            this.Width = this.Height = 285;
            this.Left = this.Top = 100;
            this.Title = "Dynamically Loaded XAML";

            //从一个XAML文件获取XAML内容
            DependencyObject rootElement;
            using (FileStream fs = new FileStream(xamlFile, FileMode.Open))
            {
                rootElement = (DependencyObject)XamlReader.Load(fs);
            }
            this.Content = rootElement;
            myButton = (Button)LogicalTreeHelper.FindLogicalNode(rootElement, "button1");
            myButton.Click += myButton_Click;
        }
        private void myButton_Click(object sender, RoutedEventArgs e)
        {
            myButton.Content = "Thank you";
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

波雅_汉库克

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值