Proyekt Strukturu:
ProjeAdı/
│
├── Model/
│ └── MyModel.cs
│
├── ViewModel/
│ └── MyViewModel.cs
│
└── View/
├── MainWindow.xaml
└── MainWindow.xaml.cs
Tətbiqetmə sxemi:
Model (MyModel) <-------> ViewModel (MyViewModel) <-------> View (MainWindow)
İndi bu nümunəvi tətbiqin hər hissəsi üçün bir fayl yaradılması və hər bir kod sətrinə bir şərh əlavə edilməsi prosesinə keçək:
1. Model (MyModel.cs)
namespace ProjeAdı.Model
{
public class MyModel
{
// Modelin xüsusiyyətləri
public string Ad { get; set; }
public int Yaş { get; set; }
}
}
Bu faylda, Model (MyModel) sinfi təsvir edilir. Burada, tətbiqin məlumat strukturunu təmsil edən sadə xüsusiyyətlər yer alır.
2. ViewModel (MyViewModel.cs)
using System.ComponentModel;
using System.Windows.Input;
using System.Windows;
namespace ProjeAdı.ViewModel
{
public class MyViewModel : INotifyPropertyChanged
{
// ViewModel-dəki xüsusiyyətlər
private MyModel _model;
public MyModel Model
{
get { return _model; }
set
{
_model = value;
OnPropertyChanged(nameof(Model));
}
}
// Konstruktor
public MyViewModel()
{
Model = new MyModel();
ShowMessageCommand = new RelayCommand(ShowMessage);
}
// ICommand təyini
public ICommand ShowMessageCommand { get; }
// ICommand ilə əlaqələndirilmiş metod
private void ShowMessage()
{
MessageBox.Show($"Ad: {Model.Ad}, Yaş: {Model.Yaş}");
}
// INotifyPropertyChanged implementasiyası
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
// RelayCommand sinifi
public class RelayCommand : ICommand
{
private readonly Action _execute;
public RelayCommand(Action execute)
{
_execute = execute;
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
_execute();
}
public event EventHandler CanExecuteChanged;
}
}
}
Bu faylda, ViewModel (MyViewModel) sinfi təsvir edilir. ViewModel, View (UI) və Model arasındakı əlaqədən məsul olaraq, istifadəçi arayüzündəki logikanı daxil edir.
3. View (MainWindow.xaml və MainWindow.xaml.cs)
MainWindow.xaml
<Window x:Class="ProjeAdı.View.MainWindow"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ProjeAdı.View"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
DataContext="{StaticResource AnaViewModel}">
<Grid>
<!-- İstifadəçi arayüzü elementləri buraya daxil ediləcək -->
<Button Content="Mesaj Göstər" Command="{Binding ShowMessageCommand}" Margin="10"/>
</Grid>
</Window>
MainWindow.xaml.cs
using System.Windows;
namespace ProjeAdı.View
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
MainWindow.xaml faylı, UI elementlərinin (məsələn, düymənin) XAML təyinlərini içərir. DataContext, ViewModel ilə View arasındakı bağlantını təmin edir.
MainWindow.xaml.cs faylı, MainWindow.xaml-in kod arxasını əhatə edir. Bu sinif, istifadəçi arayüzünün kod tərəfli hadisələrini idarə edir.
Bu şekildə faylları ayıraraq, kodunuzu daha nizamlı hala gətirə bilər və hər bir faylın müəyyən bir məsuliyyəti olmasını təmin edə bilərsiniz. Bu, tətbiqinizi daha asan idarə etmənizi və genişləndirilə bilən hala gətirəcəkdir.