何となく Blog by Jitta
Microsoft .NET 考

目次

Blog 利用状況
  • 投稿数 - 761
  • 記事 - 18
  • コメント - 35955
  • トラックバック - 222
ニュース
  • IE7以前では、表示がおかしい。div の解釈に問題があるようだ。
    IE8の場合は、「互換」表示を OFF にしてください。
  • 検索エンジンで来られた方へ:
    お望みの情報は見つかりましたか? よろしければ、コメント欄にどのような情報を探していたのか、ご記入ください。
It's ME!
  • はなおか じった
  • 世界遺産の近くに住んでます。
  • Microsoft MVP for Visual Developer ASP/ASP.NET 10, 2004 - 9, 2011
広告

記事カテゴリ

書庫

日記カテゴリ

ギャラリ

その他

わんくま同盟

同郷

 

前回のあらすじ。線を引くことができるようになった。アニメーションのさせ方も、何となくわかった。

今回の目標。ファイルからデータを読み込む。


さて、ファイル。ウェブを使うアプリケーションで、開発者が必ず意識しないといけないこと。「このコードは、クライアントとサーバ、どちらで動いているの?」これを意識しないと、「**の設定がとれない」とか、「**の設定を変えられない」とか、泣くことになる。

じゃぁ、Silverlight アプリケーションは、どこで動いているのか。

Visual Studio が作ったプロジェクトのディレクトリを見ると、「ClientBin」というディレクトリがある。ここに、プロジェクト名と同じ名前の DLL がある。また、TestPage.html というファイルがあり(どうしたマイクロソフト?拡張子が .html じゃないか!!)、デバッガはこいつを表示しているようだ。

この、TestPage.html をのぞいてみる。「SilverlightControlHost」という div 要素の中に、パラメータでプロジェクト名の DLL をソースとして指定している object 要素がある。プロジェクトを、「テスト用 HTML ファイルを作る」で作ると、.xap ファイルの他に .dll もできる。「Web サイト」で作ると、.xap だけが、テスト用サイトにコピーされる。.zap ファイルは、拡張子が .zap な ZIP ファイル。拡張子を変更して解凍してみるとよい。これが、Silverlight アプリケーションの実体だろう。IE にホストさせる Windows Forms のような形で動いているわけですね。どういう事?こういう事→Code Access Security and Distribution Features in .NET Enhance Client-Side Apps 日本語は、こちら→msdn magazine July 2002 No.28こいつを探すために、Insider.NET 会議室に 2002/8/8 に投稿した質問を引っ張り出してきたよ。

ということで、これで Silverlight アプリケーションとして書いている C# や VB のコードは、DLL などをまとめた XAP ファイルという形でクライアントに送られ、ブラウザ(あるいは、ブラウザのプラグイン)がホストになって実行している、ということがわかりました。.xap ファイルを、おそらくブラウザ プラグインとして実装された Silverlight ランタイムが受け取り、DLL になっているアセンブリを実行している...と、想像します。←なんか、「想像します」とか「思います」とかって、ちゃんとつけているのに「誰それが言っていることだから、そうだ」と決めつける人がいるみたいです。

ということは、前回のようにトライ アンド エラーでやるのは、ちょっときつそうです。観念して、英文を読むことにしますorz

目次では、[Microsoft Silverlight 2 SDK → Silverlight 2 Beta 1 - Development with the .NET Framework → Accessing Web Services in Silverlight] の下です。

おっとその前に、どんなアクセスをするのか、決めなくちゃ。

とりあえず簡単に、Silverlight アプリケーションを実行しているのと同じディレクトリ(サイト)にあるテキスト ファイルを読み込めればいい、とします。XML Web Service にアクセスするのが、Silverlight 側は簡単なのでしょうが、サーバ側にそういう実装ができるかどうかはまた別の問題なので。

で、ドキュメント。ざっとタイトルを眺めて、これかな?「Accessing HTTP-Based Services Directly」(HTTP ベースのサービスに直接アクセスする)。この下に、「Working with XML Data in Silverlight」(Silverlight で XML データを扱う)とあるので、これ。

ざっと読むと、HTTP リクエストによる、人が理解しやすい形のメタデータを扱う、らしい。で、「How To: Make Requests to HTTP-Based Services」(HTTP ベースのサービスへのリクエストを作成する方法)を読んで、「Working with XML Data in Silverlight」を読めば、何とかなりそう。なので、読む。



読んだ。翻訳しておいた

XML データを読む方法は3通りある(XQuery, XLINQ, シリアル化解除)が、ここではあまり工数をかけたくないので、知っている方法である、シリアル化解除を利用することにする。時間がかけられるなら、XLINQ がおもしろそう。


まず、URI を作成する。これは、Silverlight アプリケーションが置いてあるドメインか、サイトがとれれば良いんだけど...ってことで、またドキュメントにあたる。System.Net.WebClient.BaseAddress という、プロパティがある。たぶんこれ。だと思うけど、説明がなんもないよ?!こういうときは、やってみるしかない。

まず、IIS をインストールする。そして、Silverlight アプリケーションの ClientBin ディレクトリを、適当な名前で仮想ディレクトリとして登録する。http://localhost/silvertest/testpage.html につながるかどうか、テスト。………よし、OK!

前回、Page コンストラクタに追加したコードを、別の適当な名前のメソッドに追い出す。そして、今回のテスト用に、コードを追加する。

まず、XAML に、TextBlock を追加しておく。コードからアクセスするので、x:Name も、追加しておく。ここでは、"baseUri" にしておく。XAML を保存すると、裏で .g.cs ファイルが更新される。そうしないと、.cs ファイルでオブジェクトにアクセスできないので注意。


public Page() {
    InitializeComponent();
    baseUri.Text = new System.Net.WebClient().BaseAddress.AbsoluteUri;
}

すると、「http://localhost/silvertest/」と表示された。これで、出所がとれる。

本当に良いのかなぁ?なんか違うような気がするなぁ?でも、実行したら期待通りのものが出たから、よしとする。

これを元に、Uri を、次のように作る。


public Page() {
    InitializeComponent();
    System.Net.WebClient client = new System.Net.WebClient();
    string uri = String.Format("{0}warnigLog.xml", client.BaseAddress.AbsoluteUri);
}

次に、HttpWebRequest を作るわけですが、System.Net の下にそれがない。よくよく読んでいくと、アセンブリを参照しろ、と書いてあるorz というわけで、System.Net.dll と、System.Xml.Serialization.dll を、参照に追加する。

で、リクエストを作る。


using System.Net;
using System.Xml.Serialization;

public Page() {
    InitializeComponent();
    WebClient client = new WebClient();
    string uri = String.Format("{0}warnigLog.xml", client.BaseAddress.AbsoluteUri);
    textBox1.Text = uri; // TextArea だと、クリップするので、TextBox に変えた
    Uri logUri = new Uri(uri);
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(logUri);
    request.BeginGetResponse(new AsyncCallback(GetWarningLog), request);
}
private void GetWarningLog(IAsyncResult result) {
    // とりあえず用意
}

ここ、GetWarningLog メソッドの中で、HttpResponse にあるデータを復元するわけです。が、データをまだ作ってないです。



というわけで、次回に続く。。。


Tips 的なことを一つ。

Silverlight アプリケーションのデバッグについてです。

「デバッグ実行」で、一応いけますが、いちいち IE が起動するのが面倒だ、という方向け。IE を起動して、IIS 経由なり仮想サーバ経由で .html ファイルを表示しておきます。

デバッグの時は、VS2008 のメインメニューより、[デバッグ → プロセスにアタッチ] を選びます。

選択可能なプロセスの中に、iexplorer.exe が、2つあります。このうち、「型」のところに、「スクリプト, マネージ, x86」と書かれているものに、アタッチします。

その前に、[アタッチ先] の右にある [選択] ボタンをクリックして、[Silverlight] のチェックボックスに、チェックをしておきます。←これ、重要。[デバッグするコードの種類を自動的に判断する] だと、Silverlight アプリケーションがその他のアタッチ可能なものと互換性がないらしく、デバッグできません。

投稿日時 : 2008年5月30日 23:31
コメント
  • # re: [Silverlight 奮戦記] (3) ファイルを読む
    zzyytt
    Posted @ 2018/07/18 11:08
    http://www.jordan11retro.us.com
    http://www.kobesneakers.com
    http://www.jordan4.us.com
    http://www.adidasnmdshoes.co.uk
    http://www.goyard.us.com
    http://www.offwhite.us.com
  • # When someone writes an paragraph he/she keeps the idea of a user in his/her brain that how a user can know it. Therefore that's why this post is great. Thanks!
    When someone writes an paragraph he/she keeps the
    Posted @ 2019/05/02 23:32
    When someone writes an paragraph he/she keeps the idea of a user in his/her brain that how a user can know it.
    Therefore that's why this post is great. Thanks!
  • # Good day! I could have sworn I've been to this blog before but after checking through some of the post I realized it's new to me. Anyways, I'm definitely happy I found it and I'll be bookmarking and checking back frequently!
    Good day! I could have sworn I've been to this blo
    Posted @ 2019/05/08 21:04
    Good day! I could have sworn I've been to this blog before but after checking through some
    of the post I realized it's new to me. Anyways, I'm definitely happy I
    found it and I'll be bookmarking and checking back frequently!
  • # Its like you read my mind! You seem to know a lot about this, like you wrote the book in it or something. I think that you could do with a few pics to drive the message home a bit, but instead of that, this is wonderful blog. A great read. I will certa
    Its like you read my mind! You seem to know a lot
    Posted @ 2019/05/13 1:31
    Its like you read my mind! You seem to know a lot about this, like
    you wrote the book in it or something. I think that you could do with a few pics to drive the message home
    a bit, but instead of that, this is wonderful blog.

    A great read. I will certainly be back.
  • # It's enormous that you are getting ideas from this paragraph as well as from our dialogue made at this time.
    It's enormous that you are getting ideas from this
    Posted @ 2019/06/15 6:21
    It's enormous that you are getting ideas from this paragraph as well as from
    our dialogue made at this time.
  • # It's great that you are getting ideas from this paragraph as well as from our argument made at this place.
    It's great that you are getting ideas from this pa
    Posted @ 2019/06/16 21:40
    It's great that you are getting ideas from this paragraph as well as from our argument
    made at this place.
  • # Hello, its fastidious piece of writing concerning media print, we all be familiar with media is a wonderful source of information.
    Hello, its fastidious piece of writing concerning
    Posted @ 2019/06/17 16:59
    Hello, its fastidious piece of writing concerning media print, we all be familiar with media is a wonderful source of information.
  • # You ought to take part in a contest for one of the best websites online. I am going to highly recommend this blog!
    You ought to take part in a contest for one of the
    Posted @ 2019/07/19 13:24
    You ought to take part in a contest for one of the best websites online.
    I am going to highly recommend this blog!
  • # Hello! This post could not be written any better! Reading this post reminds me of my previous room mate! He always kept talking about this. I will forward this write-up to him. Fairly certain he will have a good read. Many thanks for sharing!
    Hello! This post could not be written any better!
    Posted @ 2019/07/21 13:52
    Hello! This post could not be written any better! Reading
    this post reminds me of my previous room mate! He always kept talking about this.
    I will forward this write-up to him. Fairly certain he will have a good read.
    Many thanks for sharing!
  • # I am sure this post has touched all the internet viewers, its really really fastidious paragraph on building up new blog. natalielise pof
    I am sure this post has touched all the internet v
    Posted @ 2019/08/01 15:39
    I am sure this post has touched all the internet viewers, its really really fastidious
    paragraph on building up new blog. natalielise pof
タイトル
名前
Url
コメント