Skip to content

Latest commit

 

History

History
126 lines (91 loc) · 4.51 KB

README.md

File metadata and controls

126 lines (91 loc) · 4.51 KB

scriptcs-wpf

About

This is a Script Pack for scriptcs that allows you to script WPF applications fast and easy 😄

Scripting a WPF application requires you to write lots of boilerplate code:

  • A WPF application requires several references to assemblies and importing of namespaces
  • The application and all XAML resources must be loaded and run on a Single Thread Apartment thread.
  • Since XAML resources aren't compiled (no generated partial/codebehind), they have to be dynamically loaded at runtime.

This Script Pack is created to assist you with these tasks, help you get rid of boilerplate code and get on with the scripting! 😎

Installation

To intall this Script Pack, simply navigate to your script folder and run scriptcs -install ScriptCs.Wpf. 🤘

Usage

Using the pack in your scripts is as easy as 🍰! After it has been installed, it will automatically be loaded (from the bin folder).

The pack will reference the following assemblies:

  • WindowsBase
  • PresentationCore
  • PresentationFramework
  • System.Xaml
  • System.Xml

And import the following namespaces:

  • System.ComponentModel
  • System.Diagnostics
  • System.Threading
  • System.Windows
  • System.Windows.Input

To use the packs utility methods in your scripts, use Require<Wpf> to get the Script Pack context.

The context has several handy utility methods:

void RunApplication<TViewModel>()
void RunApplication<TViewModel>(TViewModel viewModel)
void RunApplication<TViewModel>(string xamlFile)
void RunApplication<TViewModel>(string xamlFile, TViewModel viewModel)
void RunApplication(string xamlFile)
void RunInSTA(Action action)
DependencyObject LoadXaml(string xamlFile)

LoadXaml and RunInSTA

LoadXaml loads the given a XAML resource as a DependencyObject.
RunInSTA wraps and invokes the given action in an STA thread. This method will block until the application is terminated.

The following is a more verbose script that shows the usage of the LoadXaml and RunInSTA methods:

public class MyApplication : Application
{
    private readonly Window _mainWindow;
 
    public MyApplication(Window mainWindow)
    {
        _mainWindow = mainWindow;
    }
 
    protected override void OnStartup(StartupEventArgs e)
    {
        _mainWindow.Show();
    }
}
 
var wpf = Require<Wpf>(); // Gets the WPF Script Pack context
wpf.RunInSTA(() =>
{
  // Load the View from XAML
  var view = wpf.LoadXaml("CalculatorView.xaml");
  
  // Set the ViewModel
  view.DataContext = new CalculatorViewModel();
 
  // Create a new Window
  var mainWindow = new Window { Content = view, SizeToContent = SizeToContent.WidthAndHeight };
  
  // Create a new Application
  var application = new MyApplication(mainWindow);
  
  // Run the Application
  application.Run();
});

RunApplication

The RunApplication methods will take care of creating an Application object for you.
In addition to that, they will load the XAML View, set its DataContext, create a Window and run the application, all in an STA thread ☀️

The two methods that don't take a string will look for a XAML file (View) for the given ViewModel type by convention.
This is a super simple convention that will look for a XAML file in the script directory with the same name as the ViewModel, but without the 'Model' at the end.

CalculatorViewModel -> CalculatorView.xaml

var wpf = Require<Wpf>();
wpf.RunApplication("CalculatorView.xaml", new CalculatorViewModel());

wpf.RunApplication<CalculatorViewModel>("CalculatorView.xaml");

wpf.RunApplication(new CalculatorViewModel()); // Uses convention to find View for ViewModel
 
wpf.RunApplication<CalculatorViewModel>();  // Uses convention to find View for ViewModel

wpf.RunApplication("CalculatorView.xaml") // No ViewModel

Sample

For a working sample application, please check out the sample folder...

Bugs and Feature Requests

If you find any bugs or would like to see new features/changes in this Script Pack, please let me know by filing an issue or sending a pull request 😁

Did you count how many times you read the words "script" and "pack" in this readme? 😝