-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewModelLocator.cs
67 lines (58 loc) · 2.3 KB
/
ViewModelLocator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using Autofac;
using Autofac.Extras.CommonServiceLocator;
using GalaSoft.MvvmLight.Views;
using Microsoft.Practices.ServiceLocation;
using Repsaj.Bots.Domoticz.Logic.Domoticz;
using Repsaj.Bots.Domoticz.Logic.Logging;
using Repsaj.Bots.Domoticz.App.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Repsaj.Bots.Domoticz.Logic.RequestHandler;
namespace Repsaj.Bots.Domoticz.App
{
public class ViewModelLocator
{
private readonly IContainer _container;
public ViewModelLocator()
{
var builder = new ContainerBuilder();
// Register the navigation service
var nav = new NavigationService();
builder.RegisterInstance<INavigationService>(nav);
// Register the settings object
var settings = new DomoticzSettingsService();
builder.RegisterInstance<IDomoticzSettingsService>(settings);
// Register types
builder.RegisterType<Services.DialogService>().As<IDialogService>().SingleInstance();
builder.RegisterType<ApiConnector>().As<IApiConnector>().SingleInstance();
builder.RegisterType<RequestHandler>().As<IRequestHandler>().SingleInstance();
builder.RegisterType<DomoticzManager>().As<IDomoticzManager>().SingleInstance();
// Register ViewModels
builder.RegisterType<MainPageViewModel>().SingleInstance();
try
{
// Perform registrations and build the container.
_container = builder.Build();
}
catch (Exception ex)
{
LogEventSource.Log.Error($"Could not compile the Autofac container: {ex}");
}
AutofacServiceLocator csl = new AutofacServiceLocator(_container);
ServiceLocator.SetLocatorProvider(() => csl);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
"CA1822:MarkMembersAsStatic",
Justification = "This non-static member is needed for data binding purposes.")]
public MainPageViewModel MainPageViewModel
{
get
{
return _container.Resolve<MainPageViewModel>();
}
}
}
}