Skip to content

Commit 31c8005

Browse files
authored
Merge pull request #806 from RudolfJan/WpfTutorial2
Added the source code for the Wpf Tutorial.
2 parents 872033b + 5b90dc2 commit 31c8005

19 files changed

+576
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31612.314
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Caliburn.Micro.Tutorial.Wpf", "Caliburn.Micro.Tutorial.Wpf\Caliburn.Micro.Tutorial.Wpf.csproj", "{91C89E27-E316-4117-85D0-5B7DA2AC3939}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{91C89E27-E316-4117-85D0-5B7DA2AC3939}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{91C89E27-E316-4117-85D0-5B7DA2AC3939}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{91C89E27-E316-4117-85D0-5B7DA2AC3939}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{91C89E27-E316-4117-85D0-5B7DA2AC3939}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {7EFDF5AC-4D67-47F0-BA20-395AC9E897F9}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Application x:Class="Caliburn.Micro.Tutorial.Wpf.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="clr-namespace:Caliburn.Micro.Tutorial.Wpf">
5+
<Application.Resources>
6+
<ResourceDictionary>
7+
<ResourceDictionary.MergedDictionaries>
8+
<ResourceDictionary>
9+
<local:Bootstrapper x:Key="Bootstrapper" />
10+
</ResourceDictionary>
11+
</ResourceDictionary.MergedDictionaries>
12+
</ResourceDictionary>
13+
</Application.Resources>
14+
</Application>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Configuration;
4+
using System.Data;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
9+
namespace Caliburn.Micro.Tutorial.Wpf
10+
{
11+
/// <summary>
12+
/// Interaction logic for App.xaml
13+
/// </summary>
14+
public partial class App : Application
15+
{
16+
}
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Windows;
2+
3+
[assembly: ThemeInfo(
4+
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
5+
//(used if a resource is not found in the page,
6+
// or application resource dictionaries)
7+
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
8+
//(used if a resource is not found in the page,
9+
// app, or any theme specific resource dictionaries)
10+
)]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System.Collections.Generic;
2+
using System;
3+
using System.Linq;
4+
using System.Windows;
5+
using Caliburn.Micro.Tutorial.Wpf.ViewModels;
6+
using System.Diagnostics;
7+
8+
namespace Caliburn.Micro.Tutorial.Wpf
9+
{
10+
public class Bootstrapper: BootstrapperBase
11+
{
12+
private readonly SimpleContainer _container = new SimpleContainer();
13+
14+
public Bootstrapper()
15+
{
16+
Initialize();
17+
StartDebugLogger();
18+
}
19+
20+
// [Conditional("DEBUG")] You can use this conditional starting with C# 9.0
21+
public static void StartDebugLogger()
22+
{
23+
LogManager.GetLog = type => new DebugLog(type);
24+
}
25+
26+
protected override void Configure()
27+
{
28+
_container.Instance(_container);
29+
_container
30+
.Singleton<IWindowManager, WindowManager>()
31+
.Singleton<IEventAggregator, EventAggregator>();
32+
33+
foreach(var assembly in SelectAssemblies())
34+
{
35+
assembly.GetTypes()
36+
.Where(type => type.IsClass)
37+
.Where(type => type.Name.EndsWith("ViewModel"))
38+
.ToList()
39+
.ForEach(viewModelType => _container.RegisterPerRequest(
40+
viewModelType, viewModelType.ToString(), viewModelType));
41+
}
42+
}
43+
44+
protected override async void OnStartup(object sender, StartupEventArgs e)
45+
{
46+
var c= IoC.Get<SimpleContainer>();
47+
await DisplayRootViewForAsync(typeof(ShellViewModel));
48+
}
49+
50+
protected override object GetInstance(Type service, string key)
51+
{
52+
return _container.GetInstance(service, key);
53+
}
54+
55+
protected override IEnumerable<object> GetAllInstances(Type service)
56+
{
57+
return _container.GetAllInstances(service);
58+
}
59+
60+
protected override void BuildUp(object instance)
61+
{
62+
_container.BuildUp(instance);
63+
}
64+
}
65+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
<UseWPF>true</UseWPF>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Caliburn.Micro" Version="4.0.173" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Diagnostics;
3+
4+
namespace Caliburn.Micro.Tutorial.Wpf
5+
{
6+
class DebugLogger : ILog
7+
8+
{
9+
private readonly Type _type;
10+
11+
public DebugLogger(Type type)
12+
{
13+
_type = type;
14+
}
15+
16+
public void Info(string format, params object[] args)
17+
{
18+
if (format.StartsWith("No bindable"))
19+
return;
20+
if (format.StartsWith("Action Convention Not Applied"))
21+
return;
22+
Debug.WriteLine("INFO: " + format, args);
23+
}
24+
25+
public void Warn(string format, params object[] args)
26+
{
27+
Debug.WriteLine("WARN: " + format, args);
28+
}
29+
30+
public void Error(Exception exception)
31+
{
32+
Debug.WriteLine("ERROR: {0}\n{1}", _type.Name, exception);
33+
}
34+
}
35+
}
36+
37+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Caliburn.Micro.Tutorial.Wpf.Models
2+
{
3+
public class AboutModel
4+
{
5+
public string Title { get; set; } ="Caliburn.Micro Tutorial";
6+
public string Version { get; set; } ="1.0";
7+
public string Author { get; set; } = "Rudolf";
8+
public string Url { get; set; } = "https://caliburnmicro.com/";
9+
}
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Caliburn.Micro.Tutorial.Wpf.Models
2+
{
3+
public class CategoryModel
4+
{
5+
public string CategoryName { get; set; }
6+
public string CategoryDescription { get; set; }
7+
}
8+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Caliburn.Micro.Tutorial.Wpf.Models;
2+
using System.Threading.Tasks;
3+
4+
namespace Caliburn.Micro.Tutorial.Wpf.ViewModels
5+
{
6+
public class AboutViewModel: Screen
7+
{
8+
private AboutModel _aboutData = new AboutModel();
9+
10+
public AboutModel AboutData
11+
{
12+
get { return _aboutData; }
13+
}
14+
15+
public Task CloseForm()
16+
{
17+
return TryCloseAsync();
18+
}
19+
}
20+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
using Caliburn.Micro.Tutorial.Wpf.Models;
2+
3+
namespace Caliburn.Micro.Tutorial.Wpf.ViewModels
4+
{
5+
public class CategoryViewModel : Screen
6+
{
7+
private BindableCollection<CategoryModel> _categoryList = new BindableCollection<CategoryModel>();
8+
private CategoryModel _selectedCategoryModel;
9+
private string _categoryName;
10+
private string _categoryDescription;
11+
12+
public BindableCollection<CategoryModel> CategoryList
13+
{
14+
get
15+
{
16+
return _categoryList;
17+
}
18+
set
19+
{
20+
_categoryList = value;
21+
}
22+
}
23+
24+
public CategoryModel SelectedCategory
25+
{
26+
get
27+
{
28+
return _selectedCategoryModel;
29+
}
30+
31+
set
32+
{
33+
_selectedCategoryModel = value;
34+
NotifyOfPropertyChange(nameof(SelectedCategory));
35+
NotifyOfPropertyChange(nameof(CanEdit));
36+
NotifyOfPropertyChange(nameof(CanDelete));
37+
}
38+
}
39+
40+
public string CategoryName
41+
{
42+
get => _categoryName; set
43+
{
44+
_categoryName = value;
45+
NotifyOfPropertyChange(nameof(CategoryName));
46+
NotifyOfPropertyChange(nameof(CanSave));
47+
}
48+
}
49+
50+
public string CategoryDescription
51+
{
52+
get => _categoryDescription; set
53+
{
54+
_categoryDescription = value;
55+
NotifyOfPropertyChange(nameof(CategoryDescription));
56+
}
57+
}
58+
59+
protected override void OnViewLoaded(object view)
60+
{
61+
base.OnViewLoaded(view);
62+
CategoryList.Add(new CategoryModel { CategoryName = "Meals", CategoryDescription = "Lunches and diners" });
63+
CategoryList.Add(new CategoryModel { CategoryName = "Representation", CategoryDescription = "Gifts for our customers" });
64+
}
65+
66+
67+
public bool CanEdit
68+
{
69+
get
70+
{
71+
return SelectedCategory != null;
72+
}
73+
}
74+
75+
public void Edit()
76+
{
77+
CategoryName = SelectedCategory.CategoryName;
78+
CategoryDescription = SelectedCategory.CategoryDescription;
79+
}
80+
81+
public bool CanDelete
82+
{
83+
get
84+
{
85+
return SelectedCategory != null;
86+
}
87+
}
88+
89+
public void Delete()
90+
{
91+
CategoryList.Remove(SelectedCategory);
92+
Clear();
93+
}
94+
95+
public bool CanSave
96+
{
97+
get
98+
{
99+
return CategoryName?.Length > 2;
100+
}
101+
}
102+
103+
public void Save()
104+
{
105+
CategoryModel newCategory = new CategoryModel();
106+
newCategory.CategoryName = CategoryName;
107+
newCategory.CategoryDescription = CategoryDescription;
108+
if (SelectedCategory != null)
109+
{
110+
// remove the existing category, needed to update the view
111+
CategoryList.Remove(SelectedCategory);
112+
}
113+
CategoryList.Add(newCategory);
114+
Clear();
115+
}
116+
117+
public void Clear()
118+
{
119+
CategoryName = string.Empty;
120+
CategoryDescription = string.Empty;
121+
SelectedCategory = null;
122+
}
123+
}
124+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
4+
namespace Caliburn.Micro.Tutorial.Wpf.ViewModels
5+
{
6+
public class ShellViewModel : Conductor<object>
7+
{
8+
private readonly IWindowManager _windowManager;
9+
10+
public ShellViewModel(IWindowManager windowManager)
11+
{
12+
_windowManager= windowManager;
13+
}
14+
15+
public bool CanFileMenu
16+
{
17+
get
18+
{
19+
return false;
20+
}
21+
}
22+
protected async override void OnViewLoaded(object view)
23+
{
24+
base.OnViewLoaded(view);
25+
await EditCategories();
26+
}
27+
28+
public Task EditCategories()
29+
{
30+
var viewmodel = IoC.Get<CategoryViewModel>();
31+
return ActivateItemAsync(viewmodel, new CancellationToken());
32+
}
33+
34+
public Task About()
35+
{
36+
return _windowManager.ShowDialogAsync(IoC.Get<AboutViewModel>());
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)