{"id":1713,"date":"2023-12-12T14:19:39","date_gmt":"2023-12-12T06:19:39","guid":{"rendered":"https:\/\/sanlangcode.com\/?p=1713"},"modified":"2023-12-12T14:19:39","modified_gmt":"2023-12-12T06:19:39","slug":"wpf%e5%ba%94%e7%94%a8%e5%ae%9e%e7%8e%b0%e4%b8%ad%e8%8b%b1%e6%96%87%e9%85%8d%e7%bd%ae%e4%b8%8e%e8%b5%84%e6%ba%90%e8%ae%be%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/sanlangcode.com\/index.php\/2023\/12\/12\/wpf%e5%ba%94%e7%94%a8%e5%ae%9e%e7%8e%b0%e4%b8%ad%e8%8b%b1%e6%96%87%e9%85%8d%e7%bd%ae%e4%b8%8e%e8%b5%84%e6%ba%90%e8%ae%be%e7%bd%ae\/","title":{"rendered":"WPF\u5e94\u7528\u5b9e\u73b0\u4e2d\u82f1\u6587\u914d\u7f6e\u4e0e\u8d44\u6e90\u8bbe\u7f6e"},"content":{"rendered":"\n<p>\u5728WPF\u5e94\u7528\u4e2d\u5b9e\u73b0\u4e2d\u82f1\u6587\u914d\u7f6e\u4e0e\u8d44\u6e90\u8bbe\u7f6e\u901a\u5e38\u6d89\u53ca\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>\u521b\u5efa\u8d44\u6e90\u6587\u4ef6<\/strong>\uff1a<ul><li>\u5728\u9879\u76ee\u4e2d\u521b\u5efa\u8d44\u6e90\u6587\u4ef6\uff08.resx\uff09\uff0c\u5206\u522b\u521b\u5efa\u4e00\u4e2a\u7528\u4e8e\u4e2d\u6587\u548c\u4e00\u4e2a\u7528\u4e8e\u82f1\u6587\u3002<\/li><li>\u547d\u540d\u65b9\u5f0f\u53ef\u4ee5\u662f<code>Resources.zh-CN.resx<\/code>\u548c<code>Resources.en-US.resx<\/code>\u3002<\/li><\/ul><\/li><li><strong>\u6dfb\u52a0\u8d44\u6e90\u9879<\/strong>\uff1a<ul><li>\u5728\u8d44\u6e90\u6587\u4ef6\u4e2d\u6dfb\u52a0\u9700\u8981\u56fd\u9645\u5316\u7684\u5b57\u7b26\u4e32\uff0c\u6bcf\u4e2a\u5b57\u7b26\u4e32\u5bf9\u5e94\u4e00\u4e2a\u952e\u503c\u5bf9\u3002<\/li><li>\u4f8b\u5982\uff0c\u5728<code>Resources.zh-CN.resx<\/code>\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9\uff1a&#8221;Hello&#8221; -&gt; &#8220;\u4f60\u597d&#8221;\uff0c\u800c\u5728<code>Resources.en-US.resx<\/code>\u4e2d\u6dfb\u52a0\uff1a&#8221;Hello&#8221; -&gt; &#8220;Hello&#8221;\u3002<\/li><\/ul><\/li><li><strong>\u8bbe\u7f6eLocalizable\u5c5e\u6027<\/strong>\uff1a<ul><li>\u5bf9\u4e8e\u6bcf\u4e2a\u7a97\u53e3\u6216\u7528\u6237\u63a7\u4ef6\uff0c\u8bbe\u7f6e <code>Localizable<\/code> \u5c5e\u6027\u4e3a <code>True<\/code>\u3002<\/li><li>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a\u5bf9\u5e94\u4e8e\u6bcf\u79cd\u8bed\u8a00\u7684 .resx \u6587\u4ef6\u3002<\/li><\/ul><\/li><li><strong>\u8bbe\u7f6e\u8bed\u8a00\u5207\u6362\u903b\u8f91<\/strong>\uff1a<ul><li>\u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\uff0c\u68c0\u6d4b\u5f53\u524d\u7cfb\u7edf\u7684\u8bed\u8a00\u8bbe\u7f6e\u3002<\/li><li>\u53ef\u4ee5\u901a\u8fc7<code>System.Threading.Thread.CurrentThread.CurrentCulture<\/code>\u548c<code>System.Threading.Thread.CurrentThread.CurrentUICulture<\/code>\u6765\u83b7\u53d6\u5f53\u524d\u7684\u6587\u5316\u4fe1\u606f\u3002<\/li><li>\u6839\u636e\u6587\u5316\u4fe1\u606f\u52a0\u8f7d\u5bf9\u5e94\u7684\u8d44\u6e90\u6587\u4ef6\u3002<\/li><\/ul><\/li><li><strong>\u52a8\u6001\u7ed1\u5b9a\u8d44\u6e90<\/strong>\uff1a<ul><li>\u5728 XAML \u4e2d\u4f7f\u7528\u52a8\u6001\u7ed1\u5b9a\uff0c\u4ee5\u4fbf\u5728\u8fd0\u884c\u65f6\u6839\u636e\u5f53\u524d\u6587\u5316\u52a0\u8f7d\u76f8\u5e94\u7684\u8d44\u6e90\u3002<\/li><li>\u4f8b\u5982\uff0c\u4f7f\u7528<code>{Binding Path=Resources.Hello, Source={x:Static my:Resources.Instance}}<\/code>\uff0c\u5176\u4e2d<code>my<\/code>\u662f\u4f60\u7684\u9879\u76ee\u7684\u547d\u540d\u7a7a\u95f4\u3002<\/li><\/ul><\/li><li><strong>\u521b\u5efa\u8d44\u6e90\u7ba1\u7406\u7c7b<\/strong>\uff08\u53ef\u9009\uff09\uff1a<ul><li>\u4e3a\u4e86\u66f4\u597d\u5730\u7ba1\u7406\u8d44\u6e90\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u8d44\u6e90\u7ba1\u7406\u7c7b\uff0c\u8d1f\u8d23\u52a0\u8f7d\u548c\u83b7\u53d6\u5f53\u524d\u8bed\u8a00\u4e0b\u7684\u8d44\u6e90\u3002<\/li><li>\u8fd9\u4e2a\u7c7b\u53ef\u80fd\u4f1a\u5305\u542b\u4e00\u4e2a\u5b57\u5178\u6216\u7c7b\u4f3c\u7684\u7ed3\u6784\uff0c\u7528\u4e8e\u5b58\u50a8\u6240\u6709\u652f\u6301\u7684\u8bed\u8a00\u7684\u8d44\u6e90\u3002<\/li><\/ul><\/li><li><strong>\u5b9e\u73b0\u8bed\u8a00\u5207\u6362\u529f\u80fd<\/strong>\uff1a<ul><li>\u63d0\u4f9b\u7528\u6237\u754c\u9762\u6216\u8bbe\u7f6e\u9009\u9879\uff0c\u5141\u8bb8\u7528\u6237\u5728\u8fd0\u884c\u65f6\u5207\u6362\u5e94\u7528\u7a0b\u5e8f\u7684\u8bed\u8a00\u3002<\/li><li>\u5728\u8bed\u8a00\u5207\u6362\u65f6\u66f4\u65b0<code>CurrentCulture<\/code>\u548c<code>CurrentUICulture<\/code>\uff0c\u5e76\u91cd\u65b0\u52a0\u8f7d\u754c\u9762\u3002<\/li><\/ul><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Globalization;\nusing System.Threading;\nusing System.Windows;\n\nnamespace WpfLocalizationExample\n{\n    public partial class App : Application\n    {\n        protected override void OnStartup(StartupEventArgs e)\n        {\n            \/\/ \u83b7\u53d6\u7cfb\u7edf\u9ed8\u8ba4\u8bed\u8a00\n            CultureInfo culture = CultureInfo.CurrentCulture;\n\n            \/\/ \u8bbe\u7f6e\u5e94\u7528\u7a0b\u5e8f\u7684\u8bed\u8a00\n            SetLanguage(culture);\n\n            base.OnStartup(e);\n        }\n\n        public static void SetLanguage(CultureInfo culture)\n        {\n            \/\/ \u8bbe\u7f6e\u5f53\u524d\u7ebf\u7a0b\u7684\u8bed\u8a00\n            Thread.CurrentThread.CurrentCulture = culture;\n            Thread.CurrentThread.CurrentUICulture = culture;\n\n            \/\/ \u91cd\u65b0\u52a0\u8f7d\u754c\u9762\n            MainWindow mainWindow = new MainWindow();\n            mainWindow.Show();\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;TextBlock Text=\"{Binding Path=Resources.Hello, Source={x:Static my:Resources.Instance}}\" \/><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728WPF\u5e94\u7528\u4e2d\u5b9e\u73b0\u4e2d\u82f1\u6587\u914d\u7f6e\u4e0e\u8d44&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[],"class_list":["post-1713","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts\/1713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/comments?post=1713"}],"version-history":[{"count":0,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts\/1713\/revisions"}],"wp:attachment":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/media?parent=1713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/categories?post=1713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/tags?post=1713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}