Quick Start - canton7/Stylet GitHub Wiki
Want to get up and running as quickly as possible? This is the right place!
NOTE: If you're looking for example applications, download the source code and look in the Samples folder.
The following instructions will set up a minimal skeleton project.
Note: This will not work if your project uses PackageReference for NuGet packages or if you are using VS2013 or earlier. Follow the "Manual Option" section below instead.
If you're new to Stylet (and you're running VS2015 or later), this is the easiest way to get started.
- Open Visual Studio, and create a new
WPF Application
project. - Open NuGet (right-click your project -> Manage NuGet Packages), and install the
Stylet.Start
package.
This will give you a working skeleton project.
When it has finished installing, uninstall Stylet.Start.
Happy coding!
For .NET Core projects, the quickest way to get started is by using dotnet new
with Stylet's template.
Open a command window where you want to create your new project, and install the Stylet templates using:
dotnet new -i Stylet.Templates
Then create a new project with:
dotnet new stylet -o MyStyletProject
(changing MyStyletProject
as appropriate).
If you don't want to use the Stylet.Start
package and would prefer to create your own skeleton project, follow the instructions in this section.
- Open Visual Studio, and create a new
WPF Application
project. - Open NuGet (right-click your project -> Manage NuGet Packages), and install the
Stylet
package.
First off, delete MainWindow.xaml
and MainWindow.xaml.cs/vb
. You won't be needing them.
Next, you'll need a root View and a ViewModel. The View has to be a Window
, but there are no other restrictions.
<Window x:Class="Stylet.Samples.Hello.RootView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<TextBlock>Hello, World</TextBlock>
</Window>
The ViewModel can be any old class (for now - you might want it to be a Screen or Conductor).
C# | VB.NET |
public class RootViewModel
{
} |
Public Class RootViewModel
End Class |
Next, you'll need a bootstrapper. For now, you don't need anything special - just something to identify your root ViewModel. Later, you'll be able to configure your IoC container here, as well as other application-level stuff.
C# | VB.NET |
public class Bootstrapper : Bootstrapper<RootViewModel>
{
} |
Public Class Bootstrapper
Inherits Bootstrapper(Of RootViewModel)
End Class |
Finally, this needs to be referenced as a resource in your App.xaml
. You'll need to remove the StartUri
attribute, and add xmlns
entries for Stylet and your own application. Finally, you'll need to add Stylet's ApplicationLoader
to the resources, and identify the bootstrapper you created above.
It should look something like this:
<Application x:Class="Stylet.Samples.Hello.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="https://github.com/canton7/Stylet"
xmlns:local="clr-namespace:Stylet.Samples.Hello">
<Application.Resources>
<s:ApplicationLoader>
<s:ApplicationLoader.Bootstrapper>
<local:Bootstrapper/>
</s:ApplicationLoader.Bootstrapper>
</s:ApplicationLoader>
</Application.Resources>
</Application>
That's it! Run that, and you should get a window with 'Hello World' in it.
It's worth noting that <s:ApplicationLoader>
above is a ResourceDictionary subclass.
This allows it to load in Stylet's built-in resources (see Screens and Conductors). You can choose not to load Stylet's resources like this:
<s:ApplicationLoader LoadStyletResources="False">
...
</s:ApplicationLoader>
If you want to add your own Resources / ResourceDictionaries to the Application, the simplest way is like this:
<Application.Resources>
<s:ApplicationLoader>
<s:ApplicationLoader.Bootstrapper>
<local:Bootstrapper/>
</s:ApplicationLoader.Bootstrapper>
<Style x:Key="MyResourceKey">
...
</Style>
<s:ApplicationLoader.MergedDictionaries>
<ResourceDictionary Source="MyResourceDictionary.xaml"/>
</s:ApplicationLoader.MergedDictionaries>
</s:ApplicationLoader>
</Application.Resources>
If this makes you uncomfortable for some reason, you can also do this:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<s:ApplicationLoader>
<s:ApplicationLoader.Bootstrapper>
<local:Bootstrapper/>
</s:ApplicationLoader.Bootstrapper>
</s:ApplicationLoader>
<ResourceDictionary Source="MyResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="MyResourceKey">
...
</Style>
</ResourceDictionary>
</Application.Resources>