/// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.navigationHelper.OnNavigatedTo(e);

            Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
            ViewModel.EmprestimoViewModel emprestimo = new ViewModel.EmprestimoViewModel();
            emprestimo.SelectedEmprestimo = new Model.Emprestimo();
            if (e.Parameter.GetType() == typeof(ViewModel.EmprestimoViewModel))
            {
                emprestimo = (ViewModel.EmprestimoViewModel)e.Parameter;
                roamingSettings.Values["Livro"] = emprestimo.SelectedEmprestimo.Livro.ID.Value;
            }
            else
            {
                if (roamingSettings.Values.ContainsKey("Livro"))
                {
                    var codigoLivro = Convert.ToInt32(roamingSettings.Values["Livro"].ToString());
                    emprestimo.SelectedEmprestimo.Livro = (new ViewModel.LivroViewModel()).ObterLivroPorCodigo(codigoLivro);
                }
                emprestimo.SelectedEmprestimo.Usuario = (Model.Usuario)e.Parameter;
            }

            if (emprestimo.SelectedEmprestimo.ID.HasValue)
                ButtonSalvar.IsEnabled = false;
            else
                ButtonSalvar.IsEnabled = true;

            this.DataContext = emprestimo;
        }
Exemplo n.º 2
0
        private void MenuFlyoutEmprestar_Click(object sender, RoutedEventArgs e)
        {
            Model.Emprestimo novoEmprestimo = new Emprestimo();
            novoEmprestimo.Livro = (Model.Livro)((MenuFlyoutItem)e.OriginalSource).CommandParameter;

            ViewModel.EmprestimoViewModel emprestimoViewModel = new ViewModel.EmprestimoViewModel();
            emprestimoViewModel.SelectedEmprestimo = novoEmprestimo;

            Frame rootFrame = Window.Current.Content as Frame;
            rootFrame.Navigate(typeof(View.NovoEmprestimoPage), emprestimoViewModel);
        }
        private void ButtonSalvar_Click(object sender, RoutedEventArgs e)
        {
            this.EmprestimoViewModel = new ViewModel.EmprestimoViewModel();

            Model.Emprestimo emprestimo = (Model.Emprestimo)((AppBarButton)e.OriginalSource).CommandParameter;
            emprestimo.CodigoLivro = emprestimo.Livro.ID.Value;
            emprestimo.CodigoUsuario = emprestimo.Usuario.ID.Value;

            if (this.EmprestimoViewModel.ListaDeEmprestimosAtivos.Exists(i => i.Livro.ID == emprestimo.CodigoLivro))
                MessageBox.Show("Livro está emprestado.");
            else
            {
                if (emprestimo.Usuario == null)
                    MessageBox.Show("Selecione um usuário.");
                else
                {
                    if (ToggleEmprestimoDataAtual.IsOn)
                        emprestimo.DataEmprestimo = DateTime.Now;
                    else
                    {
                        DateTime data;
                        if (DateTime.TryParse(TextBoxData.Text, out data))
                            emprestimo.DataEmprestimo = data;
                        else
                            MessageBox.Show("Data inválida");
                    }

                    this.EmprestimoViewModel.SalvarEmprestimo.Execute(emprestimo);
                }
            }
        }