Skip to content

Commit

Permalink
Merge pull request #806 from RudolfJan/WpfTutorial2
Browse files Browse the repository at this point in the history
Added the source code for the Wpf Tutorial.
  • Loading branch information
KasperSK authored Jun 14, 2022
2 parents 872033b + 5b90dc2 commit 31c8005
Show file tree
Hide file tree
Showing 19 changed files with 576 additions and 0 deletions.
25 changes: 25 additions & 0 deletions samples/tutorals/WPF/Wpf.Tutorial/Caliburn.Micro.Tutorial.App.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31612.314
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Caliburn.Micro.Tutorial.Wpf", "Caliburn.Micro.Tutorial.Wpf\Caliburn.Micro.Tutorial.Wpf.csproj", "{91C89E27-E316-4117-85D0-5B7DA2AC3939}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{91C89E27-E316-4117-85D0-5B7DA2AC3939}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{91C89E27-E316-4117-85D0-5B7DA2AC3939}.Debug|Any CPU.Build.0 = Debug|Any CPU
{91C89E27-E316-4117-85D0-5B7DA2AC3939}.Release|Any CPU.ActiveCfg = Release|Any CPU
{91C89E27-E316-4117-85D0-5B7DA2AC3939}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7EFDF5AC-4D67-47F0-BA20-395AC9E897F9}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Application x:Class="Caliburn.Micro.Tutorial.Wpf.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Caliburn.Micro.Tutorial.Wpf">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<local:Bootstrapper x:Key="Bootstrapper" />
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace Caliburn.Micro.Tutorial.Wpf
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}
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)
)]
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.Collections.Generic;
using System;
using System.Linq;
using System.Windows;
using Caliburn.Micro.Tutorial.Wpf.ViewModels;
using System.Diagnostics;

namespace Caliburn.Micro.Tutorial.Wpf
{
public class Bootstrapper: BootstrapperBase
{
private readonly SimpleContainer _container = new SimpleContainer();

public Bootstrapper()
{
Initialize();
StartDebugLogger();
}

// [Conditional("DEBUG")] You can use this conditional starting with C# 9.0
public static void StartDebugLogger()
{
LogManager.GetLog = type => new DebugLog(type);
}

protected override void Configure()
{
_container.Instance(_container);
_container
.Singleton<IWindowManager, WindowManager>()
.Singleton<IEventAggregator, EventAggregator>();

foreach(var assembly in SelectAssemblies())
{
assembly.GetTypes()
.Where(type => type.IsClass)
.Where(type => type.Name.EndsWith("ViewModel"))
.ToList()
.ForEach(viewModelType => _container.RegisterPerRequest(
viewModelType, viewModelType.ToString(), viewModelType));
}
}

protected override async void OnStartup(object sender, StartupEventArgs e)
{
var c= IoC.Get<SimpleContainer>();
await DisplayRootViewForAsync(typeof(ShellViewModel));
}

protected override object GetInstance(Type service, string key)
{
return _container.GetInstance(service, key);
}

protected override IEnumerable<object> GetAllInstances(Type service)
{
return _container.GetAllInstances(service);
}

protected override void BuildUp(object instance)
{
_container.BuildUp(instance);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Caliburn.Micro" Version="4.0.173" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Diagnostics;

namespace Caliburn.Micro.Tutorial.Wpf
{
class DebugLogger : ILog

{
private readonly Type _type;

public DebugLogger(Type type)
{
_type = type;
}

public void Info(string format, params object[] args)
{
if (format.StartsWith("No bindable"))
return;
if (format.StartsWith("Action Convention Not Applied"))
return;
Debug.WriteLine("INFO: " + format, args);
}

public void Warn(string format, params object[] args)
{
Debug.WriteLine("WARN: " + format, args);
}

public void Error(Exception exception)
{
Debug.WriteLine("ERROR: {0}\n{1}", _type.Name, exception);
}
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Caliburn.Micro.Tutorial.Wpf.Models
{
public class AboutModel
{
public string Title { get; set; } ="Caliburn.Micro Tutorial";
public string Version { get; set; } ="1.0";
public string Author { get; set; } = "Rudolf";
public string Url { get; set; } = "https://caliburnmicro.com/";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Caliburn.Micro.Tutorial.Wpf.Models
{
public class CategoryModel
{
public string CategoryName { get; set; }
public string CategoryDescription { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Caliburn.Micro.Tutorial.Wpf.Models;
using System.Threading.Tasks;

namespace Caliburn.Micro.Tutorial.Wpf.ViewModels
{
public class AboutViewModel: Screen
{
private AboutModel _aboutData = new AboutModel();

public AboutModel AboutData
{
get { return _aboutData; }
}

public Task CloseForm()
{
return TryCloseAsync();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using Caliburn.Micro.Tutorial.Wpf.Models;

namespace Caliburn.Micro.Tutorial.Wpf.ViewModels
{
public class CategoryViewModel : Screen
{
private BindableCollection<CategoryModel> _categoryList = new BindableCollection<CategoryModel>();
private CategoryModel _selectedCategoryModel;
private string _categoryName;
private string _categoryDescription;

public BindableCollection<CategoryModel> CategoryList
{
get
{
return _categoryList;
}
set
{
_categoryList = value;
}
}

public CategoryModel SelectedCategory
{
get
{
return _selectedCategoryModel;
}

set
{
_selectedCategoryModel = value;
NotifyOfPropertyChange(nameof(SelectedCategory));
NotifyOfPropertyChange(nameof(CanEdit));
NotifyOfPropertyChange(nameof(CanDelete));
}
}

public string CategoryName
{
get => _categoryName; set
{
_categoryName = value;
NotifyOfPropertyChange(nameof(CategoryName));
NotifyOfPropertyChange(nameof(CanSave));
}
}

public string CategoryDescription
{
get => _categoryDescription; set
{
_categoryDescription = value;
NotifyOfPropertyChange(nameof(CategoryDescription));
}
}

protected override void OnViewLoaded(object view)
{
base.OnViewLoaded(view);
CategoryList.Add(new CategoryModel { CategoryName = "Meals", CategoryDescription = "Lunches and diners" });
CategoryList.Add(new CategoryModel { CategoryName = "Representation", CategoryDescription = "Gifts for our customers" });
}


public bool CanEdit
{
get
{
return SelectedCategory != null;
}
}

public void Edit()
{
CategoryName = SelectedCategory.CategoryName;
CategoryDescription = SelectedCategory.CategoryDescription;
}

public bool CanDelete
{
get
{
return SelectedCategory != null;
}
}

public void Delete()
{
CategoryList.Remove(SelectedCategory);
Clear();
}

public bool CanSave
{
get
{
return CategoryName?.Length > 2;
}
}

public void Save()
{
CategoryModel newCategory = new CategoryModel();
newCategory.CategoryName = CategoryName;
newCategory.CategoryDescription = CategoryDescription;
if (SelectedCategory != null)
{
// remove the existing category, needed to update the view
CategoryList.Remove(SelectedCategory);
}
CategoryList.Add(newCategory);
Clear();
}

public void Clear()
{
CategoryName = string.Empty;
CategoryDescription = string.Empty;
SelectedCategory = null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Threading;
using System.Threading.Tasks;

namespace Caliburn.Micro.Tutorial.Wpf.ViewModels
{
public class ShellViewModel : Conductor<object>
{
private readonly IWindowManager _windowManager;

public ShellViewModel(IWindowManager windowManager)
{
_windowManager= windowManager;
}

public bool CanFileMenu
{
get
{
return false;
}
}
protected async override void OnViewLoaded(object view)
{
base.OnViewLoaded(view);
await EditCategories();
}

public Task EditCategories()
{
var viewmodel = IoC.Get<CategoryViewModel>();
return ActivateItemAsync(viewmodel, new CancellationToken());
}

public Task About()
{
return _windowManager.ShowDialogAsync(IoC.Get<AboutViewModel>());
}
}
}
Loading

0 comments on commit 31c8005

Please sign in to comment.