質問編集履歴

2

コードの修正

2018/11/01 14:03

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
 
10
10
 
11
- ですがページA→ページB→ページAと移動するとページAのテキストボックスに入力されていた値が保持されません。
11
+ ですがSetting(ページ)Home(ページ)Setting(ページ)と移動するとSettingのテキストボックスに入力されていた値が保持されません。
12
12
 
13
13
 
14
14
 
@@ -16,17 +16,17 @@
16
16
 
17
17
 
18
18
 
19
- PageAを読み込むとSettingクラスのオブジェクトsetting_classが生成。
19
+ Settingページを読み込むとSettingクラスのオブジェクトsetting_classが生成。
20
-
20
+
21
- PageAのTextBoxに値が入力される
21
+ 自分で入力することでSettingページのTextBoxに値が入力される
22
22
 
23
23
  ③BindingしてあるSettingクラスのusernameプロパティが入力された値になる
24
24
 
25
- 違いページに移動
25
+ Homeページに移動
26
-
26
+
27
- ⑤再びPageAに戻る
27
+ ⑤再びSettingページに戻る
28
-
28
+
29
- PageAのコンストラクタでSettingクラスのオブジェクトsetting_classがテキストボックスに割り当てられる
29
+ SettingページのコンストラクタでSettingクラスのオブジェクトsetting_classがテキストボックスに割り当てられる
30
30
 
31
31
 
32
32
 
@@ -42,19 +42,9 @@
42
42
 
43
43
  <Window x:Class="MVVM.MainWindow"
44
44
 
45
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
46
-
47
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
48
-
49
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
50
-
51
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
52
-
53
- xmlns:local="clr-namespace:MVVM"
54
-
55
- mc:Ignorable="d"
45
+ //不必要な部分はカット
56
-
46
+
57
- Title="MainWindow" Height="450" Width="800">
47
+ Title="MainWindow" Height="450" Width="800">
58
48
 
59
49
  <Grid>
60
50
 
@@ -134,19 +124,7 @@
134
124
 
135
125
  <Page x:Class="MVVM.Home"
136
126
 
137
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
138
-
139
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
140
-
141
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
142
-
143
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
144
-
145
- xmlns:local="clr-namespace:MVVM"
146
-
147
- mc:Ignorable="d"
127
+ //不必要な部分はカット
148
-
149
- d:DesignHeight="450" d:DesignWidth="800"
150
128
 
151
129
  Title="Home">
152
130
 
@@ -172,194 +150,178 @@
172
150
 
173
151
  {
174
152
 
153
+
154
+
155
+ public partial class Home : Page
156
+
157
+ {
158
+
159
+ public Home()
160
+
161
+ {
162
+
163
+ InitializeComponent();
164
+
165
+ }
166
+
167
+ }
168
+
169
+ }
170
+
171
+ ```
172
+
173
+
174
+
175
+ **Setting.xaml**
176
+
177
+
178
+
179
+ ```
180
+
181
+ <Page x:Class="MVVM.Setting"
182
+
183
+ //不必要な部分はカット
184
+
185
+ Title="Setting">
186
+
187
+
188
+
189
+ <Grid Background="White">
190
+
191
+ <TextBox HorizontalAlignment="Left" Height="23" Margin="263,163,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"
192
+
193
+ Text="{Binding username}"/>
194
+
195
+
196
+
197
+ </Grid>
198
+
199
+ </Page>
200
+
201
+
202
+
203
+ ```
204
+
205
+
206
+
207
+ **Setting_ViewModel.cs**
208
+
209
+
210
+
211
+ ```
212
+
213
+ using System;
214
+
215
+ using System.Collections.Generic;
216
+
217
+ using System.Linq;
218
+
219
+ using System.Text;
220
+
221
+ using System.Threading.Tasks;
222
+
223
+ using System.ComponentModel;
224
+
225
+
226
+
227
+ namespace MVVM
228
+
229
+ {
230
+
231
+ class Setting_ViewModel :INotifyPropertyChanged
232
+
233
+ {
234
+
235
+ private string username_value;
236
+
237
+ public string username
238
+
239
+ {
240
+
241
+ get { return username_value; }
242
+
243
+
244
+
245
+ set
246
+
247
+ {
248
+
249
+ username_value = value;
250
+
251
+ NotifyPropertyChanged("username");
252
+
253
+ }
254
+
255
+ }
256
+
257
+
258
+
259
+ public event PropertyChangedEventHandler PropertyChanged;
260
+
261
+ private void NotifyPropertyChanged(String info)
262
+
263
+ {
264
+
265
+ if (PropertyChanged != null)
266
+
267
+ {
268
+
269
+ PropertyChanged(this, new PropertyChangedEventArgs(info));
270
+
271
+ }
272
+
273
+ }
274
+
275
+ }
276
+
277
+ }
278
+
279
+
280
+
281
+ ```
282
+
283
+
284
+
285
+
286
+
287
+ **Setting.xaml.cs**
288
+
289
+
290
+
291
+ ```
292
+
293
+ namespace MVVM
294
+
295
+ {
296
+
175
297
  /// <summary>
176
298
 
177
- /// Home.xaml の相互作用ロジック
299
+ /// Setting.xaml の相互作用ロジック
178
300
 
179
301
  /// </summary>
180
302
 
181
- public partial class Home : Page
303
+ public partial class Setting : Page
182
304
 
183
305
  {
184
306
 
185
- public Home()
307
+ public Setting()
186
308
 
187
309
  {
188
310
 
189
311
  InitializeComponent();
190
312
 
313
+ IdTextBox.DataContext = setting_class;
314
+
191
- }
315
+ }
316
+
317
+
318
+
319
+ Setting_ViewModel setting_class = new Setting_ViewModel();
192
320
 
193
321
  }
194
322
 
195
323
  }
196
324
 
325
+
326
+
197
- ```
327
+ ```
198
-
199
-
200
-
201
- **Setting.xaml**
202
-
203
-
204
-
205
- ```
206
-
207
- <Page x:Class="MVVM.Setting"
208
-
209
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
210
-
211
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
212
-
213
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
214
-
215
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
216
-
217
- xmlns:local="clr-namespace:MVVM"
218
-
219
- mc:Ignorable="d"
220
-
221
- d:DesignHeight="450" d:DesignWidth="800"
222
-
223
- Title="Setting">
224
-
225
-
226
-
227
- <Grid Background="White">
228
-
229
- <TextBox HorizontalAlignment="Left" Height="23" Margin="263,163,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"
230
-
231
- Text="{Binding username}"/>
232
-
233
-
234
-
235
- </Grid>
236
-
237
- </Page>
238
-
239
-
240
-
241
- ```
242
-
243
-
244
-
245
- **Setting_ViewModel.cs**
246
-
247
-
248
-
249
- ```
250
-
251
- using System;
252
-
253
- using System.Collections.Generic;
254
-
255
- using System.Linq;
256
-
257
- using System.Text;
258
-
259
- using System.Threading.Tasks;
260
-
261
- using System.ComponentModel;
262
-
263
-
264
-
265
- namespace MVVM
266
-
267
- {
268
-
269
- class Setting_ViewModel :INotifyPropertyChanged
270
-
271
- {
272
-
273
- private string username_value;
274
-
275
- public string username
276
-
277
- {
278
-
279
- get { return username_value; }
280
-
281
-
282
-
283
- set
284
-
285
- {
286
-
287
- username_value = value;
288
-
289
- NotifyPropertyChanged("username");
290
-
291
- }
292
-
293
- }
294
-
295
-
296
-
297
- public event PropertyChangedEventHandler PropertyChanged;
298
-
299
- private void NotifyPropertyChanged(String info)
300
-
301
- {
302
-
303
- if (PropertyChanged != null)
304
-
305
- {
306
-
307
- PropertyChanged(this, new PropertyChangedEventArgs(info));
308
-
309
- }
310
-
311
- }
312
-
313
- }
314
-
315
- }
316
-
317
-
318
-
319
- ```
320
-
321
-
322
-
323
-
324
-
325
- **Setting.xaml.cs**
326
-
327
-
328
-
329
- ```
330
-
331
- namespace MVVM
332
-
333
- {
334
-
335
- /// <summary>
336
-
337
- /// Setting.xaml の相互作用ロジック
338
-
339
- /// </summary>
340
-
341
- public partial class Setting : Page
342
-
343
- {
344
-
345
- public Setting()
346
-
347
- {
348
-
349
- InitializeComponent();
350
-
351
- IdTextBox.DataContext = setting_class;
352
-
353
- }
354
-
355
-
356
-
357
- Setting_ViewModel setting_class = new Setting_ViewModel();
358
-
359
- }
360
-
361
- }
362
-
363
-
364
-
365
- ```

1

コードの編集

2018/11/01 14:03

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -40,7 +40,49 @@
40
40
 
41
41
  ```
42
42
 
43
-
43
+ <Window x:Class="MVVM.MainWindow"
44
+
45
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
46
+
47
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
48
+
49
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
50
+
51
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
52
+
53
+ xmlns:local="clr-namespace:MVVM"
54
+
55
+ mc:Ignorable="d"
56
+
57
+ Title="MainWindow" Height="450" Width="800">
58
+
59
+ <Grid>
60
+
61
+ <Button Content="Button" HorizontalAlignment="Left" Margin="10,139,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.24,1.068" Click="Button_Click"/>
62
+
63
+ <Button Content="Button" HorizontalAlignment="Left" Margin="10,183,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.24,1.068" Click="Button_Click_1"/>
64
+
65
+ <Frame Grid.Row="1" NavigationUIVisibility="Hidden" Name="Frame" Margin="160,0,0,0" Background="Black"/>
66
+
67
+ </Grid>
68
+
69
+ </Window>
70
+
71
+
72
+
73
+ ```
74
+
75
+
76
+
77
+ **MainWindows.xaml.cs**
78
+
79
+ ```
80
+
81
+
82
+
83
+ public partial class MainWindow : Window
84
+
85
+ {
44
86
 
45
87
  public MainWindow()
46
88
 
@@ -50,57 +92,201 @@
50
92
 
51
93
  Navi = this.Frame.NavigationService;
52
94
 
53
-
54
-
55
- }
56
-
57
-
58
-
59
- //フィールドに定義
60
-
61
- private NavigationService Navi;
62
-
63
- private List<Uri> UriList = new List<Uri>() {new Uri("PageA.xaml",UriKind.Relative),
64
-
65
- new Uri("PageB.xaml",UriKind.Relative),};
66
-
67
-
68
-
69
- private void ListMenu_SelectionChanged(object sender, SelectionChangedEventArgs e)
70
-
71
- {
72
-
73
- int index = ListMenu.SelectedIndex;
74
-
75
- switch (index)
95
+ }
96
+
97
+
98
+
99
+ private NavigationService Navi;
100
+
101
+ private List<Uri> UriList = new List<Uri>() {new Uri("Home.xaml",UriKind.Relative),
102
+
103
+ new Uri("Setting.xaml",UriKind.Relative),};
104
+
105
+
106
+
107
+ private void Button_Click(object sender, RoutedEventArgs e)
108
+
109
+ {
110
+
111
+ Navi.Navigate(UriList[0]);
112
+
113
+ }
114
+
115
+
116
+
117
+ private void Button_Click_1(object sender, RoutedEventArgs e)
118
+
119
+ {
120
+
121
+ Navi.Navigate(UriList[1]);
122
+
123
+ }
124
+
125
+ }
126
+
127
+ ```
128
+
129
+
130
+
131
+ **Home.xaml**
132
+
133
+ ```
134
+
135
+ <Page x:Class="MVVM.Home"
136
+
137
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
138
+
139
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
140
+
141
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
142
+
143
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
144
+
145
+ xmlns:local="clr-namespace:MVVM"
146
+
147
+ mc:Ignorable="d"
148
+
149
+ d:DesignHeight="450" d:DesignWidth="800"
150
+
151
+ Title="Home">
152
+
153
+
154
+
155
+ <Grid Background="#FFFF0202"/>
156
+
157
+ </Page>
158
+
159
+
160
+
161
+
162
+
163
+ ````
164
+
165
+
166
+
167
+ **Home.xaml.cs***
168
+
169
+ ```
170
+
171
+ namespace MVVM
172
+
173
+ {
174
+
175
+ /// <summary>
176
+
177
+ /// Home.xaml の相互作用ロジック
178
+
179
+ /// </summary>
180
+
181
+ public partial class Home : Page
182
+
183
+ {
184
+
185
+ public Home()
186
+
187
+ {
188
+
189
+ InitializeComponent();
190
+
191
+ }
192
+
193
+ }
194
+
195
+ }
196
+
197
+ ```
198
+
199
+
200
+
201
+ **Setting.xaml**
202
+
203
+
204
+
205
+ ```
206
+
207
+ <Page x:Class="MVVM.Setting"
208
+
209
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
210
+
211
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
212
+
213
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
214
+
215
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
216
+
217
+ xmlns:local="clr-namespace:MVVM"
218
+
219
+ mc:Ignorable="d"
220
+
221
+ d:DesignHeight="450" d:DesignWidth="800"
222
+
223
+ Title="Setting">
224
+
225
+
226
+
227
+ <Grid Background="White">
228
+
229
+ <TextBox HorizontalAlignment="Left" Height="23" Margin="263,163,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"
230
+
231
+ Text="{Binding username}"/>
232
+
233
+
234
+
235
+ </Grid>
236
+
237
+ </Page>
238
+
239
+
240
+
241
+ ```
242
+
243
+
244
+
245
+ **Setting_ViewModel.cs**
246
+
247
+
248
+
249
+ ```
250
+
251
+ using System;
252
+
253
+ using System.Collections.Generic;
254
+
255
+ using System.Linq;
256
+
257
+ using System.Text;
258
+
259
+ using System.Threading.Tasks;
260
+
261
+ using System.ComponentModel;
262
+
263
+
264
+
265
+ namespace MVVM
266
+
267
+ {
268
+
269
+ class Setting_ViewModel :INotifyPropertyChanged
270
+
271
+ {
272
+
273
+ private string username_value;
274
+
275
+ public string username
276
+
277
+ {
278
+
279
+ get { return username_value; }
280
+
281
+
282
+
283
+ set
76
284
 
77
285
  {
78
286
 
79
-
80
-
81
- case 0:
82
-
83
- Navi.Navigate(UriList[0]);
287
+ username_value = value;
84
-
85
- break;
288
+
86
-
87
-
88
-
89
- case 1:
90
-
91
- Navi.Navigate(UriList[1]);
289
+ NotifyPropertyChanged("username");
92
-
93
- break;
94
-
95
-
96
-
97
- default:
98
-
99
- break;
100
-
101
-
102
-
103
-
104
290
 
105
291
  }
106
292
 
@@ -108,124 +294,72 @@
108
294
 
109
295
 
110
296
 
111
-
297
+ public event PropertyChangedEventHandler PropertyChanged;
298
+
112
-
299
+ private void NotifyPropertyChanged(String info)
300
+
301
+ {
302
+
303
+ if (PropertyChanged != null)
304
+
305
+ {
306
+
307
+ PropertyChanged(this, new PropertyChangedEventArgs(info));
308
+
309
+ }
310
+
311
+ }
312
+
313
+ }
314
+
315
+ }
316
+
317
+
318
+
113
- ```
319
+ ```
114
-
115
-
116
-
320
+
321
+
322
+
323
+
324
+
117
- **PageA**
325
+ **Setting.xaml.cs**
118
-
326
+
327
+
328
+
119
- ```
329
+ ```
330
+
120
-
331
+ namespace MVVM
332
+
333
+ {
334
+
335
+ /// <summary>
336
+
337
+ /// Setting.xaml の相互作用ロジック
338
+
339
+ /// </summary>
340
+
121
- public partial class PageA : Page
341
+ public partial class Setting : Page
122
342
 
123
343
  {
124
344
 
125
-
126
-
127
- public PageA()
345
+ public Setting()
128
346
 
129
347
  {
130
348
 
131
349
  InitializeComponent();
132
350
 
133
- //TextBoxにオブジェクトのプロパティを割り当てる
134
-
135
- TextBox.DataContext = setting_class;
351
+ IdTextBox.DataContext = setting_class;
136
-
137
-
138
-
352
+
139
- }
353
+ }
140
-
141
- //オブジェクトの生成
354
+
142
-
355
+
356
+
143
- Setting setting_class = new Setting { };
357
+ Setting_ViewModel setting_class = new Setting_ViewModel();
358
+
359
+ }
144
360
 
145
361
  }
146
362
 
147
363
 
148
364
 
149
-
150
-
151
- ````
152
-
153
-
154
-
155
- **PageA_Model.cs**
156
-
157
-
158
-
159
- ```
365
+ ```
160
-
161
- //クラスの定義
162
-
163
- class Setting : INotifyPropertyChanged
164
-
165
- {
166
-
167
-
168
-
169
- private string username_value;
170
-
171
- public string username
172
-
173
- {
174
-
175
- get { return username_value; }
176
-
177
-
178
-
179
- set
180
-
181
- {
182
-
183
- username_value = value;
184
-
185
- NotifyPropertyChanged("username");
186
-
187
- }
188
-
189
- }
190
-
191
-
192
-
193
- public event PropertyChangedEventHandler PropertyChanged;
194
-
195
- private void NotifyPropertyChanged(String info)
196
-
197
- {
198
-
199
- if (PropertyChanged != null)
200
-
201
- {
202
-
203
- PropertyChanged(this, new PropertyChangedEventArgs(info));
204
-
205
- }
206
-
207
- }
208
-
209
-
210
-
211
-
212
-
213
- }
214
-
215
- ```
216
-
217
-
218
-
219
-
220
-
221
- **PageA.xaml**
222
-
223
-
224
-
225
- ```
226
-
227
- <TextBox x:Name="TextBox" Text="{Binding username}"> </TextBox>
228
-
229
-
230
-
231
- ```