2011年7月22日 星期五

初始化Silverlight應用程式

在內嵌Silverlight應用程式的網頁或ASPX之中,若想要傳遞參數給Silverlight應用程式

  • 在網頁>Object>標籤中加入
<param name="initParams" value="initVar=Mary" />
 
image
 
  • 修改App.xaml.cs檔案,在Application_Startup事件中,利用StartupEventArgs的InitParams 讀取參數,並加入Appliation的Resources
private void Application_Startup ( object sender , StartupEventArgs e ) {
          if ( e.InitParams != null ) {
              foreach ( var initVar in e.InitParams ) {
                  this.Resources.Add ( initVar.Key , initVar.Value );
              }
          }

          this.RootVisual = new MainPage ( );
      }

  • 後續便可在Silverlight程式中,讀取參數,例如以下程式將讀出的參數顯示在label上
label1.Content = App.Current.Resources [ "initVar" ].ToString ( );
  • 若參數有兩個以上,可以使用逗號區隔:
<param name="initParams" value="initVar=Mary,initVar2=Candy" />

在Silverlight應用程式存取HTML DOM

Silverlight應用程式可以直接存取內嵌它的網頁HTML DOM或是讀取查詢字串的內容,你可以利用System.Windows.Browser.HtmlPage類別來達成。

  • 使用Visual Studio 2010 建立一個Silverlight應用程式,並建立測試的網站

image

  • 在測試用的HTML網頁中,加入一個div

image

  • 加一個Button到Silverlight應用程式 MainPage.xaml

image

  • 在Button的Click事件中,加入程式碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;

namespace AccessHTMLDOM {
    public partial class MainPage : UserControl {
        public MainPage ( ) {
            InitializeComponent ( );        }

        private void button1_Click ( object sender , RoutedEventArgs e ) {
            HtmlDocument myDoc = HtmlPage.Document;
            HtmlElement ele = myDoc.GetElementById ( "myTitle" );
            if ( ele != null ) {
                ele.SetStyleAttribute ( "background-color" , "Red" );
            }
        }
    }
}

  • 執行測試網頁,按下Button,便會變更Div的背景顏色

image

若要存取查詢字串,則可以使用以下步驟,

  • 在MainPage.xaml加一個Button,一個Label
  • 在Button的Click事件中,加入程式碼,讀取QueryString中id對應的值,然後加到UserControl的Resources集合中

if ( HtmlPage.Document.QueryString.ContainsKey ( "id" ) ) {
               string myID = HtmlPage.Document.QueryString [ "id" ].ToString ( );
               this.Resources.Add ( "id" , myID );
           } else
               this.Resources.Add ( "id" , "-1" );
           label1.Content = this.Resources [ "id" ].ToString ( );

  • 執行網頁在網址後方輸入查詢字串,按下Button就可以取得值

http://localhost:12432/AccessHTMLDOMTestPage.aspx?id=abc

2011年7月21日 星期四

WCF教學(5) - 使用程式裝載服務

 

延續 WCF教學(4) - 設計Console Client

本練習利用程式的方式,建立ServiceHost物件裝載服務,並透過程式加入服務Endpoint

  • 刪掉 Host  app.config組態檔 (ConsoleApplication1)
  • 修改Host(ConsoleApplication1) ,Main方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using WcfServiceLibrary1;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Service is running...");
            ServiceHost myHost = new
                  ServiceHost(typeof(Calc));
            myHost.AddServiceEndpoint(typeof(IMyService),
                             new BasicHttpBinding(),
                             "http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/");
            myHost.Open();
            Console.ReadLine();
        }
    }
}

  • 按CTRL + F5執行,Client可以正確呼叫Service

目前程式雖然可以正確執行,但因為沒有開放Metadata端點,因此,當你執行Host (ConsoleApplication1) 後,開啟Browser,輸入以下URL

http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/mex

你將無法取得WSDL

  • 修改Main方法,建立ServiceMetadataBehavior開放Metadata

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using WcfServiceLibrary1;
using System.ServiceModel.Description;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Service is running...");
            ServiceHost myHost = new
                  ServiceHost(typeof(Calc));
            myHost.AddServiceEndpoint(typeof(IMyService),
                             new BasicHttpBinding(),
                             "http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/");

            var metadataBehavior = new ServiceMetadataBehavior();
            metadataBehavior.HttpGetEnabled = true;
            metadataBehavior.HttpGetUrl = new Uri("http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/mex");
            myHost.Description.Behaviors.Add(metadataBehavior);

            myHost.Open();
            Console.ReadLine();
        }
    }
}

 

  • CTRL+F5執行
  • 在Browser

http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/mex

可以看到WSDL:

image

WCF教學(4) - 設計Console Client

延續WCF教學(3) - 設計Console Host
這個練習主要介紹如何在Console 程式中呼叫WCF服務
  • 加一個Console Client到Solution
image
  • 修改 Main方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceReference1.IMyService proxy =
    new ServiceReference1.MyServiceClient();
            string result = proxy.Add(3, 4);
            Console.WriteLine(result);
        }
    }
}
  • Solution Explorer –> 選Solution->滑鼠右鍵->Properties設定一次啟動兩個專案。ConsoleApplication1 (Host) 先執行,再執行ConsoleApplication2(Client)
image
  • 按CTRL + F5執行,Client可以正確呼叫Service
image

WCF教學(3) - 設計Console Host

延續 WCF教學(2) - 變更預設的Binding

Host負責接聽端點,將收到的訊息傳給適當的服務實體。這個練習將介紹如何自訂Console Host。

  • 在目前專案中,加入一個Console Application

image

image

  • Add Reference

image

  • 選WCF Service ibrary專案

image

  • Add Reference->.NET ->選System.ServeModel.dl

image

  • 修改Main方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using WcfServiceLibrary1;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Service is running...");
            ServiceHost myHost = new
                ServiceHost(typeof(Calc));
            myHost.Open();
            Console.ReadLine();
        }
    }
}


  • Copy WcfServiceLibrary1專案中的App.config檔到Console程式貼上

image

  • 設Console為起始專案

image

  • CTRL+F5,Console程式應該可以正確執行不會有錯誤。

image

總網頁瀏覽量