sugarontop2007-09-27


XAMLを動的にロードすることで、いわゆるリッチクライアントアプリケーションを作ることができそう。


開発環境:

  1. VisualStudio2005
  2. Expression Blend2 (usa, September Preview)

この環境で以下のような簡単なプログラムを作成してみた。
.NETのランタイムはC:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0以下を指定。

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;

namespace ConsoleApplication1
{
class Program
{
[STAThread]
static void Main(string[] args)
{
Stream st = File.OpenRead( @"E:\work_blend\test\Window1.xaml" );
Window mainwnd = (Window)XamlReader.Load( st );
mainwnd.Height = 480;
mainwnd.Width = 640;
mainwnd.Title = "hoi";


Button btn = (Button)mainwnd.FindName( "button1" );
btn.Click += new RoutedEventHandler(btn_Click);

Application ap = new Application();
ap.Run( mainwnd );

}

static void btn_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show( "ok" );
}
}
}

上で参照しているWindow1.xamlLファイルはBLENDで編集作成した。ただし、x:Classのタグはエラーになるので手で削除。


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

x:Name="Window"
Title="Window1"
Width="640" Height="480">






























画面をロードできても、コードビハインドはロードできない。(できたら、セキュリティ的に問題だし。)
ここを対処するには専用アプリを作成する必要がる。まあ、ブラウザのような感じ。なので、各クライアントへのインストール作業は避けられない。
上の絵が作成したアプリ。