• WPF 的另类资源方式 Resources.resx


    类似Winform的搞法,可以把资源放到Resources.resx中。

    1.字符串

    打开这个编辑器后,输入Name和Value就可以了。

    CS代码里面,很简单的调用:

    var title = WpfResource2.Properties.Resources.IDS_TEST_TITLE;

    如果要用在XAML中,需要把Access Modifier改为public,原来是Internal。

    XAML如下:

    1 <Window x:Class="WpfResource2.MainWindow"
    2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4         xmlns:res="clr-namespace:WpfResource2.Properties"
    5         Title="MainWindow" Height="350" Width="525">
    6   <Grid>
    7     <TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Text="{x:Static res:Resources.IDS_TEST_TITLE}">
    8 
    9     </TextBlock>

    2. 图片资源

    放入现有的文件

    这个不能在XAML里面直接用了,cs代码里面:

       var testImg = WpfResource2.Properties.Resources.IC371904;
                MemoryStream memory = new MemoryStream();
                testImg.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
                ImageSourceConverter converter = new ImageSourceConverter();
                ImageSource source = (ImageSource)converter.ConvertFrom(memory);
                img.Source = source;

    3. 文件,我尝试加入了一个文本,好像默认的文件格式也是文本,居然读出来string了

    var content = WpfResource2.Properties.Resources.test;

    加入了一个其他格式的文件,读取到的是byte[]

    var content = WpfResource2.Properties.Resources.New_Microsoft_Excel_Worksheet;

    4. 还有其他类型,就不在一一实验了。

    不知道MUI,能否通过这种方式来搞。

     Demo

  • 相关阅读:
    JS伪3D 图形透视效果
    源码安装apache及配置转发
    SpringSecutiry权限管理手册
    解决SMARTFORMS 中table 控件单行跨页的问题
    Cluster Table
    uva-133 The Dole Queue
    第三届蓝桥杯C++本科B组决赛解题报告(更新中)
    uva-673 Parentheses Balance
    VS2010不能编译SQLServer2005的Microsoft.SQLServer.ManagedDTS.dll的解决方法
    IOS设计模式学习(21)享元
  • 原文地址:https://www.cnblogs.com/xiaokang088/p/2567473.html
Copyright © 2020-2023  润新知