Coder Social home page Coder Social logo

scriptcs-wpf's Introduction

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? ๐Ÿ˜

scriptcs-wpf's People

Contributors

khellang avatar

Stargazers

 avatar Jared Thirsk avatar smartcaveman avatar Daniel Krikun avatar Josรฉ Manuel Nieto avatar Jonas Eriksson avatar Erfan Noury avatar Glenn Block avatar Jarda Jirava avatar  avatar

Watchers

 avatar James Cloos avatar

scriptcs-wpf's Issues

Issue about execution

I downloaded the script in a test forler where I also added a start.csx file
whith your proposed code

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

The execution ended by generating this error

ERROR: Script compilation failed: C:\start\start.csx(2,47): error CS0246: The type or namespace name 'CalculatorViewModel' could not be found (are you missing a using directive or an assembly reference?).

I tried to verify the your scriptpack references it seems to be OK. However this error leads me to have a doubt about somme missing assembly references

Looking forward for an answer

Missing using directive or an assembly reference

I am only able to run your examples successfully with all 5 explicit assembly references at the top of the csx file.

For example, here's one of them:

#r "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.8\PresentationCore.dll"

If I don't reference them, I get errors like:

The type or namespace name Application could not be found (are you missing a using directive or an assembly reference?)

Any idea why?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.