質問編集履歴
2
コードの修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,16 +3,16 @@
|
|
3
3
|
|
4
4
|
[WPFサンプル:NavigationServiceを使ったページ遷移](http://gushwell.ldblog.jp/archives/52335648.html)
|
5
5
|
|
6
|
-
ですがページ
|
6
|
+
ですがSetting(ページ)→Home(ページ)→Setting(ページ)と移動するとSettingのテキストボックスに入力されていた値が保持されません。
|
7
7
|
|
8
8
|
オブジェクトのプロパティをTextBoxに割り当てています。
|
9
9
|
|
10
|
-
①
|
10
|
+
①Settingページを読み込むとSettingクラスのオブジェクトsetting_classが生成。
|
11
|
-
②
|
11
|
+
②自分で入力することでSettingページのTextBoxに値が入力される
|
12
12
|
③BindingしてあるSettingクラスのusernameプロパティが入力された値になる
|
13
|
-
④
|
13
|
+
④Homeページに移動
|
14
|
-
⑤再び
|
14
|
+
⑤再びSettingページに戻る
|
15
|
-
⑥
|
15
|
+
⑥SettingページのコンストラクタでSettingクラスのオブジェクトsetting_classがテキストボックスに割り当てられる
|
16
16
|
|
17
17
|
これでページ遷移して値が保存されることを実現できるかと思ったのですがうまくいかないです。
|
18
18
|
原因分かる方いましたらご協力をお願いします。
|
@@ -20,13 +20,8 @@
|
|
20
20
|
**MainWindows.xaml**
|
21
21
|
```
|
22
22
|
<Window x:Class="MVVM.MainWindow"
|
23
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
24
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
25
|
-
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
26
|
-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
27
|
-
xmlns:local="clr-namespace:MVVM"
|
28
|
-
|
23
|
+
//不必要な部分はカット
|
29
|
-
|
24
|
+
Title="MainWindow" Height="450" Width="800">
|
30
25
|
<Grid>
|
31
26
|
<Button Content="Button" HorizontalAlignment="Left" Margin="10,139,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.24,1.068" Click="Button_Click"/>
|
32
27
|
<Button Content="Button" HorizontalAlignment="Left" Margin="10,183,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.24,1.068" Click="Button_Click_1"/>
|
@@ -66,13 +61,7 @@
|
|
66
61
|
**Home.xaml**
|
67
62
|
```
|
68
63
|
<Page x:Class="MVVM.Home"
|
69
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
70
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
71
|
-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
72
|
-
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
73
|
-
xmlns:local="clr-namespace:MVVM"
|
74
|
-
|
64
|
+
//不必要な部分はカット
|
75
|
-
d:DesignHeight="450" d:DesignWidth="800"
|
76
65
|
Title="Home">
|
77
66
|
|
78
67
|
<Grid Background="#FFFF0202"/>
|
@@ -85,9 +74,7 @@
|
|
85
74
|
```
|
86
75
|
namespace MVVM
|
87
76
|
{
|
88
|
-
|
77
|
+
|
89
|
-
/// Home.xaml の相互作用ロジック
|
90
|
-
/// </summary>
|
91
78
|
public partial class Home : Page
|
92
79
|
{
|
93
80
|
public Home()
|
@@ -102,13 +89,7 @@
|
|
102
89
|
|
103
90
|
```
|
104
91
|
<Page x:Class="MVVM.Setting"
|
105
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
106
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
107
|
-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
108
|
-
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
109
|
-
xmlns:local="clr-namespace:MVVM"
|
110
|
-
|
92
|
+
//不必要な部分はカット
|
111
|
-
d:DesignHeight="450" d:DesignWidth="800"
|
112
93
|
Title="Setting">
|
113
94
|
|
114
95
|
<Grid Background="White">
|
1
コードの編集
title
CHANGED
File without changes
|
body
CHANGED
@@ -19,98 +19,165 @@
|
|
19
19
|
|
20
20
|
**MainWindows.xaml**
|
21
21
|
```
|
22
|
+
<Window x:Class="MVVM.MainWindow"
|
23
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
24
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
25
|
+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
26
|
+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
27
|
+
xmlns:local="clr-namespace:MVVM"
|
28
|
+
mc:Ignorable="d"
|
29
|
+
Title="MainWindow" Height="450" Width="800">
|
30
|
+
<Grid>
|
31
|
+
<Button Content="Button" HorizontalAlignment="Left" Margin="10,139,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.24,1.068" Click="Button_Click"/>
|
32
|
+
<Button Content="Button" HorizontalAlignment="Left" Margin="10,183,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.24,1.068" Click="Button_Click_1"/>
|
33
|
+
<Frame Grid.Row="1" NavigationUIVisibility="Hidden" Name="Frame" Margin="160,0,0,0" Background="Black"/>
|
34
|
+
</Grid>
|
35
|
+
</Window>
|
22
36
|
|
37
|
+
```
|
38
|
+
|
39
|
+
**MainWindows.xaml.cs**
|
40
|
+
```
|
41
|
+
|
42
|
+
public partial class MainWindow : Window
|
43
|
+
{
|
23
44
|
public MainWindow()
|
24
45
|
{
|
25
46
|
InitializeComponent();
|
26
47
|
Navi = this.Frame.NavigationService;
|
27
|
-
|
28
48
|
}
|
29
49
|
|
30
|
-
//フィールドに定義
|
31
|
-
|
50
|
+
private NavigationService Navi;
|
32
|
-
|
51
|
+
private List<Uri> UriList = new List<Uri>() {new Uri("Home.xaml",UriKind.Relative),
|
33
|
-
|
52
|
+
new Uri("Setting.xaml",UriKind.Relative),};
|
34
53
|
|
35
|
-
|
54
|
+
private void Button_Click(object sender, RoutedEventArgs e)
|
36
55
|
{
|
37
|
-
int index = ListMenu.SelectedIndex;
|
38
|
-
switch (index)
|
39
|
-
{
|
40
|
-
|
41
|
-
case 0:
|
42
|
-
|
56
|
+
Navi.Navigate(UriList[0]);
|
43
|
-
|
57
|
+
}
|
44
|
-
|
45
|
-
case 1:
|
46
|
-
Navi.Navigate(UriList[1]);
|
47
|
-
break;
|
48
58
|
|
59
|
+
private void Button_Click_1(object sender, RoutedEventArgs e)
|
60
|
+
{
|
49
|
-
|
61
|
+
Navi.Navigate(UriList[1]);
|
62
|
+
}
|
63
|
+
}
|
50
|
-
|
64
|
+
```
|
51
65
|
|
66
|
+
**Home.xaml**
|
67
|
+
```
|
68
|
+
<Page x:Class="MVVM.Home"
|
69
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
70
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
71
|
+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
72
|
+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
73
|
+
xmlns:local="clr-namespace:MVVM"
|
74
|
+
mc:Ignorable="d"
|
75
|
+
d:DesignHeight="450" d:DesignWidth="800"
|
76
|
+
Title="Home">
|
52
77
|
|
78
|
+
<Grid Background="#FFFF0202"/>
|
53
|
-
|
79
|
+
</Page>
|
54
|
-
}
|
55
80
|
|
56
81
|
|
57
|
-
```
|
82
|
+
````
|
58
83
|
|
59
|
-
**
|
84
|
+
**Home.xaml.cs***
|
60
85
|
```
|
86
|
+
namespace MVVM
|
87
|
+
{
|
88
|
+
/// <summary>
|
89
|
+
/// Home.xaml の相互作用ロジック
|
90
|
+
/// </summary>
|
61
|
-
|
91
|
+
public partial class Home : Page
|
62
92
|
{
|
63
|
-
|
64
|
-
|
93
|
+
public Home()
|
65
94
|
{
|
66
95
|
InitializeComponent();
|
67
|
-
//TextBoxにオブジェクトのプロパティを割り当てる
|
68
|
-
TextBox.DataContext = setting_class;
|
69
|
-
|
70
96
|
}
|
71
|
-
//オブジェクトの生成
|
72
|
-
|
97
|
+
}
|
73
98
|
}
|
99
|
+
```
|
74
100
|
|
101
|
+
**Setting.xaml**
|
75
102
|
|
76
|
-
```
|
103
|
+
```
|
104
|
+
<Page x:Class="MVVM.Setting"
|
105
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
106
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
107
|
+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
108
|
+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
109
|
+
xmlns:local="clr-namespace:MVVM"
|
110
|
+
mc:Ignorable="d"
|
111
|
+
d:DesignHeight="450" d:DesignWidth="800"
|
112
|
+
Title="Setting">
|
77
113
|
|
78
|
-
|
114
|
+
<Grid Background="White">
|
115
|
+
<TextBox HorizontalAlignment="Left" Height="23" Margin="263,163,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"
|
116
|
+
Text="{Binding username}"/>
|
79
117
|
|
118
|
+
</Grid>
|
119
|
+
</Page>
|
120
|
+
|
80
121
|
```
|
122
|
+
|
123
|
+
**Setting_ViewModel.cs**
|
124
|
+
|
125
|
+
```
|
126
|
+
using System;
|
127
|
+
using System.Collections.Generic;
|
81
|
-
|
128
|
+
using System.Linq;
|
129
|
+
using System.Text;
|
130
|
+
using System.Threading.Tasks;
|
131
|
+
using System.ComponentModel;
|
132
|
+
|
133
|
+
namespace MVVM
|
134
|
+
{
|
82
|
-
class
|
135
|
+
class Setting_ViewModel :INotifyPropertyChanged
|
83
136
|
{
|
137
|
+
private string username_value;
|
138
|
+
public string username
|
139
|
+
{
|
140
|
+
get { return username_value; }
|
84
141
|
|
85
|
-
private string username_value;
|
86
|
-
|
142
|
+
set
|
87
143
|
{
|
88
|
-
get { return username_value; }
|
89
|
-
|
90
|
-
set
|
91
|
-
{
|
92
|
-
|
144
|
+
username_value = value;
|
93
|
-
|
145
|
+
NotifyPropertyChanged("username");
|
94
|
-
}
|
95
146
|
}
|
147
|
+
}
|
96
148
|
|
97
|
-
|
149
|
+
public event PropertyChangedEventHandler PropertyChanged;
|
98
|
-
|
150
|
+
private void NotifyPropertyChanged(String info)
|
151
|
+
{
|
152
|
+
if (PropertyChanged != null)
|
99
153
|
{
|
100
|
-
if (PropertyChanged != null)
|
101
|
-
{
|
102
|
-
|
154
|
+
PropertyChanged(this, new PropertyChangedEventArgs(info));
|
103
|
-
}
|
104
155
|
}
|
156
|
+
}
|
157
|
+
}
|
158
|
+
}
|
105
159
|
|
106
|
-
|
107
|
-
}
|
108
160
|
```
|
109
161
|
|
110
162
|
|
111
|
-
**
|
163
|
+
**Setting.xaml.cs**
|
112
164
|
|
113
165
|
```
|
166
|
+
namespace MVVM
|
167
|
+
{
|
168
|
+
/// <summary>
|
169
|
+
/// Setting.xaml の相互作用ロジック
|
114
|
-
|
170
|
+
/// </summary>
|
171
|
+
public partial class Setting : Page
|
172
|
+
{
|
173
|
+
public Setting()
|
174
|
+
{
|
175
|
+
InitializeComponent();
|
176
|
+
IdTextBox.DataContext = setting_class;
|
177
|
+
}
|
115
178
|
|
179
|
+
Setting_ViewModel setting_class = new Setting_ViewModel();
|
180
|
+
}
|
181
|
+
}
|
182
|
+
|
116
183
|
```
|