#眉標=Silverlight 4、Elevated Trust、OOB #副標=Silverlight最新技術與應用(2) #大標=體驗Silverlight 4在前端介面上的新應用 #作者=文/圖 董大偉 ===<反灰>============= //CaptureSource物件 CaptureSource CaptureSource; //影像儲存用集合 System.Collections.ObjectModel.ObservableCollection images = new System.Collections.ObjectModel.ObservableCollection(); //按下開始鈕 private void Button1_Click(object sender, RoutedEventArgs e) { //判斷目前啟動狀態 if (!CaptureDeviceConfiguration.AllowedDeviceAccess) { //透過RequestDeviceAccess要求使用者允許使用WebCam(這時畫面上會跳出Yes/No詢問視窗) if (!CaptureDeviceConfiguration.RequestDeviceAccess()) { //失敗 MessageBox.Show("存取設備失敗!"); return; } } //建立CaptureSource CaptureSource = new CaptureSource(); //hook截取影像事件 CaptureSource.CaptureImageCompleted += new EventHandler(CaptureSource_CaptureImageCompleted); //設定影像擷取時的Binding CaptureImages.ItemsSource = images; //VideoBrush VideoBrush vb = new VideoBrush(); //將Grid的背景設為VideoBrush Grid1.Background = vb; //將VideoBrush的資料來源設為CaptureSource vb.SetSource(CaptureSource); //開始截取 CaptureSource.Start(); //按鈕隱藏 Button1.Visibility = System.Windows.Visibility.Collapsed; } ================ ===<反灰>============= //擷取影像鈕 private void button3_Click(object sender, RoutedEventArgs e) {  //擷取影像  CaptureSource.CaptureImageAsync(); }    ================ ===<反灰>============= //hook截取影像事件  CaptureSource.CaptureImageCompleted += new EventHandler(CaptureSource_CaptureImageCompleted); ================ ===<反灰>============= //擷取影像完成 void CaptureSource_CaptureImageCompleted(object sender, CaptureImageCompletedEventArgs e) {  //將影像加入  images.Add(e.Result); } ================ ===<反灰>============= WebCam public class MemoryAudioSink : AudioSink { //存放Audio的format private MemoryStream _stream; //紀錄與回傳格式 private AudioFormat _format; //回傳AudioStream public Stream AudioStream { get { return _stream; } } //回傳當前格式 public AudioFormat CurrentFormat { get { return _format; } } //開始擷取 protected override void OnCaptureStarted() { //…略…  } //擷取完畢 protected override void OnCaptureStopped() { //…略…  } //當音訊格式改變 protected override void OnFormatChange(AudioFormat audioFormat) { //…略… } //當音訊資料傳入 protected override void OnSamples(long sampleTime, long sampleDuration, byte[] sampleData)  { //…略…  } } ================ ===<反灰>============= //Audio recorder AudioSink = new MemoryAudioSink(); AudioSink.CaptureSource = CaptureSource; ================== ===<反灰>============= //停止鈕 private void buttonStop_Click(object sender, RoutedEventArgs e) {  //存檔對話視窗  SaveFileDialog saveFileDialog = new SaveFileDialog() { Filter = "音訊檔案(*.wav)|*.wav" };  //STOP  CaptureSource.Stop();  CaptureSource.CaptureImageCompleted -= CaptureSource_CaptureImageCompleted;  //顯示對話視窗  if (saveFileDialog.ShowDialog() == true)  {   //從使用者選擇的儲存檔案視窗開啟檔案   Stream FileStream = saveFileDialog.OpenFile();   //儲存檔案(AudioSink.AudioStream為音訊內容)   WavHelper.SaveWavFile(AudioSink.AudioStream, FileStream, AudioSink.CurrentFormat);   //關檔   FileStream.Close();   //Message   MessageBox.Show("錄音已存檔至[" + saveFileDialog.SafeFileName + "]");  }  //按鈕出現  Button1.Visibility = System.Windows.Visibility.Visible; } ================ =======<反灰>============= private void textBox1_Drag(object sender, DragEventArgs e) {  if (e.Data == null) return;  //從e.Data取得資料物件  IDataObject DataObject = e.Data as IDataObject;  //取得檔案(集合,可能有多個檔案)  System.IO.FileInfo[] files = (System.IO.FileInfo[])DataObject.GetData(DataFormats.FileDrop);  //列舉每一個檔案  foreach (System.IO.FileInfo item in files)  { //如果檔名為*.txt   if (item.Extension.Equals(".txt"))   { //則開檔 using (System.IO.Stream stream = item.OpenRead()) {    using (System.IO.StreamReader reader = new System.IO.StreamReader(stream, System.Text.UnicodeEncoding.Unicode ))    { //讀取並填入TextBox中 this.textBox1.Text = reader.ReadToEnd();    } } return; //僅處理第一個   } else {   MessageBox.Show("請拖曳文字檔!"); }  } } ==================== ===<反灰>============= private void image1_Drop(object sender, DragEventArgs e) {  if (e.Data == null)   return;  //從e.Data取得資料物件  IDataObject DataObject = e.Data as IDataObject;  System.IO.FileInfo[] files = (System.IO.FileInfo[])DataObject.GetData(DataFormats.FileDrop);  //取第一個  System.IO.FileInfo file = files[0];  try  {   //讀取檔案   using (var stream = file.OpenRead())   {//取得檔案資料    var imageSource = new System.Windows.Media.Imaging.BitmapImage();    imageSource.SetSource(stream); //設為來源    image1.Source = imageSource; }  }  catch (Exception) //如果發生錯誤  {  MessageBox.Show("請拖曳圖片檔!");  } } ================ ===<反灰>============= private void mediaElement1_Drop(object sender, DragEventArgs e) {  if (e.Data == null) return;  //從e.Data取得資料物件  IDataObject DataObject = e.Data as IDataObject;  System.IO.FileInfo[] files = (System.IO.FileInfo[])DataObject.GetData(DataFormats.FileDrop);  //取第一個  System.IO.FileInfo file = files[0];  try  {   var stream = file.OpenRead();   //直接把file stream透過SetSource設為mediaElement1的影片來源   this.mediaElement1.SetSource(stream);   mediaElement1.AutoPlay = true;   mediaElement1.Play(); }  catch (Exception)  {   MessageBox.Show("請拖曳影片檔!");  } } ==================== ========<反灰>============= this.webBrowser1.Navigate(new Uri("http://www.yahoo.com.tw"));    ==================== ========<反灰>============= this.webBrowser1.Navigate(new Uri("http://www.yahoo.com.tw")); this.Shape1.Fill = new WebBrowserBrush { SourceName = "webBrowser1" };    ==================== ========<反灰>============= //粗體 private void button_B_Click(object sender, RoutedEventArgs e) { RichTextBox1.Selection.ApplyPropertyValue( Run.FontWeightProperty, FontWeights.ExtraBold); RichTextBox1.Focus(); } //底線 private void button_U_Click(object sender, RoutedEventArgs e) { RichTextBox1.Selection.ApplyPropertyValue( Run.TextDecorationsProperty, TextDecorations.Underline); RichTextBox1.Focus(); } //傾體 private void button_I_Click(object sender, RoutedEventArgs e) { RichTextBox1.Selection.ApplyPropertyValue( Run.FontStyleProperty, FontStyles.Italic); RichTextBox1.Focus(); } //設定文字大小 private void ComboBox_FontSize_SelectionChanged(object sender, SelectionChangedEventArgs e) { RichTextBox1.Selection.ApplyPropertyValue( Run.FontSizeProperty, ((ComboBoxItem)ComboBox_FontSize.SelectedItem).Content.ToString()); RichTextBox1.Focus(); } //設定顏色 private void ComboBox_Color_SelectionChanged(object sender, SelectionChangedEventArgs e) { RichTextBox1.Selection.ApplyPropertyValue( Run.ForegroundProperty, ((Rectangle)ComboBox_Color.SelectedItem).Fill); RichTextBox1.Focus(); } ==================== ========<反灰>============= //按下加入物件鈕 private void button_InsertUIElement_Click(object sender, RoutedEventArgs e) { //建立InlineUIContainer與圖形 InlineUIContainer container = new InlineUIContainer(); container.Child = new Ellipse { Fill = new SolidColorBrush(Colors.Blue), Width = 100, Height = 30 }; //加入物件 RichTextBox1.Selection.Insert(container); RichTextBox1.Focus(); } //按下加入圖形鈕 private void button_InsertImage_Click(object sender, RoutedEventArgs e) { //建立InlineUIContainer與Image InlineUIContainer container = new InlineUIContainer(); container.Child = new Image() { Width=100, Height=100, Source = new System.Windows.Media.Imaging.BitmapImage( new Uri("http://www.StudyHost.Com/StudyHostLogo.png")) }; //加入物件 RichTextBox1.Selection.Insert(container); RichTextBox1.Focus(); } ==================== ========<反灰>============= //取得XAML this.TextBox1.Text = RichTextBox1.Xaml; //設定XAML RichTextBox1.Xaml = this.TextBox1.Text; ==================== ========<反灰>============= //建立NotificationWindow物件 NotificationWindow nw ; private void button1_Click(object sender, RoutedEventArgs e) {  //顯示突顯式視窗  ShowNotificationWindow("突顯式視窗!"); } void ShowNotificationWindow(string msg) {  if (Application.Current.IsRunningOutOfBrowser == true)  {   //建立NotificationWindow的內容   var tb = new MyMessageWindow();   tb.textBlock1.Text = msg;   //建立NotificationWindow   nw = new NotificationWindow();   nw.Width = 300.0;   nw.Height = 100.0;   //設定NotificationWindow的內容   nw.Content = tb;   //顯示的毫秒數(自動消失時間)   nw.Show(3000);  }  else { MessageBox.Show("只有OOB模式下可使用"); } } ====================