-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
340 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 0 additions & 6 deletions
6
MvvmDemo/Messages/AsyncYesNoMessage.cs → ...DemoLibrary/Messages/AsyncYesNoMessage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Toolkit.Mvvm" Version="7.0.0-build.149" /> | ||
</ItemGroup> | ||
|
||
</Project> |
2 changes: 1 addition & 1 deletion
2
MvvmDemo/Services/DebugLogger.cs → MvvmDemoLibrary/Services/DebugLogger.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" /> | ||
<PackageReference Include="xunit" Version="2.4.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" /> | ||
<PackageReference Include="coverlet.collector" Version="1.2.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\MvvmDemoLibrary\MvvmDemoLibrary.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using Microsoft.Toolkit.Mvvm.Messaging; | ||
using MvvmDemo.Messages; | ||
using MvvmDemo.Services; | ||
using MvvmDemo.ViewModels; | ||
using System; | ||
using Xunit; | ||
|
||
namespace MvvmDemoTest { | ||
|
||
public class MainViewModelTest { | ||
|
||
[Fact] | ||
public void DeleteYes() { | ||
// Arrange | ||
var vm = new MainViewModel(new DebugLogger()); | ||
|
||
Messenger.Default.Register<AsyncYesNoMessage>(this, m => { | ||
m.Reply(true); | ||
}); | ||
|
||
// Act | ||
vm.DeleteCommand.Execute(vm.Employees[1]); | ||
|
||
Messenger.Default.Unregister<AsyncYesNoMessage>(this); | ||
|
||
// Assert | ||
Assert.Equal(2, vm.Employees.Count); | ||
} | ||
|
||
[Fact] | ||
public void RaiseSalary() { | ||
// Arrange | ||
var vm = new MainViewModel(new DebugLogger()); | ||
var emp = vm.Employees[1]; | ||
var oldSal = emp.Salary; | ||
|
||
// Act | ||
vm.RaiseSalaryCommand.Execute(emp); | ||
|
||
// Assert | ||
Assert.Equal(oldSal + 100, emp.Salary); | ||
} | ||
|
||
[Fact] | ||
public void RaiseSalaryAboveLimit() { | ||
// Arrange | ||
var vm = new MainViewModel(new DebugLogger()); | ||
var emp = vm.Employees[1]; | ||
emp.Salary = 6000; | ||
var oldSal = emp.Salary; | ||
|
||
// Act | ||
vm.RaiseSalaryCommand.Execute(emp); | ||
|
||
// Assert | ||
Assert.Equal(oldSal, emp.Salary); | ||
} | ||
|
||
[Fact] | ||
public void DeleteNo() { | ||
// Arrange | ||
var vm = new MainViewModel(new DebugLogger()); | ||
|
||
Messenger.Default.Register<AsyncYesNoMessage>(this, m => { | ||
m.Reply(false); | ||
}); | ||
|
||
// Act | ||
vm.DeleteCommand.Execute(vm.Employees[1]); | ||
|
||
Messenger.Default.Unregister<AsyncYesNoMessage>(this); | ||
|
||
// Assert | ||
Assert.Equal(3, vm.Employees.Count); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Application x:Class="MvvmDemoWpf.App" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:local="clr-namespace:MvvmDemoWpf" | ||
StartupUri="Views/Windows/MainWindow.xaml"> | ||
<Application.Resources> | ||
|
||
</Application.Resources> | ||
</Application> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Toolkit.Mvvm.DependencyInjection; | ||
using Microsoft.Toolkit.Mvvm.Messaging; | ||
using MvvmDemo.Messages; | ||
using MvvmDemo.Services; | ||
using MvvmDemo.ViewModels; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Configuration; | ||
using System.Data; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using System.Windows; | ||
|
||
namespace MvvmDemoWpf { | ||
/// <summary> | ||
/// Interaction logic for App.xaml | ||
/// </summary> | ||
public partial class App : Application { | ||
|
||
public App() { | ||
Ioc.Default.ConfigureServices(services => { | ||
services.AddSingleton<ILogger, DebugLogger>(); | ||
services.AddSingleton<MainViewModel>(); | ||
}); | ||
|
||
Messenger.Default.Register<AsyncYesNoMessage>(this, m => { | ||
|
||
Task<bool> GetResult() { | ||
var result = MessageBox.Show(m.Text, "Confirm", MessageBoxButton.YesNo ); | ||
return Task.FromResult(result == MessageBoxResult.Yes); | ||
} | ||
|
||
m.Reply(GetResult()); | ||
}); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Windows; | ||
|
||
[assembly: ThemeInfo( | ||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located | ||
//(used if a resource is not found in the page, | ||
// or application resource dictionaries) | ||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located | ||
//(used if a resource is not found in the page, | ||
// app, or any theme specific resource dictionaries) | ||
)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop"> | ||
|
||
<PropertyGroup> | ||
<OutputType>WinExe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<UseWPF>true</UseWPF> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\MvvmDemoLibrary\MvvmDemoLibrary.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<Window x:Class="MvvmDemoWpf.Views.Windows.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:local="clr-namespace:MvvmDemoWpf" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:vm="clr-namespace:MvvmDemo.ViewModels;assembly=MvvmDemoLibrary" | ||
Title="MainWindow" | ||
Width="800" | ||
FontSize="20" | ||
Height="450" | ||
DataContext="{x:Static vm:MainViewModel.Current}" | ||
mc:Ignorable="d"> | ||
<Grid Margin="4"> | ||
<Grid.RowDefinitions> | ||
<RowDefinition Height="Auto" /> | ||
<RowDefinition Height="1*" /> | ||
</Grid.RowDefinitions> | ||
<StackPanel Orientation="Horizontal"> | ||
<Button Command="{Binding RaiseSalaryCommand, Mode=OneTime}" | ||
CommandParameter="{Binding ElementName=listViewEmployees, Path=SelectedItem, Mode=OneWay}" | ||
Margin="4,0" | ||
Content="Raise Salary" /> | ||
<Button Command="{Binding DeleteCommand, Mode=OneTime}" | ||
CommandParameter="{Binding ElementName=listViewEmployees, Path=SelectedItem, Mode=OneWay}" | ||
Content="Delete" /> | ||
</StackPanel> | ||
<ListBox x:Name="listViewEmployees" | ||
Margin="4" | ||
SelectionChanged="ListViewEmployees_SelectionChanged" | ||
Grid.Row="1" | ||
ItemsSource="{Binding Employees, Mode=OneTime}"> | ||
<ListBox.ItemTemplate> | ||
<DataTemplate> | ||
<StackPanel Margin="0,4"> | ||
<TextBlock FontWeight="Bold" | ||
Text="{Binding Name, Mode=OneTime}" /> | ||
<TextBlock Text="{Binding Salary, Mode=OneWay}" /> | ||
</StackPanel> | ||
</DataTemplate> | ||
</ListBox.ItemTemplate> | ||
</ListBox> | ||
</Grid> | ||
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using MvvmDemo.ViewModels; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using System.Windows.Data; | ||
using System.Windows.Documents; | ||
using System.Windows.Input; | ||
using System.Windows.Media; | ||
using System.Windows.Media.Imaging; | ||
using System.Windows.Navigation; | ||
using System.Windows.Shapes; | ||
|
||
namespace MvvmDemoWpf.Views.Windows { | ||
/// <summary> | ||
/// Interaction logic for MainWindow.xaml | ||
/// </summary> | ||
public partial class MainWindow : Window { | ||
public MainWindow() { | ||
InitializeComponent(); | ||
} | ||
|
||
private void ListViewEmployees_SelectionChanged(object sender, SelectionChangedEventArgs e) { | ||
var vm = MainViewModel.Current; | ||
vm.RaiseSalaryCommand.NotifyCanExecuteChanged(); | ||
vm.DeleteCommand.NotifyCanExecuteChanged(); | ||
} | ||
} | ||
} |
Oops, something went wrong.