###前提・実現したいこと
Visual Studio 2015 と教則本を使ってC#プログラミングの勉強をしている超初心者です。
今とある教則本の、WPFアプリケーション(顧客住所録)を、全て写経し、デバッグモードでエラーが無くアプリが正しく起動し、動作すること、までを確認したところです。
次のステップとして、勉強のために、ウオッチウインドウに「自動変数」と「呼び出し履歴」を表示させて、最初からステップイン実行して見ることで、プログラムがどういうプロセスで動くのかを勉強しようと思っています。
そこで、いきなり壁にぶちあたったので質問させて下さい。
得にブレークポイントは設定せず、最初から、ステップイン実行(F11キー押下)を始めると
先ず
MainWindow.xaml.cs の
public partial class MainWindow : Window クラスの
public MainWindow() コンストラクタ { DataContextChanged += MainWindow_DataContextChanged; InitializeComponent(); }
が実行され(黄色くハイライトされ)、「InitializeComponent();」の部分で、ステップイン実行(F11キー押下)すると次は、
public partial class MainViewModel : CustomerViewModel クラスの
public ObservableCollection<CustomerViewModel> Items
{ get; private set; } = new ObservableCollection<CustomerViewModel>();
に処理が移り、次にステップイン実行(F11キー押下)すると、同じクラス内の下記コードに
処理が移りました。
private string _keyword = "";
public MainViewModel() { Clear(); }
###分からないこと
①なぜ、MainWindow(){} の InitializeComponent(); からMainViewModel クラスの Itemsプロパティに処理が飛んだ(?)のか、
②その後、なぜ、同クラス内のフィールド変数 _keyword の宣言に飛んだのか
③その後、なぜ、同クラス内の MainViewModel() のコンストラクタに飛んだのか
が分からないので、ご教示いただけませんでしょうか。なにとぞよろしくお願いいたします。
質問の仕方、必要な情報などが間違っていましたら、お叱り、ご指摘頂ければ幸いです。
###補足情報
- MainViewModel と 2) CustomerViewModel のコードを貼ります。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; using System.Collections.ObjectModel; using System.Data.Entity.Infrastructure; namespace AddClassLibrary { public partial class MainViewModel : CustomerViewModel { public MainViewModel() { Clear(); } public string Title => "ひと目でわかる顧客情報"; public Action<string> ShowErrorMessage { private get; set; } public Func<string, bool> ShowYesNoMessage { private get; set; } public Action ShowAboutWindow { private get; set; } public string DebugConnectionString { get { return AddDbContext.DebugConnectionString; } set { AddDbContext.DebugConnectionString = value; } } public string ReleaseConnectionString { get { return AddDbContext.ReleaseConnectionString; } set { AddDbContext.ReleaseConnectionString = value; } } public string HelpUri { get; set; } public ObservableCollection<CustomerViewModel> Items { get; private set; } = new ObservableCollection<CustomerViewModel>(); private CustomerViewModel _item; public CustomerViewModel Item { get { return _item; } set { if (_item == value) return; _item = value; SetProperties(value); OnPropertyChanged(nameof(Item)); } } private string _keyword = ""; public string Keyword { get { return _keyword; } set { if (_keyword == value) return; _keyword = value; OnPropertyChanged(nameof(Keyword)); } } private void Clear() { Item = null; Items.Clear(); CustomerViewModel m = new CustomerViewModel { Id = 0, Name = "(新規)", PostCode = "0" }; Items.Add(m); Item = m; } private void ExecuteSearchCommand() { Clear(); foreach (Customer x in MainModel.GetItems(Keyword)) { CustomerViewModel item = new CustomerViewModel(x); Items.Add(item); } OnPropertyChanged(nameof(Items)); } private void ExecuteClearCommand() { Keyword = ""; Clear(); OnPropertyChanged(nameof(Items)); } private bool CanExecuteUpdateCommand() => !HasErrors; private void ExecuteUpdateCommand() { Action add = () => { Customer m = new Customer(); m.SetProperties(Customer); m = MainModel.AddItem(m); CustomerViewModel item = new CustomerViewModel(m); Items.Insert(1, item); Item = item; }; Action update = () => { Customer m = Customer; m = MainModel.UpdateItem(m); Item.SetProperties(m); SetProperties(Item); }; Action a = (Item.Id == 0) ? add : update; ExecuteUpdateItem(a); } private bool CanExecuteDeleteCommand() => !HasErrors && Id != 0; private void ExecuteDeleteCommand() { if (!ShowYesNoMessage("選択中の顧客情報を削除して良いですか?")) return; Action delete = () => { MainModel.DeleteItem(Customer); CustomerViewModel m = Item; Item = null; Items.Remove(m); Item = Items[0]; }; ExecuteUpdateItem(delete); } private void ExecuteAboutCommand() => ShowAboutWindow(); private void ExecuteHelpCommand() { try { System.Diagnostics.Process.Start(HelpUri); } catch (Exception ex) { ShowErrorMessage(ex.Message); } } private void ExecuteUpdateItem(Action action) { try { action(); OnPropertyChanged(nameof(Items)); } catch(DbUpdateConcurrencyException ex) { ShowErrorMessage( "他のユーザーによって変更されています。再度、検索しなおして下さい\n\n" + ex.Message); } catch(Exception ex) { ShowErrorMessage("データの更新または削除に失敗しました\n\n" + ex.Message); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AddClassLibrary { public class CustomerViewModel : ModelBase<Customer> { public CustomerViewModel() { } public CustomerViewModel(Customer model) { SetProperties(model); } public void SetProperties(CustomerViewModel source) { if (source == null) return; Id = source.Id; Name = source.Name; Kana = source.Kana; Address = source.Address; TelNumber = source.TelNumber; Memo = source.Memo; TimeStamp = source.TimeStamp; PostCode = source.PostCode; } public void SetProperties(Customer source) { if (source == null) return; Id = source.Id; Name = source.Name; Kana = source.Kana; Address = source.Address; TelNumber = source.TelNumber; Memo = source.Memo; TimeStamp = source.TimeStamp; PostCode = source.PostCode.ToString(); } public Customer Customer => _model; private int _Id; public int Id { get { return _Id; } set { if (_Id == value) return; _Id = value; if (ValidationValue(nameof(Id), value)) { _model.Id = value; } OnPropertyChanged(nameof(Id)); OnPropertyChanged(nameof(Error)); } } private string _Name; public string Name { get { return _Name; } set { if (_Name == value) return; _Name = value; if (ValidationValue(nameof(Name), value)) { _model.Name = value; } OnPropertyChanged(nameof(Name)); OnPropertyChanged(nameof(Error)); } } //(中略 同じように各プロパティの宣言) public override string Error { get { if (_errors.Count > 0) return "不正な値が入力されています"; if (!string.IsNullOrWhiteSpace(_model.Address) && _model.PostCode == 0) { return "住所に準じた郵便番号が入力されていません"; } return null; } } } }

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/14 01:21
2017/08/14 11:42
2017/08/14 12:22
2017/08/14 13:02
2017/08/14 13:05