###前提・実現したいこと
MVVMで、Web検索しますと、複数の解釈とコード例が検索されますが、基本的な例が見つかりません。
そこで、MS社の資料を参考にC# Prism Unityで、MVVM構造の単純なプログラムを作成しました。
<作成内容>
「各テキストボックスに、ある数Aとある数Bを入力し、ボタンを押すとラベルに結果を表示する。」
<課題>
素直に、そのまま作りますと、只の足し算が信じられない複雑なプログラムになりました。
簡単なプログラムにする良い方法を検討しています。
###ここに質問したいことを詳細に書いてください
C# Prism Unityで、MVVM構造プログラムを作成する場合、定石的な方法で、簡単なコードをご教授下さい。
###該当のソースコード
マイクロソフト社の解説書
CompositeWPF-June2008-JA-JP.chm
MVVM関連
https://www.microsoft.com/ja-JP/download/details.aspx?id=39042
(複雑になった基本プログラム)
1.準備
VisualStudioCommunity2015
wpfアプリケーションのプロジェクト
NuGetパッケージ管理より
Prism、Prism.Unitをインストール
2.Composite Application Libraryを学ぶ
2-1 App.XAML
XAML言語
<Application x:Class="WpfMVVMPrism.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfMVVMPrism" Startup ="Application_Startup"> <Application.Resources> </Application.Resources> </Application>
2-2 App.XAML.cs
C#言語
using System.Windows; namespace WpfMVVMPrism { /// <summary> /// App.xaml の相互作用ロジック /// </summary> public partial class App : Application { private void Application_Startup(object sender, StartupEventArgs e) { var boot = new Bootstrap(); boot.Run(); } } }
2-3 Bootstrap.cs Shellをコンテナに登録(最小コード)
C#言語
using System.Windows; using Microsoft.Practices.Unity; using Prism.Mvvm; namespace WpfMVVMPrism { /// <summary> /// Unity コンテナーを使用する。 /// </summary> public class Bootstrap : Prism.Unity.UnityBootstrapper { protected override DependencyObject CreateShell() { return this.Container.Resolve<Shell>(); } protected override void InitializeShell() { ViewModelLocator.SetAutoWireViewModel(this.Shell, true); ((Shell)this.Shell).Show(); } } }
2-4 Shell.XAML
XAML言語
<Window x:Class="WpfMVVMPrism.Shell" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WpfMVVMPrism" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Title="MVVMの基本その1(計算 A+B=C)" Width="350" Height="200" mc:Ignorable="d"> <!-- <Window.DataContext> <local:ViewModel /> </Window.DataContext> --> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <TextBox x:Name="textBoxA" Grid.Row="0" Text="{Binding DataA}" /> <TextBox x:Name="textBoxB" Grid.Row="1" Text="{Binding DataB}" /> <Button x:Name="button" Grid.Row="2" Content="「A+B=C」の計算をする" Command="{Binding CalculationCommand}"/> <Label x:Name="labelC" Grid.Row="3" Content="{Binding ResultC}" /> </Grid> </Window>
2-5 Shell.XAML.CS
C#言語
using System.Windows; namespace WpfMVVMPrism { /// <summary> /// Shell.xaml の相互作用ロジック /// </summary> public partial class Shell : Window { public Shell() { InitializeComponent(); this.DataContext = new ViewModel(); } } }
2-6 VeiwModel.cs
C#言語
using System; using System.ComponentModel; using System.Windows.Input; namespace WpfMVVMPrism { public class ViewModel : INotifyPropertyChanged { public int DataA { get; set; } public int DataB { get; set; } public event PropertyChangedEventHandler PropertyChanged; private int _resultC; public int ResultC { get { return _resultC; } set { _resultC = value; OnPropertyChanged("ResultC"); } } protected void OnPropertyChanged(string name) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(name)); } } private ICommand _calculationCommand; public ICommand CalculationCommand { get { if (_calculationCommand == null) { _calculationCommand = new RelayCommand(ExecuteCalculationCommand); } return _calculationCommand; } } private void ExecuteCalculationCommand(object x) { Model m = new Model(); m.a_val = DataA; m.b_val = DataB; ResultC = m.SumAB(); } } internal class RelayCommand : ICommand { private Action<object> executeCalculationCommand; public RelayCommand(Action<object> executeCalculationCommand) { this.executeCalculationCommand = executeCalculationCommand; } public event EventHandler CanExecuteChanged; public bool CanExecute(object parameter) { return true; } public void Execute(object parameter) { this.executeCalculationCommand(parameter); } } }
2-7 Model.cs
C#言語
namespace WpfMVVMPrism { public class Model { public int a_val; public int b_val; public int c_val; public int SumAB() { return c_val = a_val + b_val; } } }
以上です。
まだ回答がついていません
会員登録して回答してみよう