回答編集履歴

4

見直しキャンペーン中

2023/07/17 04:34

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,323 +1,164 @@
1
1
  アプリケーション全体で切り替えてよければ、
2
-
3
2
  `this.Resources.MergedDictionaries.Add(dictionary);//←この部分`を
4
-
5
3
  `Application.Current.Resources.MergedDictionaries.Add(dictionary);`に
6
-
7
4
  するのが簡単でしょうか。
8
-
9
-
10
5
 
11
6
  ---
12
7
 
8
+ 追記
13
- 追記 メインウィンドウと言語設定ダイアログがあると思い、こんな感じになりましたがどうでしょうか。
9
+ メインウィンドウと言語設定ダイアログがあると思い、こんな感じになりましたがどうでしょうか。
14
10
 
15
11
 
16
-
17
- MainWindow.xaml
12
+ ```xml:MainWindow.xaml
18
-
19
- ```xaml
20
-
21
13
  <Window
22
-
23
14
  x:Class="Questions._224205.MainWindow"
24
-
25
15
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
26
-
27
16
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
28
-
29
17
  Title="MainWindow"
30
-
31
18
  Width="800"
32
-
33
19
  Height="450">
34
-
35
20
  <StackPanel>
36
-
37
21
  <Label Content="{DynamicResource language}" />
38
-
39
22
  <Button Click="Button_Click" Content="{DynamicResource setting}" />
40
-
41
23
  </StackPanel>
42
-
43
24
  </Window>
44
-
45
25
  ```
46
26
 
47
- MainWindow.xaml.cs
27
+ ```cs:MainWindow.xaml.cs
48
-
49
- ```cs
50
-
51
28
  using System;
52
-
53
29
  using System.Windows;
54
30
 
31
+ namespace Questions._224205
32
+ {
33
+ public partial class MainWindow : Window
34
+ {
35
+ // 何か設定ファイルから読んだとして
36
+ internal static string NowLanguage = "ja-JP";
37
+ public MainWindow()
38
+ {
39
+ InitializeComponent();
40
+ SetLanguage(NowLanguage);
41
+ }
42
+ private void Button_Click(object sender, RoutedEventArgs e)
43
+ => new Language().ShowDialog();
55
44
 
45
+ public static void SetLanguage(string cultureCode)
46
+ {
47
+ var dictionary = new ResourceDictionary
48
+ {
49
+ Source = new Uri(@"Resources/Language/StringResource." + cultureCode + @".xaml", UriKind.Relative)
50
+ };
51
+ Application.Current.Resources.MergedDictionaries.Clear();
52
+ Application.Current.Resources.MergedDictionaries.Add(dictionary);
53
+ }
54
+ }
55
+ }
56
+ ```
57
+
58
+ ```xml:Language.xaml
59
+ <Window
60
+ x:Class="Questions._224205.Language"
61
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
62
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
63
+ Title="{DynamicResource setting}"
64
+ Width="800"
65
+ Height="450"
66
+ WindowStartupLocation="CenterScreen">
67
+ <StackPanel>
68
+ <TextBlock x:Name="TestTextBox" Text="{DynamicResource language}" />
69
+ <RadioButton
70
+ x:Name="radioButton_ENG"
71
+ VerticalContentAlignment="Center"
72
+ Checked="RadioButtonENG_Checked"
73
+ Content="English" />
74
+ <RadioButton
75
+ x:Name="radioButton_JPA"
76
+ VerticalContentAlignment="Center"
77
+ Checked="RadioButtonJPA_Checked"
78
+ Content="日本語" />
79
+ </StackPanel>
80
+ </Window>
81
+ ```
82
+
83
+ ```cs:Language.xaml.cs
84
+ using System.Windows;
56
85
 
57
86
  namespace Questions._224205
58
-
59
87
  {
60
-
61
- public partial class MainWindow : Window
88
+ public partial class Language : Window
62
-
63
89
  {
64
-
65
- // 何か設定ファイルから読んだとして
66
-
67
- internal static string NowLanguage = "ja-JP";
68
-
69
- public MainWindow()
90
+ public Language()
70
-
71
91
  {
72
-
73
92
  InitializeComponent();
74
93
 
94
+ // 雑すぎますが本題ではないので。。
75
- SetLanguage(NowLanguage);
95
+ switch(MainWindow.NowLanguage)
76
-
96
+ {
97
+ case "en-US":
98
+ radioButton_ENG.IsChecked = true;
99
+ break;
100
+ case "ja-JP":
101
+ radioButton_JPA.IsChecked = true;
102
+ break;
103
+ }
77
104
  }
78
105
 
79
- private void Button_Click(object sender, RoutedEventArgs e)
106
+ private void RadioButtonENG_Checked(object sender, RoutedEventArgs e)
107
+ // 雑すぎますが本題ではないので。。
108
+ => MainWindow.SetLanguage("en-US");
80
109
 
110
+ private void RadioButtonJPA_Checked(object sender, RoutedEventArgs e)
81
- => new Language().ShowDialog();
111
+ => MainWindow.SetLanguage("ja-JP");
82
-
83
-
84
-
85
- public static void SetLanguage(string cultureCode)
86
-
87
- {
88
-
89
- var dictionary = new ResourceDictionary
90
-
91
- {
92
-
93
- Source = new Uri(@"Resources/Language/StringResource." + cultureCode + @".xaml", UriKind.Relative)
94
-
95
- };
96
-
97
- Application.Current.Resources.MergedDictionaries.Clear();
98
-
99
- Application.Current.Resources.MergedDictionaries.Add(dictionary);
100
-
101
- }
102
-
103
112
  }
104
-
105
113
  }
106
-
107
114
  ```
108
115
 
109
- Language.xaml
116
+ ```xml:StringResource.en-us.xaml
110
-
111
- ```xaml
112
-
113
- <Window
117
+ <ResourceDictionary
114
-
115
- x:Class="Questions._224205.Language"
116
-
117
118
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
118
-
119
119
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
120
-
121
- Title="{DynamicResource setting}"
122
-
123
- Width="800"
124
-
125
- Height="450"
126
-
127
- WindowStartupLocation="CenterScreen">
120
+ xmlns:system="clr-namespace:System;assembly=mscorlib">
128
-
129
- <StackPanel>
121
+ <system:String x:Key="language">Language</system:String>
130
-
131
- <TextBlock x:Name="TestTextBox" Text="{DynamicResource language}" />
122
+ <system:String x:Key="setting">setting</system:String>
132
-
133
- <RadioButton
134
-
135
- x:Name="radioButton_ENG"
136
-
137
- VerticalContentAlignment="Center"
138
-
139
- Checked="RadioButtonENG_Checked"
140
-
141
- Content="English" />
142
-
143
- <RadioButton
144
-
145
- x:Name="radioButton_JPA"
146
-
147
- VerticalContentAlignment="Center"
148
-
149
- Checked="RadioButtonJPA_Checked"
150
-
151
- Content="日本語" />
152
-
153
- </StackPanel>
123
+ </ResourceDictionary>
154
-
155
- </Window>
156
-
157
124
  ```
158
125
 
159
- Language.xaml.cs
126
+ ```xml:StringResource.ja-jp.xaml
160
-
161
- ```cs
162
-
163
- using System.Windows;
164
-
165
-
166
-
167
- namespace Questions._224205
127
+ <ResourceDictionary
168
-
128
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
169
- {
129
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
170
-
171
- public partial class Language : Window
130
+ xmlns:system="clr-namespace:System;assembly=mscorlib">
172
-
173
- {
174
-
175
- public Language()
131
+ <system:String x:Key="language">言語</system:String>
176
-
177
- {
178
-
179
- InitializeComponent();
132
+ <system:String x:Key="setting">設定</system:String>
180
-
181
-
182
-
183
- // 雑すぎますが本題ではないので。。
133
+ </ResourceDictionary>
184
-
185
- switch(MainWindow.NowLanguage)
186
-
187
- {
188
-
189
- case "en-US":
190
-
191
- radioButton_ENG.IsChecked = true;
192
-
193
- break;
194
-
195
- case "ja-JP":
196
-
197
- radioButton_JPA.IsChecked = true;
198
-
199
- break;
200
-
201
- }
202
-
203
- }
204
-
205
-
206
-
207
- private void RadioButtonENG_Checked(object sender, RoutedEventArgs e)
208
-
209
- // 雑すぎますが本題ではないので。。
210
-
211
- => MainWindow.SetLanguage("en-US");
212
-
213
-
214
-
215
- private void RadioButtonJPA_Checked(object sender, RoutedEventArgs e)
216
-
217
- => MainWindow.SetLanguage("ja-JP");
218
-
219
- }
220
-
221
- }
222
-
223
134
  ```
224
-
225
- StringResource.en-us.xaml
226
-
227
- ```xaml
228
-
229
- <ResourceDictionary
230
-
231
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
232
-
233
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
234
-
235
- xmlns:system="clr-namespace:System;assembly=mscorlib">
236
-
237
- <system:String x:Key="language">Language</system:String>
238
-
239
- <system:String x:Key="setting">setting</system:String>
240
-
241
- </ResourceDictionary>
242
-
243
- ```
244
-
245
- StringResource.ja-jp.xaml
246
-
247
- ```xaml
248
-
249
- <ResourceDictionary
250
-
251
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
252
-
253
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
254
-
255
- xmlns:system="clr-namespace:System;assembly=mscorlib">
256
-
257
- <system:String x:Key="language">言語</system:String>
258
-
259
- <system:String x:Key="setting">設定</system:String>
260
-
261
- </ResourceDictionary>
262
-
263
- ```
264
-
265
135
  ![イメージ説明](5f42c41892cb0664d8a1ed9db1bf37e2.png)
266
-
267
-
268
136
 
269
137
  ---
270
138
 
271
139
  追記 画像の場合
272
140
 
273
- StringResource.ja-jp.xaml
141
+ ```xml:StringResource.ja-jp.xaml
274
-
275
- ```xaml
276
-
277
142
  <ResourceDictionary
278
-
279
143
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
280
-
281
144
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
282
-
283
145
  xmlns:system="clr-namespace:System;assembly=mscorlib">
284
-
285
146
  <BitmapImage x:Key="ButtonImage" UriSource="/Questions.224205;component/Resources/Image/ButtonImage_jpa.png" />
286
-
287
147
  </ResourceDictionary>
288
-
289
148
  ```
290
-
291
149
  `Questions.224205`の部分はアセンブリ名(マウスで簡単に指定できます)
292
-
293
150
  ![イメージ説明](9b69bdf8168d3322fc2369c84cd24e14.png)
294
151
 
295
-
296
-
297
- MainWindow.xaml
152
+ ```xml:MainWindow.xaml
298
-
299
- ```xaml
300
-
301
153
  <Window
302
-
303
154
  x:Class="Questions._224205.MainWindow"
304
-
305
155
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
306
-
307
156
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
308
-
309
157
  Title="MainWindow"
310
-
311
158
  Width="800"
312
-
313
159
  Height="450">
314
-
315
160
  <StackPanel>
316
-
317
161
  <Image Source="{DynamicResource ButtonImage}" />
318
-
319
162
  </StackPanel>
320
-
321
163
  </Window>
322
-
323
164
  ```

3

コード修正

2019/11/20 04:38

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -94,6 +94,8 @@
94
94
 
95
95
  };
96
96
 
97
+ Application.Current.Resources.MergedDictionaries.Clear();
98
+
97
99
  Application.Current.Resources.MergedDictionaries.Add(dictionary);
98
100
 
99
101
  }

2

追記 画像の場合

2019/11/20 04:38

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -261,3 +261,61 @@
261
261
  ```
262
262
 
263
263
  ![イメージ説明](5f42c41892cb0664d8a1ed9db1bf37e2.png)
264
+
265
+
266
+
267
+ ---
268
+
269
+ 追記 画像の場合
270
+
271
+ StringResource.ja-jp.xaml
272
+
273
+ ```xaml
274
+
275
+ <ResourceDictionary
276
+
277
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
278
+
279
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
280
+
281
+ xmlns:system="clr-namespace:System;assembly=mscorlib">
282
+
283
+ <BitmapImage x:Key="ButtonImage" UriSource="/Questions.224205;component/Resources/Image/ButtonImage_jpa.png" />
284
+
285
+ </ResourceDictionary>
286
+
287
+ ```
288
+
289
+ `Questions.224205`の部分はアセンブリ名(マウスで簡単に指定できます)
290
+
291
+ ![イメージ説明](9b69bdf8168d3322fc2369c84cd24e14.png)
292
+
293
+
294
+
295
+ MainWindow.xaml
296
+
297
+ ```xaml
298
+
299
+ <Window
300
+
301
+ x:Class="Questions._224205.MainWindow"
302
+
303
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
304
+
305
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
306
+
307
+ Title="MainWindow"
308
+
309
+ Width="800"
310
+
311
+ Height="450">
312
+
313
+ <StackPanel>
314
+
315
+ <Image Source="{DynamicResource ButtonImage}" />
316
+
317
+ </StackPanel>
318
+
319
+ </Window>
320
+
321
+ ```

1

コード追記

2019/11/20 02:52

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -5,3 +5,259 @@
5
5
  `Application.Current.Resources.MergedDictionaries.Add(dictionary);`に
6
6
 
7
7
  するのが簡単でしょうか。
8
+
9
+
10
+
11
+ ---
12
+
13
+ 追記 メインウィンドウと言語設定ダイアログがあると思い、こんな感じになりましたがどうでしょうか。
14
+
15
+
16
+
17
+ MainWindow.xaml
18
+
19
+ ```xaml
20
+
21
+ <Window
22
+
23
+ x:Class="Questions._224205.MainWindow"
24
+
25
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
26
+
27
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
28
+
29
+ Title="MainWindow"
30
+
31
+ Width="800"
32
+
33
+ Height="450">
34
+
35
+ <StackPanel>
36
+
37
+ <Label Content="{DynamicResource language}" />
38
+
39
+ <Button Click="Button_Click" Content="{DynamicResource setting}" />
40
+
41
+ </StackPanel>
42
+
43
+ </Window>
44
+
45
+ ```
46
+
47
+ MainWindow.xaml.cs
48
+
49
+ ```cs
50
+
51
+ using System;
52
+
53
+ using System.Windows;
54
+
55
+
56
+
57
+ namespace Questions._224205
58
+
59
+ {
60
+
61
+ public partial class MainWindow : Window
62
+
63
+ {
64
+
65
+ // 何か設定ファイルから読んだとして
66
+
67
+ internal static string NowLanguage = "ja-JP";
68
+
69
+ public MainWindow()
70
+
71
+ {
72
+
73
+ InitializeComponent();
74
+
75
+ SetLanguage(NowLanguage);
76
+
77
+ }
78
+
79
+ private void Button_Click(object sender, RoutedEventArgs e)
80
+
81
+ => new Language().ShowDialog();
82
+
83
+
84
+
85
+ public static void SetLanguage(string cultureCode)
86
+
87
+ {
88
+
89
+ var dictionary = new ResourceDictionary
90
+
91
+ {
92
+
93
+ Source = new Uri(@"Resources/Language/StringResource." + cultureCode + @".xaml", UriKind.Relative)
94
+
95
+ };
96
+
97
+ Application.Current.Resources.MergedDictionaries.Add(dictionary);
98
+
99
+ }
100
+
101
+ }
102
+
103
+ }
104
+
105
+ ```
106
+
107
+ Language.xaml
108
+
109
+ ```xaml
110
+
111
+ <Window
112
+
113
+ x:Class="Questions._224205.Language"
114
+
115
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
116
+
117
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
118
+
119
+ Title="{DynamicResource setting}"
120
+
121
+ Width="800"
122
+
123
+ Height="450"
124
+
125
+ WindowStartupLocation="CenterScreen">
126
+
127
+ <StackPanel>
128
+
129
+ <TextBlock x:Name="TestTextBox" Text="{DynamicResource language}" />
130
+
131
+ <RadioButton
132
+
133
+ x:Name="radioButton_ENG"
134
+
135
+ VerticalContentAlignment="Center"
136
+
137
+ Checked="RadioButtonENG_Checked"
138
+
139
+ Content="English" />
140
+
141
+ <RadioButton
142
+
143
+ x:Name="radioButton_JPA"
144
+
145
+ VerticalContentAlignment="Center"
146
+
147
+ Checked="RadioButtonJPA_Checked"
148
+
149
+ Content="日本語" />
150
+
151
+ </StackPanel>
152
+
153
+ </Window>
154
+
155
+ ```
156
+
157
+ Language.xaml.cs
158
+
159
+ ```cs
160
+
161
+ using System.Windows;
162
+
163
+
164
+
165
+ namespace Questions._224205
166
+
167
+ {
168
+
169
+ public partial class Language : Window
170
+
171
+ {
172
+
173
+ public Language()
174
+
175
+ {
176
+
177
+ InitializeComponent();
178
+
179
+
180
+
181
+ // 雑すぎますが本題ではないので。。
182
+
183
+ switch(MainWindow.NowLanguage)
184
+
185
+ {
186
+
187
+ case "en-US":
188
+
189
+ radioButton_ENG.IsChecked = true;
190
+
191
+ break;
192
+
193
+ case "ja-JP":
194
+
195
+ radioButton_JPA.IsChecked = true;
196
+
197
+ break;
198
+
199
+ }
200
+
201
+ }
202
+
203
+
204
+
205
+ private void RadioButtonENG_Checked(object sender, RoutedEventArgs e)
206
+
207
+ // 雑すぎますが本題ではないので。。
208
+
209
+ => MainWindow.SetLanguage("en-US");
210
+
211
+
212
+
213
+ private void RadioButtonJPA_Checked(object sender, RoutedEventArgs e)
214
+
215
+ => MainWindow.SetLanguage("ja-JP");
216
+
217
+ }
218
+
219
+ }
220
+
221
+ ```
222
+
223
+ StringResource.en-us.xaml
224
+
225
+ ```xaml
226
+
227
+ <ResourceDictionary
228
+
229
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
230
+
231
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
232
+
233
+ xmlns:system="clr-namespace:System;assembly=mscorlib">
234
+
235
+ <system:String x:Key="language">Language</system:String>
236
+
237
+ <system:String x:Key="setting">setting</system:String>
238
+
239
+ </ResourceDictionary>
240
+
241
+ ```
242
+
243
+ StringResource.ja-jp.xaml
244
+
245
+ ```xaml
246
+
247
+ <ResourceDictionary
248
+
249
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
250
+
251
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
252
+
253
+ xmlns:system="clr-namespace:System;assembly=mscorlib">
254
+
255
+ <system:String x:Key="language">言語</system:String>
256
+
257
+ <system:String x:Key="setting">設定</system:String>
258
+
259
+ </ResourceDictionary>
260
+
261
+ ```
262
+
263
+ ![イメージ説明](5f42c41892cb0664d8a1ed9db1bf37e2.png)