日常生活的交流与学习

首页 新随笔 联系 管理

bmal文件读取

运行效果

Demo01\Demo01\MainWindow.xaml.cs

using System.Collections;
using System.Globalization;
using System.IO;
using System.Resources;
using System.Text;
using System.Windows;
using System.Windows.Baml2006;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Demo01
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // 获取当前类型的程序集名称
            var resourcesName = this.GetType().Assembly.GetName().Name + ".g";
            // 创建ResourceManager实例
            var manager = new ResourceManager(resourcesName, this.GetType().Assembly);
            // 获取资源集
            var resourceSet = manager.GetResourceSet(CultureInfo.CurrentCulture, true, true);
            // 将资源集中的DictionaryEntry类型转换为列表
            var dictionaryEntries = resourceSet.OfType<DictionaryEntry>().ToList();
            // 遍历所有字典条目
            dictionaryEntries.ForEach(arg =>
            {
                // 使用Baml2006Reader读取BAML数据流
                var reader = new Baml2006Reader((Stream)arg.Value);

                // 使用XamlReader将BAML数据转换为XAML对象
                var win = XamlReader.Load(reader) as Window;

                // 输出窗口的名称
                Console.WriteLine(win.Name);
                MessageBox.Show(win.Name);

            });
        }
    }
}

Demo01\Demo01\MainWindow.xaml

<Window x:Class="Demo01.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:Demo01"
        mc:Ignorable="d"
        Name="theses_words_should_be_read_from_bmal"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button Width="100" Height="100" Content="Click" Click="Button_Click"></Button>

    </Grid>
</Window>

posted on 2024-10-26 21:06  lazycookie  阅读(7)  评论(0编辑  收藏  举报