• 1405/05/25

ساخت مدیریت آموزشگاه با Maui :

با سلام خدمت استاد مدائنی عزیز

بنده برای اینکه بیشتر بتوانم تمرین کنم تصمیم گرفتم با آموزش سی شارپ دات نت و maui خود شما در تاپ لرن همین پروژه را در maui پیاده سازی کنم. البته بصورت خیلی مبتدی یعنی از MVVM و Id و ... استفاده نکردم و تقریبا از همین مباحث بوت کمپ برای پیاده سازی استفاده کردم.

البته امیدوارم این کار از نظر شما صحیح بوده باشد.

تا الان فقط تونستم صفحه اصلی، فرمهای مدیریت اشخاص شامل لیست اشخاص و افزودن و ویرایش را پیاده سازی کنم.

حتی الامکان سعی کردم از هوش مصنوعی استفاده نکنم ولی چون برخی کدها جدید بود مجبور شدم!!! 

کدهای صفحه اصلی

using Utilities.Convertor;
using DataLayer.Contex;
namespace Maui_Gym_Managment
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            Dispatcher.StartTimer(TimeSpan.FromSeconds(1), () =>
                {
                    lblTime.Text = $"ساعت: {DateTime.Now.ToString("HH:mm:ss")}";
                    return true;
                });
            lblDate.Text = $"تاریخ امروز: {DateTime.Now.ToShamsiLong()}";
            LoadReportData();
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            LoadReportData();
        }

        private async void btnPersonManagment_Clicked(object sender, EventArgs e)
        {
            await Shell.Current.GoToAsync(nameof(PersonListPage));
        }

        void LoadReportData()
        {
            using (EF_GymManagment_DB_Contex _contex = new EF_GymManagment_DB_Contex())
            {
                var playerCount = _contex.Role
                                    .Where(r => r.PersonRole == "ورزشکار")
                                    .Select(r => r.PersonId).Distinct().Count();

                var coachCount = _contex.Role
                    .Where(r => r.PersonRole == "مربی")
                    .Select(r => r.PersonId).Distinct().Count();

                var courseActive = _contex.Courses
                    .Where(c => c.IsActive == true)
                    .Count();

                var allCourse = _contex.Courses
                    .Count();
                lblPlayerCount.Text = playerCount.ToString();
                lblCoachCount.Text = coachCount.ToString();
                lblActiveCourse.Text = courseActive.ToString();
                lblAllCourse.Text = allCourse.ToString();
            }
        }
    }
}

کدهای صفحه لیست اشخاص

using DataLayer.Contex;
using DataLayer.Models;

namespace Maui_Gym_Managment;

public partial class PersonListPage : ContentPage
{
    EF_GymManagment_DB_Contex _Contex = new EF_GymManagment_DB_Contex();
    public PersonListPage()
    {
        InitializeComponent();
        LoadData();
    }
    protected override void OnAppearing()
    {
        base.OnAppearing();
        LoadData();
    }

    private async void TapGestureRecognizer_Tapped(object sender, TappedEventArgs e)
    {
        await Shell.Current.GoToAsync(nameof(PersonAddOrEditPage));
    }

    private async void btnAddPerson_Clicked(object sender, EventArgs e)
    {
        await Shell.Current.GoToAsync(nameof(PersonAddOrEditPage));
    }

    private void LoadData()
    {
        var list = _Contex.People.Select(p => new
        {
            p.PersonId,
            p.Name,
            p.Family,
            p.Mobile,
            p.Email,
            Roles = string.Join(", ", _Contex.Role.Where(r => r.PersonId == p.PersonId).Select(r => r.PersonRole).ToList()),
            p.CreateDate
        }).ToList();

        PersonColectionView.ItemsSource = list;
    }
    private Person selectedPerson;
    private void PersonColectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        selectedPerson = e.CurrentSelection.FirstOrDefault() as Person;
    }

    private async void Edit_Clicked(object sender, EventArgs e)
    {
        var imageButton = sender as ImageButton;

        if (imageButton == null)
            return;
        var context = imageButton.BindingContext;

        var personId = imageButton.CommandParameter as int?;

        if (personId == null)
            return;

        await Shell.Current.GoToAsync(
       nameof(PersonAddOrEditPage),
       new Dictionary<string, object>
       {
            { "PersonId", personId }
       });
    }

    async void DeletePerson(int id)
    {
        var person = _Contex.People.Find(id);

        var roleList = _Contex.Role.Where(r => r.PersonId == id).ToList();

        if (person == null)
            return;
        bool result = await DisplayAlertAsync(
             "حذف",
             $"آیا از حذف '{person.Name} {person.Family}' مطمئن هستید؟",
             "بله",
             "خیر");

        if (result)
        {
            _Contex.Role.RemoveRange(roleList);
            _Contex.People.Remove(person);
            _Contex.SaveChanges();
            LoadData();
        }
    }

    private void btnDelete_Clicked(object sender, EventArgs e)
    {
        var imageButton = sender as ImageButton;

        if (imageButton == null)
            return;

        var context = imageButton.BindingContext;

        var personId = (int)imageButton.CommandParameter;

        DeletePerson(personId);
    }
}

صفحه افزودن و ویرایش شخص

namespace Maui_Gym_Managment;

using DataLayer.Models;
using DataLayer.Contex;


[QueryProperty(nameof(personId), "PersonId")]
public partial class PersonAddOrEditPage : ContentPage
{
    private int _personId;

    EF_GymManagment_DB_Contex _contex = new EF_GymManagment_DB_Contex();

    public int personId
    {
        get => _personId;
        set
        {
            _personId = value;
            LoadData();
        }
    }
    public PersonAddOrEditPage()
    {
        InitializeComponent();
    }

    void LoadData()
    {
        if (personId == 0)
            return;
        var person = _contex.People.Find(personId);
        var roleList = _contex.Role.Where(r => r.PersonId == personId)
            .Select(r => r.PersonRole).ToList();

        if (person == null)
            return;

        txtName.Text = person.Name;
        txtFamily.Text = person.Family;
        txtMobile.Text = person.Mobile;
        txtEmail.Text = person.Email;
        txtAge.Text = person.Age.ToString();
        chManager.IsChecked = roleList.Contains("مدیر");
        chCoach.IsChecked = roleList.Contains("مربی");
        chPlayer.IsChecked = roleList.Contains("ورزشکار");

        btnSave.Text = "ویرایش";
    }

    #region Validation
    private bool Validation()
    {
        bool isValid = true;
        if (string.IsNullOrEmpty(txtName.Text))
        {
            txtName.BackgroundColor = Colors.LightYellow;
            isValid = false;
        }
        else
        {
            txtName.BackgroundColor = Colors.White;
        }

        if (string.IsNullOrEmpty(txtFamily.Text))
        {
            txtFamily.BackgroundColor = Colors.LightYellow;
            isValid = false;
        }
        else
        {
            txtFamily.BackgroundColor = Colors.White;
        }

        if (string.IsNullOrEmpty(txtMobile.Text))
        {
            txtMobile.BackgroundColor = Colors.LightYellow;
            isValid = false;
        }
        else
        {
            txtMobile.BackgroundColor = Colors.White;
        }
        if (!chManager.IsChecked && !chCoach.IsChecked && !chPlayer.IsChecked)
        {
            isValid = false;
            chPlayer.BackgroundColor = Colors.LightYellow;
            chManager.BackgroundColor = Colors.LightYellow;
            chCoach.BackgroundColor = Colors.LightYellow;
        }
        else
        {
            chPlayer.BackgroundColor = Colors.White;
            chManager.BackgroundColor = Colors.White;
            chCoach.BackgroundColor = Colors.White;
        }
        return isValid;
    }
    #endregion

    private async void btnSave_Clicked(object sender, EventArgs e)
    {
        if (!Validation())
            return;

        if (personId == 0)
        {
            AddPerson();
        }
        else
        {
            EditPerson();
            lblMessage.Text = "شخص با موفقیت ویرایش شد";
        }
        lblMessage.IsVisible = true;
        await Task.Delay(2000);
        lblMessage.IsVisible = false;
        Refresh();
    }

    void Refresh()
    {
        txtName.Focus();
        txtName.Text = string.Empty;
        txtFamily.Text = string.Empty;
        txtMobile.Text = string.Empty;
        txtEmail.Text = string.Empty;
        txtAge.Text = string.Empty;
        chCoach.IsChecked = false;
        chManager.IsChecked = false;
    }

    private async void btnCancele_Clicked(object sender, EventArgs e)
    {
        await Shell.Current.GoToAsync(nameof(PersonListPage));
    }

    void AddPerson()
    {
        Person person = new Person();
        List<string> roles = new List<string>();
        if (chManager.IsChecked)
            roles.Add("مدیر");
        if (chCoach.IsChecked)
            roles.Add("مربی");
        if (chPlayer.IsChecked)
            roles.Add("ورزشکار");
        person.Name = txtName.Text;
        person.Family = txtFamily.Text;
        person.Age = Convert.ToInt32(txtAge.Text);
        person.Mobile = txtMobile.Text;
        person.Email = txtEmail.Text;
        _contex.People.Add(person);
        _contex.SaveChanges();
        foreach (string r in roles)
        {
            Role role = new Role();
            role.PersonId = person.PersonId;
            role.PersonRole = r.ToString();
            _contex.Role.Add(role);
        }
        _contex.SaveChanges();
    }

    void EditPerson()
    {
        var person = _contex.People.Find(personId);
        if (person == null)
            return;
        person.Name = txtName.Text;
        person.Family = txtFamily.Text;
        person.Age = Convert.ToInt32(txtAge.Text);
        person.Mobile = txtMobile.Text;
        person.Email = txtEmail.Text;
        _contex.People.Update(person);

        //حذف نقش های قبلی از جدول Role
        var roleList = _contex.Role.Where(r => r.PersonId == personId).ToList();
        _contex.Role.RemoveRange(roleList);

        //افزودن نقش جدید در جدول Role
        List<string> roles = new List<string>();
        if (chManager.IsChecked)
            roles.Add("مدیر");
        if (chCoach.IsChecked)
            roles.Add("مربی");
        if (chPlayer.IsChecked)
            roles.Add("ورزشکار");

        foreach (string r in roles)
        {
            Role role = new Role();
            role.PersonId = person.PersonId;
            role.PersonRole = r.ToString();
            _contex.Role.Add(role);
        }
        _contex.SaveChanges();
    }
}

 

  • 1405/05/25
  • ساعت 08:20

چقدر عالی و زیبا 

بسیار کار خوبی کردید 

حتما تکمیلش کنید


  • 1405/05/25
  • ساعت 12:00

ممنون از دلگرمی شما استاد عزیز