Portable Class Libraries
and the MVVM pattern
Andreas Kuntner
 C# .NET Development
 Client, UI, Mobile development
 App and library publications
 Developer’s blog
http://www.mobilemotion.eu
office@mobilemotion.eu
Andreas Kuntner
 Platform-independent libraries
 Cross-platform applications
 Model / View / ViewModel
 The MVVM pattern in practice
 Beyond the MVVM pattern
 Project setup in practice
Agenda
 Platform-independent libraries
 Cross-platform applications
 Model / View / ViewModel
 The MVVM pattern in practice
 Beyond the MVVM pattern
 Project setup in practice
Agenda
encapsulate re-useable functionality (code snippets), to:
 avoid redundant code
 improve maintainability
 share libraries
.NET Class Libraries
.NET Class Libraries
SomeMethod()
SomeMethod()
.NET Class Libraries
SomeMethod()
.NET Class Libraries
?
Windows Runtime vs. .NET Framework
Portable Class Libraries
Portable Class Libraries
+ share functionalities between different platforms
‒ limit available APIs to intersection of all platforms
→Choose only relevant target platforms!
Portable Class Libraries
 Platform-independent libraries
 Cross-platform applications
 Model / View / ViewModel
 The MVVM pattern in practice
 Beyond the MVVM pattern
 Project setup in practice
Agenda
Cross-platform applications
Which parts of an App are portable?
User interface
Data processing
Data structure
Which parts of an App are portable?
User interface
Data processing
Data structure
Which parts of an App are portable?
User interface
Data processing
Data structure
Which parts of an App are portable?
System.Windows.Controls.Button
Windows.UI.Xaml.Controls.Button
Xamarin.Forms.Button
Which parts of an App are portable?
User interface
Data processing
Data structure
 Platform-independent libraries
 Cross-platform applications
 Model / View / ViewModel
 The MVVM pattern in practice
 Beyond the MVVM pattern
 Project setup in practice
Agenda
The MVVM pattern
User interface
Data processing
Data structure
The MVVM pattern
User interface:
Data processing:
Data structure:
View
Viewmodel
Model
The MVVM pattern
User interface:
Data processing:
Data structure:
binds to
references
View
Viewmodel
Model
The MVVM pattern
User interface:
Data processing:
Data structure:
View
Viewmodel
Model
Platformspecific
Portable
The MVVM pattern
Views
Viewmodels
Models
Views
Views
 No business logic in View code
→Redirect event handlers to Viewmodel-Commands
MVVM principles
 No business logic in View code
→Redirect event handlers to Viewmodel-Commands
 No references to View in Viewmodel code
→Communication between View and Viewmodel through
public properties and Commands
MVVM principles
 Platform-independent libraries
 Cross-platform applications
 Model / View / ViewModel
 The MVVM pattern in practice
 Beyond the MVVM pattern
 Project setup in practice
Agenda
An example app
 contains simple properties
 to be used by the Viewmodel
 to be shown in the View
 must notify its container when any property changes
The (Data) Model
The (Data) Model
public class NoteModel
{
public string Description { get; set; }
public bool IsNew { get; set; }
}
The (Data) Model
public class NoteModel : INotifyPropertyChanged
{
private string _description;
public string Description
{
get { return _description; }
set {
_description = value;
RaisePropertyChanged("Description");
}
}
#region INotifyPropertyChanged implementation
}
 aggregates data items
 contains business logic to be executed on UI events
The Viewmodel
The Viewmodel
public class MainViewmodel : INotifyPropertyChanged
{
private ObservableCollection<NoteModel> _notes;
public ObservableCollection<NoteModel> Notes
{
get { return _notes; }
set {
_notes = value;
RaisePropertyChanged("Notes");
}
}
...
The Viewmodel
...
public DelegateCommand AddNoteCommand { get; set; }
public MainViewmodel()
{
AddNoteCommand = new DelegateCommand(AddNote);
}
private void AddNote()
{
// add presentation logic here
}
}
 binds to a specific Viewmodel
 uses Data Bindings to
 display data to the user
 forward user-triggered events to the Viewmodel
The View
The View
<Window>
<Window.DataContext>
<viewmodels:MainViewmodel />
</Window.DataContext>
<Grid>
<ListBox ItemsSource=" {Binding Notes} " />
<Button Command=" {Binding AddNoteCommand} ">Neu</Button>
</Grid>
</Window>
 Platform-independent libraries
 Cross-platform applications
 Model / View / ViewModel
 The MVVM pattern in practice
 Beyond the MVVM pattern
 Project setup in practice
Agenda
What about business logic beyond data processing?
 Data retrieval from hardware sensors
 Communication with (web) servers
 Database operations
 File system interaction
 Complex calculations
→Business logic that does not belong in the Viewmodel
Beyond the MVVM pattern
Integration of Services
binds to
references
View
Model
Viewmodel Service
invokes
Platformspecific
Portable
Integration of Services
View
Model
Viewmodel
Service
Declaration of Service interfaces
PlatformspecificPortable
public interface IExampleService
{
int ServiceMethod(double input);
}
public class ExampleService : IExampleService
{
public int AnotherMethod(double input)
{
// add business logic here
}
}
A simple Service Locator
public class ServiceLocator
{
public static ISomeService SomeService { get; set; }
public static IAnotherService AnotherService { get; set; }
}
→Avoid static service references to create testable code
 Inject service locator instance into Viewmodel:
 Take a look at existing IoC containers
Service Locator considerations
<Window.DataContext>
<MainViewmodel Services="{StaticResource ServiceLocatorInstance}" />
</Window.DataContext>
 Platform-independent libraries
 Cross-platform applications
 Model / View / ViewModel
 The MVVM pattern in practice
 Beyond the MVVM pattern
 Project setup in practice
Agenda
 Divide every new MVVM project into portable and
platform-specific libraries
 Extract presentation layer to use MVVM for large
rich-client applications
 Implement MVVM on your own for the first project, then
decide for a MVVM framework
How to proceed?
 Sample application
http://www.mobilemotion.eu/downloads/ADC-demo.zip
 A simple MVVM framework
http://MVVMbasics.mobilemotion.eu
http://MVVMbasics.codeplex.com
 Visit my blog:
http://blog.mobilemotion.eu
Additional resources
QUESTIONS?
Thank you!
Andreas Kuntner
Any feedback is welcome!
Partner:

Portable Class Libraries and MVVM