できるだけ頑張ってみましたが詰まってしまったので詳しい方いましたらご教示ください。
このサイトの説明の通り画面遷移するプログラムを作ることができました。
WPFサンプル:NavigationServiceを使ったページ遷移
ですがSetting(ページ)→Home(ページ)→Setting(ページ)と移動するとSettingのテキストボックスに入力されていた値が保持されません。
オブジェクトのプロパティをTextBoxに割り当てています。
①Settingページを読み込むとSettingクラスのオブジェクトsetting_classが生成。
②自分で入力することでSettingページのTextBoxに値が入力される
③BindingしてあるSettingクラスのusernameプロパティが入力された値になる
④Homeページに移動
⑤再びSettingページに戻る
⑥SettingページのコンストラクタでSettingクラスのオブジェクトsetting_classがテキストボックスに割り当てられる
これでページ遷移して値が保存されることを実現できるかと思ったのですがうまくいかないです。
原因分かる方いましたらご協力をお願いします。
MainWindows.xaml
<Window x:Class="MVVM.MainWindow" //不必要な部分はカット Title="MainWindow" Height="450" Width="800"> <Grid> <Button Content="Button" HorizontalAlignment="Left" Margin="10,139,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.24,1.068" Click="Button_Click"/> <Button Content="Button" HorizontalAlignment="Left" Margin="10,183,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.24,1.068" Click="Button_Click_1"/> <Frame Grid.Row="1" NavigationUIVisibility="Hidden" Name="Frame" Margin="160,0,0,0" Background="Black"/> </Grid> </Window>
MainWindows.xaml.cs
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Navi = this.Frame.NavigationService; } private NavigationService Navi; private List<Uri> UriList = new List<Uri>() {new Uri("Home.xaml",UriKind.Relative), new Uri("Setting.xaml",UriKind.Relative),}; private void Button_Click(object sender, RoutedEventArgs e) { Navi.Navigate(UriList[0]); } private void Button_Click_1(object sender, RoutedEventArgs e) { Navi.Navigate(UriList[1]); } }
Home.xaml
<Page x:Class="MVVM.Home" //不必要な部分はカット Title="Home"> <Grid Background="#FFFF0202"/> </Page>
Home.xaml.cs*
namespace MVVM { public partial class Home : Page { public Home() { InitializeComponent(); } } }
Setting.xaml
<Page x:Class="MVVM.Setting" //不必要な部分はカット Title="Setting"> <Grid Background="White"> <TextBox HorizontalAlignment="Left" Height="23" Margin="263,163,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="{Binding username}"/> </Grid> </Page>
Setting_ViewModel.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ComponentModel; namespace MVVM { class Setting_ViewModel :INotifyPropertyChanged { private string username_value; public string username { get { return username_value; } set { username_value = value; NotifyPropertyChanged("username"); } } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } } }
Setting.xaml.cs
namespace MVVM { /// <summary> /// Setting.xaml の相互作用ロジック /// </summary> public partial class Setting : Page { public Setting() { InitializeComponent(); IdTextBox.DataContext = setting_class; } Setting_ViewModel setting_class = new Setting_ViewModel(); } }
回答1件
あなたの回答
tips
プレビュー