WPF 访问 Resources.resx 资源文件

1. 添加 Properties 命名空间

2.Resources.resx 权限改为public

 

 

<Window x:Class="WpfDemo.ResourceDemo"
        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:WpfDemo"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:prop="clr-namespace:WpfDemo.Properties"
        mc:Ignorable="d"
        Title="ResourceDemo" Height="300" Width="300"
        >
    <Window.Resources>
    </Window.Resources>
    <Grid>
        <Grid.Resources>
        </Grid.Resources>
        <TextBlock x:Name="textBlock1" HorizontalAlignment="Left" Margin="40,144,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Height="32" Width="115"/>

    </Grid>
</Window>

  

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.Shapes;

namespace WpfDemo
{
    /// <summary>
    /// ResourceDemo.xaml 的互動邏輯
    /// </summary>
    public partial class ResourceDemo : Window
    {
        public ResourceDemo()
        {
            InitializeComponent();
           

            // 通過前台 後台 方式 綁定 Resources.resx 資源

            //Text = "{x:Static prop:Resources.mystring}"

            this.textBlock1.Text = Properties.Resources.mystring;
        }
    }
}

  

 

posted @ 2021-08-22 23:54  zq爱生活爱代码  阅读(752)  评论(0编辑  收藏  举报