Rob Relyea

xaml:Blog

April 2005 - Posts

  • 3 Coding Styles for Avalon Applications

    Filed under: ,

    There are 3 coding styles developers could use to build Avalon applications:

    1.      Code Only

    2.      Code + Markup (Runtime use of XamlReader)

    3.      Code + Markup (Markup compiled)***

     

    *** - Microsoft Development tools will recommend this style.

      

    Coding Style 1: Code Only Application

    Any CLR compatible language can write a code only application.

     

    Example in C#:

    1) Create Program.cs

    using System;

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Markup;

    using System.IO;

     

    namespace CodingStyles

    {

        public class CodeOnlyWindow : Window

        {

            [STAThread]

            static void Main(string[] args)

            {

                Application app = new Application();

                new CodeOnlyWindow().Show();

                app.Run();

            }

     

            Button button1;

     

            public CodeOnlyWindow()

            {

                StackPanel sp = new StackPanel();

                button1 = new Button();

                button1.Content = "Hello";

                sp.Children.Add(button1);

                button1.Click += new RoutedEventHandler(button1_Click);

                this.Content = sp;

                this.Width = this.Height = 285;

                this.Left = this.Top = 100;

                this.Text = "Code Only Window";

     

            }

            void button1_Click(object sender, RoutedEventArgs e)

            {

                button1.Content = "Clicked";

            }

     

        }

    }

     

    2) Compile that with the c# compiler like this:

    csc /out:CodingStyle1.exe Program.cs /lib:c:\Program Files\Reference Assemblies\Microsoft\WinFx\v3.0 /r:PresentationFramework.dll;PresentationCore.dll;WindowsBase.dll;UIAutomationTypes.dll

     

    3) Run CodingStyle1.exe

     

    Coding Style 2: Code + Markup (Runtime use of Xaml Parser)

    Some developers will want a code only application, but will still want to use some xaml.  These developers can call the parser to load xaml at runtime. 

    Any CLR compatible language will be able to do this.

     

    Example in C#:
    1) Create Program.cs

    using System;

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Markup;

    using System.IO;

     

    namespace CodingStyles

    {

        public class CodePlusRuntimeParsingWindow: Window

        {

            [STAThread]

            static void Main(string[] args)

            {

                Application app = new Application();

                new CodePlusRuntimeParsingWindow ().Show();

                app.Run();

            }

     

            Button button2;

     

     

            public CodePlusRuntimeParsingWindow()

            {

                //Load file1.xaml

                StreamReader sr = new StreamReader("file1.xaml");

                DependencyObject rootElement =

                    XamlReader.Load(sr.BaseStream) as DependencyObject;

     

                //Find element with ID of Button2

                button2 = LogicalTreeHelper.FindLogicalNode(rootElement, "button2")

                   as Button;

     

                //Wire up event

                button2.Click += new RoutedEventHandler(button2_Click);

     

                this.Content = rootElement;

                this.Text = "Code Plus Runtime Parsing Window";

            }

     

            void button2_Click(object sender, RoutedEventArgs e)

            {

                button2.Content = "Clicked";

            }

     

        }

    }

     

    2) Create File1.xaml

    <StackPanel xmlns="http://schemas.microsoft.com/winfx/avalon/2005">

          <Button ID="button2">HelloButton>

    StackPanel>

     

    Compile that with the c# compiler like this:

    3) csc /out:CodingStyle2.exe Program.cs /lib::\Program Files\Reference Assemblies\Microsoft\WinFx\v3.0 /r:PresentationFramework.dll;PresentationCore.dll;WindowsBase.dll;UIAutomationTypes.dll

     

    4) Run CodingStyle2.exe  (it should work identically to CodingStyle1.exe)

     

     

    Coding Style 3: Code + Markup (Markup compiled)

    A subset of CLR compatible languages will support this mode.

    A language must have a CodeDom provider and provide msbuild target files for their language.

     

    Microsoft languages that meet this criteria today are C#, Visual Basic and J#.  (J# has a few blocking bugs in currently available CTPs though).

    I'm aware of at least two other language vendors who will be able to match this level of support in the near future.

     

    More details about MarkupCompilation are in a blog post I did last year:  

     

    1) Use Visual Studio – File/New Project/Avalon Application – call it CodingStyle3

    2) Change Window1.xaml to look like this

    <Window x:Class="CodingStyle3.Window1"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml

        Text="CodingStyle3"

        >

        <StackPanel>

                <Button Name="button1" Click="Button1_Click">HelloButton>

        StackPanel>

    Window>

     

    3) Change Window1.xaml.cs to look like this

    using System;

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Data;

    using System.Windows.Documents;

    using System.Windows.Media;

    using System.Windows.Shapes;

     

     

    namespace CodingStyle3

    {

        ///

        /// Interaction logic for Window1.xaml

        ///

     

        public partial class Window1 : Window

        {

            public Window1()

            {

                InitializeComponent();

            }

     

            private void button1_Click(object sender, RoutedEventArgs e)

            {

                button1.Content = "Clicked";

            }

     

        }

    }

     

    4) Build and Run the application in VS  (it should work identically to CodingStyle1.exe and CodingStyle2.exe)

     

    Benefits of Coding Style 3: Markup Compilation

    Markup compilation provides the following benefits:

    ·         Xaml code is used as the element tree definition language giving many tools (form designers, timeline editors, vector graphics editors, 3d modelers, etc…) the opportunity to collaborate. (this is a benefit for coding style 2 & 3)

    ·         ID Lookup is automatic.  Event handlers can refer to an element by ID instead of having to call LogicalTreeHelper.FindLogicalNode like CodingStyle2 does.

    ·         Event Wiring is automatic.  Coding style 1 & 2 both needed to attach an event handler to the click event of the button.

    ·         Xaml code is compiled into a binary form.  Loading via the binary form is faster than parsing xml at runtime.

     

     

    [3/27/2006 - updated to make sure object model and markup is up to date for Feb 2006 CTP.]

    PostTypeIcon
    20,345 Views
  • Audio/Videos online...haven't they heard of Tivo?

    Filed under:

    Mike Swanson has pointed towards a presentation that Karsten did at Flash Forward.

    I just started watching it...good talk.  I'm intrested to see how Karsten talked to designers...nice job so far...i'm 9minutes into it.

     

    Videos Should be Downloadable
    This made me think about streaming Audio/Video.

    Streaming is more conventient than TV...you can get it anytime, but you should be able to download most streams by default.

    If a publisher wants something to be live only or streaming only...they should be able to.

     

    Windows Media Player should make that happen!

    Update: To clarify my request... I'm about to drive to work...and would love to listen to the audio on the way.  We should make that easier for people to do...
    Update2: Ok, just for the heck of it, I tried to press play on my paused video on the way to work...  It worked!  WMP did most of what I wanted.  It proactively cached the whole download.  It doesn't let me save it to a file...and it didn't really make it easy for me to discover the fact that it was cached...but i'm happier than i was this morning...  thanks wmp team!

    PostTypeIcon
    1,374 Views
  • FAQ: Why doesn't Avalon/Indigo March CTP work with VS Beta2

    Filed under: ,

    Stevey asked a FAQ about why Avalon March CTP doesn't work with VS 2005 beta2.  I'll explain why here.

    First of all there are 3 players in this story.

    1. .Net Framework 2.0
    2. Avalon
    3. Visual Studio

    Avalon & VS have dependencies on .NET Framework
    Avalon's assemblies (PresentationFramework.dll, PresentationCore.dll, etc...) are compiled against a specific build of the .net framework.  A specific build of Avalon will only work with the build of the .net framework that it was compiled against.

    Same goes for Visual Studio...it requires a specific build of .Net Framework.

    Different Schedules
    The .Net Framework team and the Visual Studio team is on one ship schedule -- shipping .Net Framework 2.0 and VS 2005 this year.

    The Avalon and Indigo teams are on another ship schedule, shipping in 2006.

    Avalon and Indigo pick up periodic builds of the .Net Framework 2.0 every couple of months.  While .Net Framework 2.0 is being developed, there are many breaking changes between builds that we pick up, so this process takes work from several people on our team.  Scheduling when this happens depends on when a partner drop is done by the .Net team and when a non-disruptive time for the Avalon team to pick up the build is.

    Now that Avalon and Indigo are shipping CTPs together, we both need to coordinate which build of .Net Framework 2.0 we work on.

    Which Build Goes With Which Build

    Here is a brief table that shows build numbers for each of the WinFX CTPs.

    Avalon & Indigo
    CTP Releases

    Required .Net Framework Version

    Required Visual Studio Version

    November 2004

    V2.0.40607

    Beta 1 (8.0.40607)

    March 2005

    V2.0.50110

    Feb CTP

    May 2005 - Beta 1 RC

    V2.0.50215

    Beta 2

    Updated: build of Avalon/Indigo is out now in May...

     How to figure out which build of the .Net Framework 2.0 you have installed:

    Look in c:\windows\microsoft.net\framework for a folder that starts with "v2.0." 

    That folder name is the build of the .Net Framework you have installed on you machine.

     

    Only one build of .Net Framework 2.0 can be installed at a time

    It is not possible to have v2.0.50110 and v2.0.50215 at the same time.

    Because of that it is not possible to have an Avalon/Indigo CTP that requires one .Net Framework build and a Visual Studio build that requires another.

    Note: It is possible to have .NetFramework 1.0, 1.1, and 2.0 installed on the machine at one time.  It is also possible to install VS 2003 and VS 2005 on the same machine.

    The Quick Summary

    If you are focused on Avalon development, don't jump the gun and upgrade your Avalon development machine to the latest VS CTP unless a new Avalon and Indigo CTP has been released.

     

    I'll modify this post based on your feedback/questions…let me know what isn't clear.

    Update: clarified many places where I said ".Net Framework" to ".Net Framework 2.0".  Added the note about being able to install 1.0, 1.1, and 2.0 at the same time.

    PostTypeIcon
    10,614 Views