WPF 读取xaml文档

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private Button myButton;
        public MainWindow()
        {
            InitializeComponent();
        }
        public MainWindow(string xamlFile)//重写MainWindow,并传递一个参数(xmal路径)
        {
            this.Width = this.Height = 285;
            this.Top = this.Left = 100;
            this.Title = "Dynamically Loaded XAML.";
            //从一个XAML文件里获取XAML内容
            DependencyObject rootElement;//定义依赖对象
            using (FileStream fs = new FileStream(xamlFile, FileMode.Open))
            {
                rootElement = (DependencyObject)XamlReader.Load(fs);//将读到的xaml内容赋值给rootElement
            }
            this.Content = rootElement;
            myButton = (Button)LogicalTreeHelper.FindLogicalNode(rootElement, "button1");//利用LogicalTreeHelper.FindLogicalNode(rootElement, "button1")从rootElement中找到button1
            myButton.Click+=myButton_Click;
        }
        private void myButton_Click(object sender, RoutedEventArgs e)//button点击事件
        {
            myButton.Content = "Thank you.";
        }
    }
    //重新定义启动程序,将原来的app.cs删除
    class Program:Application
    {
        [STAThread]
        static void Main()
        {
            Program app = new Program();
            app.MainWindow = new MainWindow("Window1.xaml");//将window1.xaml放到debug目录下,可以直接读取
            app.MainWindow.ShowDialog();
        }
    }
    //window1中的内容
    <DockPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Button Name="button1" Margin="60">Please click me.</Button>
    </DockPanel>


WPF (Windows Presentation Foundation) 的 DocumentViewer 控件是 .NET Framework 中用于显示各种文档格式,包括Microsoft Word (.doc, .docx) 文件的一个控件。你可以通过以下几个步骤在 WPF 应用程序中使用 DocumentViewer 来读取 Word 文档: 1. 引入依赖项:首先,在 XAML 中添加 `xmlns` 声明以便使用 `DocumentViewer` 控件,并确保已安装 `System.Windows.Controls.DataVisualization.Toolkit` 或 `Microsoft.Office.Interop.Word`(如果需要读取 Word 内容)。 ```xml <Window x:Class="YourNamespace.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" mc:Ignorable="d" Title="MainWindow"> <Grid> <DocumentViewer Height="400" Width="800"> <!-- 这里会放加载Word文档的部分 --> </DocumentViewer> </Grid> </Window> ``` 2. 设置内容源:在 C# 或 VB.NET 的代码背后,你需要实例化 `DocumentViewModel` 或者直接设置 `DocumentViewer.Document` 属性。如果你使用 `Microsoft.Office.Interop.Word`,可以这样做: ```csharp private void Window_Loaded(object sender, RoutedEventArgs e) { // 加载Word文档 Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open("path_to_your_word_file.docx"); DocumentViewer.Document = doc; } ``` 3. 关闭 Word 应用:记得在不需要时关闭 `Application` 对象以释放资源
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值