Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

Phân quyền trong MVVM WPF

13:56 03-08-2022 73 lượt xem 0 bình luận 13:59 03-08-2022

Chào anh Long và mọi người, anh cho em hỏi: Làm sao để truyền UserName từ LoginViewModel qua CoffeeShopManagementViewModel để làm phân quyền trong MVVM? Em có tìm trên mạng mà hướng dẫn em không hiểu gì cả. Xin anh và mọi người có thể giúp em được không? Em cảm ơn anh và mọi người

LoginViewModel.cs:

using CoffeeShopMangement.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace CoffeeShopMangement.ViewModel
{
    public class LoginViewModel : BaseViewModel
    {
        private string _UserName;
        public string UserName { get => _UserName; set { _UserName = value; OnPropertyChanged(); } }
        private string _Password;
        public string Password { get => _Password; set { _Password = value; OnPropertyChanged(); } }
        public LoginViewModel()
        {
            RegisterCommand = new RelayCommand<Window>((p) => { return true; }, (p) =>
            {
                Register window = new Register();
                window.Show();
                p.Hide();
            });
            ExitCommand = new RelayCommand<Window>((p) => { return true; },(p)=> {
                FrameworkElement window = GetWindowParent(p);
                var w = window as Window;
                if (w != null)
                {
                    if (MessageBox.Show("Bạn có thật sự muốn thoát chương trình không?", "Thông báo", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                    {
                        w.Close();
                    }
                    Environment.Exit(0);
                }
            });
            PasswordChangedCommand = new RelayCommand<PasswordBox>((p) => { return true; }, (p) => { Password = p.Password; });
            LoginCommand = new RelayCommand<Window>((p) => { return true; }, (p) =>
              {
                  if(UserName==null||Password==null)
                  {
                      MessageBox.Show("Bạn chưa nhập tên đăng nhập hoặc mật khẩu");
                  }
                  else
                  {
                      string passEncode = MD5Hash(Base64Encode(Password));
                      var Login = DataProvider.Instance.Data.Users.Where(x => x.UserName == UserName && x.Password == passEncode).Count();
                      if (Login > 0)
                      {
                          User loginUser = GetUserByUserName(UserName);
                          CoffeeShopMangementWindow window = new CoffeeShopMangementWindow();
                          window.Show();
                          p.Hide();
                      }
                      else
                      {
                          MessageBox.Show("Sai tài khoản hoặc mật khẩu");
                      }
                  }
              });
        }
        public ICommand RegisterCommand { get; set; }
        public ICommand ExitCommand { get; set; }
        public ICommand LoginCommand { get; set; }
        public ICommand PasswordChangedCommand { get; set; }
        FrameworkElement GetWindowParent(Window p)
        {
            FrameworkElement parent = p;
            while (parent.Parent != null)
            {
                parent = parent.Parent as FrameworkElement;
            }
            return parent;
        }
        public static string Base64Encode(string plainText)
        {
            var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
            return System.Convert.ToBase64String(plainTextBytes);
        }
        public static string MD5Hash(string input)
        {
            StringBuilder hash = new StringBuilder();
            MD5CryptoServiceProvider md5provider = new MD5CryptoServiceProvider();
            byte[] bytes = md5provider.ComputeHash(new UTF8Encoding().GetBytes(input));

            for (int i = 0; i < bytes.Length; i++)
            {
                hash.Append(bytes[i].ToString("x2"));
            }
            return hash.ToString();
        }
        public User GetUserByUserName(string UserName)
        {
            var user = DataProvider.Instance.Data.Users.Where(x => x.UserName==UserName);
            foreach (var item in user)
            {
                return item;
            }
            return null;
        }
    }
}

CoffeeShopMangementViewModel.cs:

using CoffeeShopMangement.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace CoffeeShopMangement.ViewModel
{
    public class CoffeeShopMangementViewModel: BaseViewModel
    {
        private User _loginUser;
        public User LoginUser { get => _loginUser; set { _loginUser = value; ChangeUser(LoginUser); } }

        private void ChangeUser(User loginUser)
        {

        }
        private ObservableCollection<TonKho> _TonKhoList;
        public ObservableCollection<TonKho> TonKhoList { get => _TonKhoList; set { _TonKhoList = value; OnPropertyChanged(); } }
        public CoffeeShopMangementViewModel()
        {
            TonKhoList = new ObservableCollection<TonKho>();
            var FoodList = DataProvider.Instance.Data.Foods;
            int i = 1;
            foreach (var item in FoodList)
            {
                var InputList = DataProvider.Instance.Data.InputInfoes.Where(x => x.IdFood == item.Id);
                var OutputList = DataProvider.Instance.Data.OutputInfoes.Where(x => x.IdFood == item.Id);
                int SumInput = 0;
                int SumOutput = 0;
                if (InputList != null && InputList.Count() > 0)
                {
                    SumInput = (int)InputList.Sum(p => p.Count);
                }
                if (OutputList != null && OutputList.Count() > 0)
                {
                    SumOutput = (int)OutputList.Sum(p => p.Count);
                }
                TonKho tonKho = new TonKho();
                tonKho.STT = i;
                tonKho.Count = SumInput - SumOutput;
                tonKho.Food = item;
                TonKhoList.Add(tonKho);
                i++;
            }
            FoodCommand = new RelayCommand<Window>((p) => { return true; }, (p) => {
                FoodWindow window = new FoodWindow();
                window.Show();
                p.Hide();
            });
            TableCommand = new RelayCommand<Window>((p) => { return true; }, (p) => {
                TableWindow window = new TableWindow();
                window.Show();
                p.Hide();
            });
            CustomerCommand = new RelayCommand<Window>((p) => { return true; }, (p) =>
              {
                  CustomerWindow window = new CustomerWindow();
                  window.Show();
                  p.Hide();
              });
            ProfitCommand = new RelayCommand<Window>((p) => { return true; }, (p) =>
              {
                  ProftWindow window = new ProftWindow();
                  window.Show();
                  p.Hide();
              });
            ExitCommand = new RelayCommand<Window>((p) => { return true; }, (p) =>
              {
                  MainWindow window = new MainWindow();
                  window.Show();
                  p.Hide();
              });
            ExportCommand = new RelayCommand<Window>((p) => { return true; }, (p) =>
            {
                OutputWindow window = new OutputWindow();
                window.Show();
                p.Hide();
            });
            InputCommand = new RelayCommand<Window>((p) => { return true; }, (p) =>
              {
                  InputWindow window = new InputWindow(LoginUser);
                  window.Show();
                  p.Hide();
              });
            SuplierCommand = new RelayCommand<Window>((p) => { return true; }, (p) =>
              {
                  SuplierWindow window = new SuplierWindow();
                  window.Show();
                  p.Hide();
              });
        }
        public ICommand FoodCommand { get; set; }
        public ICommand TableCommand { get; set; }
        public ICommand CustomerCommand { get; set; }
        public ICommand ProfitCommand { get; set; }
        public ICommand ExitCommand { get; set; }
        public ICommand InputCommand { get; set; }
        public ICommand ExportCommand { get; set; }
        public ICommand SuplierCommand { get; set; }
        //public User GetUserByUserName(string UserName)
        //{
        //    var user = DataProvider.Instance.Data.Users.Where(x => x.UserName == UserName);
        //    foreach (var item in user)
        //    {
        //        return item;
        //    }
        //    return null;
        //}
    }
}

 

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập

Câu hỏi mới nhất