Skip to content

Instantly share code, notes, and snippets.

@BrianJVarley
Last active December 14, 2015 16:08
Show Gist options
  • Save BrianJVarley/0a01d41edcc4b910d5ab to your computer and use it in GitHub Desktop.
Save BrianJVarley/0a01d41edcc4b910d5ab to your computer and use it in GitHub Desktop.
using MongoDBApp.Services;
using MongoDBApp.ViewModels;
using MongoDBApp.Views;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace MongoDBApp
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var app = new ApplicationView();
var appVM = new ApplicationViewModel();
app.DataContext = appVM;
app.Show();
}
}
}
<Window x:Class="MongoDBApp.Views.ApplicationView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views="clr-namespace:MongoDBApp.Views"
xmlns:vm="clr-namespace:MongoDBApp.ViewModels"
Title="{Binding UserName}"
Width="800"
Height="500"
WindowStartupLocation="CenterScreen">
<Window.Resources>
<DataTemplate DataType="{x:Type vm:CustomerDetailsViewModel}">
<views:CustomerDetailsView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:CustomerOrdersViewModel}">
<views:CustomerOrdersView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:OrderStatisticsViewModel}">
<views:OrderStatisticsView />
</DataTemplate>
</Window.Resources>
<Window.DataContext>
<vm:ApplicationViewModel />
</Window.DataContext>
<TabControl ItemsSource="{Binding PageViewModels}"
SelectedItem="{Binding CurrentPageViewModel}"
TabStripPlacement="Top">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ItemContainerStyle>
<Style TargetType="{x:Type TabItem}">
<Setter Property="IsEnabled" Value="{Binding IsEnabled}" />
</Style>
</TabControl.ItemContainerStyle>
</TabControl>
</Window>
using MongoDBApp.Common;
using MongoDBApp.DAL;
using MongoDBApp.Models;
using MongoDBApp.Services;
using MongoDBApp.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace MongoDBApp.ViewModels
{
public class ApplicationViewModel : ObservableObject
{
#region Fields
private ICommand _changePageCommand;
private IPageViewModel _currentPageViewModel;
private List<IPageViewModel> _pageViewModels;
private string _authenticatedUserName;
private static IDataService<CustomerModel> customerDataService = new CustomerDataService(CustomerRepository.Instance);
private static IDataService<Country> countryDataService = new CountryDataService(CountryRepository.Instance);
private static IDataService<OrderModel> orderDataService = new OrderDataService(OrderRepository.Instance);
private static IDialogService dialogService = new DialogService();
private static IAuthenticationService authoService = new AuthenticationService();
#endregion
/// <summary>
/// Initializes a new instance of the <see cref="ApplicationViewModel"/> class.
/// </summary>
public ApplicationViewModel()
{
// Add available pages
PageViewModels.Add(new CustomerDetailsViewModel(customerDataService, countryDataService, dialogService));
PageViewModels.Add(new CustomerOrdersViewModel(orderDataService));
PageViewModels.Add(new OrderStatisticsViewModel());
PageViewModels.Add(new LoginViewModel(dialogService, authoService));
// Set starting page
CurrentPageViewModel = PageViewModels[0];
Messenger.Default.Register<string>(this, OnLoggedInMessageReceived);
}
#region Properties / Commands
/// <summary>
/// Gets the change page command.
/// </summary>
/// <value>
/// The change page command.
/// </value>
public ICommand ChangePageCommand
{
get
{
if (_changePageCommand == null)
{
_changePageCommand = new RelayCommand(
p => ChangeViewModel((IPageViewModel)p),
p => p is IPageViewModel);
}
return _changePageCommand;
}
}
/// <summary>
/// Gets the page view models.
/// </summary>
/// <value>
/// The page view models.
/// </value>
public List<IPageViewModel> PageViewModels
{
get
{
if (_pageViewModels == null)
_pageViewModels = new List<IPageViewModel>();
return _pageViewModels;
}
}
/// <summary>
/// Gets or sets the current page view model.
/// </summary>
/// <value>
/// The current page view model.
/// </value>
public IPageViewModel CurrentPageViewModel
{
get
{
return _currentPageViewModel;
}
set
{
if (_currentPageViewModel != value)
{
_currentPageViewModel = value;
OnPropertyChanged("CurrentPageViewModel");
}
}
}
/// <summary>
/// Gets or sets the name of the user.
/// </summary>
/// <value>
/// The name of the user.
/// </value>
public string AuthenticatedUserName
{
get
{
return _authenticatedUserName;
}
set
{
if (_authenticatedUserName != value)
{
_authenticatedUserName = value;
OnPropertyChanged("AuthenticatedUserName");
}
}
}
#endregion
#region Methods
private void OnLoggedInMessageReceived(string username)
{
AuthenticatedUserName = username;
}
private void ChangeViewModel(IPageViewModel viewModel)
{
if (!PageViewModels.Contains(viewModel))
PageViewModels.Add(viewModel);
CurrentPageViewModel = PageViewModels
.FirstOrDefault(vm => vm == viewModel);
}
#endregion
}
}
<Window x:Class="MongoDBApp.Views.LoginView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:b="clr-namespace:MongoDBApp.Behaviors"
xmlns:btv="clr-namespace:MongoDBApp.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Width="300"
Height="300"
ResizeMode="NoResize"
d:DesignHeight="300"
d:DesignWidth="450"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="2*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".1*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="2.5*" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Label Name="label1"
Grid.Row="2"
Grid.Column="2"
Grid.ColumnSpan="2"
Height="28"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Content="Username:" />
<TextBox Name="UserTextBox"
Grid.Row="2"
Grid.Column="3"
Grid.ColumnSpan="3"
Width="150"
Height="23"
Margin="0,0,0,2"
HorizontalAlignment="Left"
VerticalAlignment="Bottom"
Text="{Binding UserName}" />
<Label Name="label2"
Grid.Row="3"
Grid.Column="2"
Grid.ColumnSpan="2"
Height="28"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Content="Password:" />
<PasswordBox Name="PasswordTextBox"
Grid.Row="3"
Grid.Column="3"
Grid.ColumnSpan="3"
Width="150"
Height="23"
HorizontalAlignment="Left"
VerticalAlignment="Center">
<i:Interaction.Behaviors>
<b:PasswordBoxBindingBehavior Password="{Binding Password}" />
</i:Interaction.Behaviors>
</PasswordBox>
<Button Name="LoginBtn"
Grid.Row="4"
Grid.Column="3"
Grid.ColumnSpan="2"
Width="80"
Height="22"
HorizontalAlignment="Left"
Command="{Binding LoginCommand}"
Content="Login" />
</Grid>
</Window>
using Auth0.Windows;
using MongoDBApp.Common;
using MongoDBApp.Messages;
using MongoDBApp.Services;
using MongoDBApp.Utility;
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows;
namespace MongoDBApp.ViewModels
{
[ImplementPropertyChanged]
class LoginViewModel : IPageViewModel
{
public string Name { get; set; }
public bool IsEnabled { get; set; }
public ICommand LoginCommand { get; set; }
private IDialogService _dialogService;
private IAuthenticationService _authService;
public LoginViewModel(IDialogService dialogService, IAuthenticationService authService)
{
this._dialogService = dialogService;
this._authService = authService;
LoadCommands();
}
public string UserName { get; set; }
public SecureString Password { get; set; }
private void LoadCommands()
{
LoginCommand = new RelayCommand(OnLogin);
}
private async void OnLogin(object obj)
{
var result = await _authService.LoginAsync(UserName, Password);
if (result)
{
Messenger.Default.Send<string>(UserName);
System.Windows.MessageBox.Show("You are logged in");
}
else
{
System.Windows.MessageBox.Show("Unknown username or password.");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment