Click or drag to resize

HelloWPF

Show how to use wpf and xaml to create a user interface

This is a more complex example and should serve as a starting point if you want to make a more complex extension to Fast video cataloger.

If you are used to program WPF or other xaml based user interface there are a few key differences that is important to be aware of.

First, we are using a few extra external libraries. These are brought in with the //css_ref comments at the top of the file.

Since the scripts are really not compiled we need to load the xaml file that defines the user interface dynamically.

Since the xaml is really not compiled things like bindings will not automatically work so they need to be setup on the code side.

Example
XAML
<Page 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Foreground="Black"
        Background="White"
        UseLayoutRounding="True">

  <StackPanel>
    <StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
      <Label Foreground="Black">Enter message:</Label>
      <TextBox Width="100" Name="input1" Margin="10"/>
    </StackPanel>
    <Button Name="button1"
        Margin="10"
        Width="96"
        BorderThickness="2"
        Content="Click Me" />
  </StackPanel>
</Page>
C#
#region hello_wpf
//css_ref WindowsBase;
//css_ref PresentationCore;
//css_ref PresentationFramework;
//css_ref System.Runtime
//css_ref System.ObjectModel

using System;
using System.Xaml;
using System.Windows;
using System.IO;
using System.Windows.Controls;
using VideoCataloger;

namespace VideoCataloger
{

    public partial class HelloWindow : Window
    {
        DependencyObject m_RootElement;

        public HelloWindow()
        {
            // setup window properties
            Width = 320;
            Height = 200;
            Title = "Dynamic WPF window";

            // load the xaml code to be used as content for this window
            string current_folder = Directory.GetCurrentDirectory();
            string path = current_folder + "\\scripts\\HelloWPF\\hello_wpf.xaml";
            using (FileStream fs = new FileStream( path, FileMode.Open))
            {
                m_RootElement = (DependencyObject) System.Windows.Markup.XamlReader.Load(fs);
            }

            // setup events for elements in xaml
            Button btn = (Button)LogicalTreeHelper.FindLogicalNode(m_RootElement, "button1");
            btn.Click += button1_Click;

            // use the loaded scene
            this.Content = m_RootElement;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            TextBox textbox = (TextBox) LogicalTreeHelper.FindLogicalNode(m_RootElement, "input1");
            MessageBox.Show("Message is: " + textbox.Text);
        }



        [STAThread]
        public static void Main()
        {
            // not used as entry point for script but needed for compilation
        }
    }

    class Script
    {
        static public async System.Threading.Tasks.Task Run(IScripting scripting, string argument)
        {
            HelloWindow wnd = new HelloWindow();
            wnd.ShowDialog();
        }
    }

}
#endregion