回答編集履歴
4
見直しキャンペーン中
answer
CHANGED
@@ -1,162 +1,164 @@
|
|
1
|
-
アプリケーション全体で切り替えてよければ、
|
2
|
-
`this.Resources.MergedDictionaries.Add(dictionary);//←この部分`を
|
3
|
-
`Application.Current.Resources.MergedDictionaries.Add(dictionary);`に
|
4
|
-
するのが簡単でしょうか。
|
5
|
-
|
6
|
-
---
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
</
|
23
|
-
|
24
|
-
|
25
|
-
```
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
}
|
54
|
-
|
55
|
-
|
56
|
-
```
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
```
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
```
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
xmlns
|
119
|
-
|
120
|
-
|
121
|
-
</
|
122
|
-
|
123
|
-
|
124
|
-
```
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
xmlns
|
129
|
-
|
130
|
-
|
131
|
-
</
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
<
|
161
|
-
</
|
1
|
+
アプリケーション全体で切り替えてよければ、
|
2
|
+
`this.Resources.MergedDictionaries.Add(dictionary);//←この部分`を
|
3
|
+
`Application.Current.Resources.MergedDictionaries.Add(dictionary);`に
|
4
|
+
するのが簡単でしょうか。
|
5
|
+
|
6
|
+
---
|
7
|
+
|
8
|
+
追記
|
9
|
+
メインウィンドウと言語設定ダイアログがあると思い、こんな感じになりましたがどうでしょうか。
|
10
|
+
|
11
|
+
|
12
|
+
```xml:MainWindow.xaml
|
13
|
+
<Window
|
14
|
+
x:Class="Questions._224205.MainWindow"
|
15
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
16
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
17
|
+
Title="MainWindow"
|
18
|
+
Width="800"
|
19
|
+
Height="450">
|
20
|
+
<StackPanel>
|
21
|
+
<Label Content="{DynamicResource language}" />
|
22
|
+
<Button Click="Button_Click" Content="{DynamicResource setting}" />
|
23
|
+
</StackPanel>
|
24
|
+
</Window>
|
25
|
+
```
|
26
|
+
|
27
|
+
```cs:MainWindow.xaml.cs
|
28
|
+
using System;
|
29
|
+
using System.Windows;
|
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();
|
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;
|
85
|
+
|
86
|
+
namespace Questions._224205
|
87
|
+
{
|
88
|
+
public partial class Language : Window
|
89
|
+
{
|
90
|
+
public Language()
|
91
|
+
{
|
92
|
+
InitializeComponent();
|
93
|
+
|
94
|
+
// 雑すぎますが本題ではないので。。
|
95
|
+
switch(MainWindow.NowLanguage)
|
96
|
+
{
|
97
|
+
case "en-US":
|
98
|
+
radioButton_ENG.IsChecked = true;
|
99
|
+
break;
|
100
|
+
case "ja-JP":
|
101
|
+
radioButton_JPA.IsChecked = true;
|
102
|
+
break;
|
103
|
+
}
|
104
|
+
}
|
105
|
+
|
106
|
+
private void RadioButtonENG_Checked(object sender, RoutedEventArgs e)
|
107
|
+
// 雑すぎますが本題ではないので。。
|
108
|
+
=> MainWindow.SetLanguage("en-US");
|
109
|
+
|
110
|
+
private void RadioButtonJPA_Checked(object sender, RoutedEventArgs e)
|
111
|
+
=> MainWindow.SetLanguage("ja-JP");
|
112
|
+
}
|
113
|
+
}
|
114
|
+
```
|
115
|
+
|
116
|
+
```xml:StringResource.en-us.xaml
|
117
|
+
<ResourceDictionary
|
118
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
119
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
120
|
+
xmlns:system="clr-namespace:System;assembly=mscorlib">
|
121
|
+
<system:String x:Key="language">Language</system:String>
|
122
|
+
<system:String x:Key="setting">setting</system:String>
|
123
|
+
</ResourceDictionary>
|
124
|
+
```
|
125
|
+
|
126
|
+
```xml:StringResource.ja-jp.xaml
|
127
|
+
<ResourceDictionary
|
128
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
129
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
130
|
+
xmlns:system="clr-namespace:System;assembly=mscorlib">
|
131
|
+
<system:String x:Key="language">言語</system:String>
|
132
|
+
<system:String x:Key="setting">設定</system:String>
|
133
|
+
</ResourceDictionary>
|
134
|
+
```
|
135
|
+

|
136
|
+
|
137
|
+
---
|
138
|
+
|
139
|
+
追記 画像の場合
|
140
|
+
|
141
|
+
```xml:StringResource.ja-jp.xaml
|
142
|
+
<ResourceDictionary
|
143
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
144
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
145
|
+
xmlns:system="clr-namespace:System;assembly=mscorlib">
|
146
|
+
<BitmapImage x:Key="ButtonImage" UriSource="/Questions.224205;component/Resources/Image/ButtonImage_jpa.png" />
|
147
|
+
</ResourceDictionary>
|
148
|
+
```
|
149
|
+
`Questions.224205`の部分はアセンブリ名(マウスで簡単に指定できます)
|
150
|
+

|
151
|
+
|
152
|
+
```xml:MainWindow.xaml
|
153
|
+
<Window
|
154
|
+
x:Class="Questions._224205.MainWindow"
|
155
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
156
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
157
|
+
Title="MainWindow"
|
158
|
+
Width="800"
|
159
|
+
Height="450">
|
160
|
+
<StackPanel>
|
161
|
+
<Image Source="{DynamicResource ButtonImage}" />
|
162
|
+
</StackPanel>
|
163
|
+
</Window>
|
162
164
|
```
|
3
コード修正
answer
CHANGED
@@ -46,6 +46,7 @@
|
|
46
46
|
{
|
47
47
|
Source = new Uri(@"Resources/Language/StringResource." + cultureCode + @".xaml", UriKind.Relative)
|
48
48
|
};
|
49
|
+
Application.Current.Resources.MergedDictionaries.Clear();
|
49
50
|
Application.Current.Resources.MergedDictionaries.Add(dictionary);
|
50
51
|
}
|
51
52
|
}
|
2
追記 画像の場合
answer
CHANGED
@@ -129,4 +129,33 @@
|
|
129
129
|
<system:String x:Key="setting">設定</system:String>
|
130
130
|
</ResourceDictionary>
|
131
131
|
```
|
132
|
-

|
132
|
+

|
133
|
+
|
134
|
+
---
|
135
|
+
追記 画像の場合
|
136
|
+
StringResource.ja-jp.xaml
|
137
|
+
```xaml
|
138
|
+
<ResourceDictionary
|
139
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
140
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
141
|
+
xmlns:system="clr-namespace:System;assembly=mscorlib">
|
142
|
+
<BitmapImage x:Key="ButtonImage" UriSource="/Questions.224205;component/Resources/Image/ButtonImage_jpa.png" />
|
143
|
+
</ResourceDictionary>
|
144
|
+
```
|
145
|
+
`Questions.224205`の部分はアセンブリ名(マウスで簡単に指定できます)
|
146
|
+

|
147
|
+
|
148
|
+
MainWindow.xaml
|
149
|
+
```xaml
|
150
|
+
<Window
|
151
|
+
x:Class="Questions._224205.MainWindow"
|
152
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
153
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
154
|
+
Title="MainWindow"
|
155
|
+
Width="800"
|
156
|
+
Height="450">
|
157
|
+
<StackPanel>
|
158
|
+
<Image Source="{DynamicResource ButtonImage}" />
|
159
|
+
</StackPanel>
|
160
|
+
</Window>
|
161
|
+
```
|
1
コード追記
answer
CHANGED
@@ -1,4 +1,132 @@
|
|
1
1
|
アプリケーション全体で切り替えてよければ、
|
2
2
|
`this.Resources.MergedDictionaries.Add(dictionary);//←この部分`を
|
3
3
|
`Application.Current.Resources.MergedDictionaries.Add(dictionary);`に
|
4
|
-
するのが簡単でしょうか。
|
4
|
+
するのが簡単でしょうか。
|
5
|
+
|
6
|
+
---
|
7
|
+
追記 メインウィンドウと言語設定ダイアログがあると思い、こんな感じになりましたがどうでしょうか。
|
8
|
+
|
9
|
+
MainWindow.xaml
|
10
|
+
```xaml
|
11
|
+
<Window
|
12
|
+
x:Class="Questions._224205.MainWindow"
|
13
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
14
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
15
|
+
Title="MainWindow"
|
16
|
+
Width="800"
|
17
|
+
Height="450">
|
18
|
+
<StackPanel>
|
19
|
+
<Label Content="{DynamicResource language}" />
|
20
|
+
<Button Click="Button_Click" Content="{DynamicResource setting}" />
|
21
|
+
</StackPanel>
|
22
|
+
</Window>
|
23
|
+
```
|
24
|
+
MainWindow.xaml.cs
|
25
|
+
```cs
|
26
|
+
using System;
|
27
|
+
using System.Windows;
|
28
|
+
|
29
|
+
namespace Questions._224205
|
30
|
+
{
|
31
|
+
public partial class MainWindow : Window
|
32
|
+
{
|
33
|
+
// 何か設定ファイルから読んだとして
|
34
|
+
internal static string NowLanguage = "ja-JP";
|
35
|
+
public MainWindow()
|
36
|
+
{
|
37
|
+
InitializeComponent();
|
38
|
+
SetLanguage(NowLanguage);
|
39
|
+
}
|
40
|
+
private void Button_Click(object sender, RoutedEventArgs e)
|
41
|
+
=> new Language().ShowDialog();
|
42
|
+
|
43
|
+
public static void SetLanguage(string cultureCode)
|
44
|
+
{
|
45
|
+
var dictionary = new ResourceDictionary
|
46
|
+
{
|
47
|
+
Source = new Uri(@"Resources/Language/StringResource." + cultureCode + @".xaml", UriKind.Relative)
|
48
|
+
};
|
49
|
+
Application.Current.Resources.MergedDictionaries.Add(dictionary);
|
50
|
+
}
|
51
|
+
}
|
52
|
+
}
|
53
|
+
```
|
54
|
+
Language.xaml
|
55
|
+
```xaml
|
56
|
+
<Window
|
57
|
+
x:Class="Questions._224205.Language"
|
58
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
59
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
60
|
+
Title="{DynamicResource setting}"
|
61
|
+
Width="800"
|
62
|
+
Height="450"
|
63
|
+
WindowStartupLocation="CenterScreen">
|
64
|
+
<StackPanel>
|
65
|
+
<TextBlock x:Name="TestTextBox" Text="{DynamicResource language}" />
|
66
|
+
<RadioButton
|
67
|
+
x:Name="radioButton_ENG"
|
68
|
+
VerticalContentAlignment="Center"
|
69
|
+
Checked="RadioButtonENG_Checked"
|
70
|
+
Content="English" />
|
71
|
+
<RadioButton
|
72
|
+
x:Name="radioButton_JPA"
|
73
|
+
VerticalContentAlignment="Center"
|
74
|
+
Checked="RadioButtonJPA_Checked"
|
75
|
+
Content="日本語" />
|
76
|
+
</StackPanel>
|
77
|
+
</Window>
|
78
|
+
```
|
79
|
+
Language.xaml.cs
|
80
|
+
```cs
|
81
|
+
using System.Windows;
|
82
|
+
|
83
|
+
namespace Questions._224205
|
84
|
+
{
|
85
|
+
public partial class Language : Window
|
86
|
+
{
|
87
|
+
public Language()
|
88
|
+
{
|
89
|
+
InitializeComponent();
|
90
|
+
|
91
|
+
// 雑すぎますが本題ではないので。。
|
92
|
+
switch(MainWindow.NowLanguage)
|
93
|
+
{
|
94
|
+
case "en-US":
|
95
|
+
radioButton_ENG.IsChecked = true;
|
96
|
+
break;
|
97
|
+
case "ja-JP":
|
98
|
+
radioButton_JPA.IsChecked = true;
|
99
|
+
break;
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
private void RadioButtonENG_Checked(object sender, RoutedEventArgs e)
|
104
|
+
// 雑すぎますが本題ではないので。。
|
105
|
+
=> MainWindow.SetLanguage("en-US");
|
106
|
+
|
107
|
+
private void RadioButtonJPA_Checked(object sender, RoutedEventArgs e)
|
108
|
+
=> MainWindow.SetLanguage("ja-JP");
|
109
|
+
}
|
110
|
+
}
|
111
|
+
```
|
112
|
+
StringResource.en-us.xaml
|
113
|
+
```xaml
|
114
|
+
<ResourceDictionary
|
115
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
116
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
117
|
+
xmlns:system="clr-namespace:System;assembly=mscorlib">
|
118
|
+
<system:String x:Key="language">Language</system:String>
|
119
|
+
<system:String x:Key="setting">setting</system:String>
|
120
|
+
</ResourceDictionary>
|
121
|
+
```
|
122
|
+
StringResource.ja-jp.xaml
|
123
|
+
```xaml
|
124
|
+
<ResourceDictionary
|
125
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
126
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
127
|
+
xmlns:system="clr-namespace:System;assembly=mscorlib">
|
128
|
+
<system:String x:Key="language">言語</system:String>
|
129
|
+
<system:String x:Key="setting">設定</system:String>
|
130
|
+
</ResourceDictionary>
|
131
|
+
```
|
132
|
+

|