In actual silverlight projects, you often need to play videos and print documents. In this section, we will create an example of the simplest way to play videos and print documents.
I. Play a WMV Video
First, create a Silverlight application SLShowVideo, and put a sample Wmv Video under the root directory of the SLShowVideo. web project.
Then we put a MediaElement control in the Xaml document and write the following code:
<MediaElement Height = "377" HorizontalAlignment = "Left" Margin =, 583 "Name =" showVideo "verticalignment =" Top "Width =" "/> <Button Content =" play "Height =" 28 "HorizontalAlignment =" Left "Margin =" 194,428, 0, 0 "Name =" button1 "verticalignment =" Top "Width =" 99 "Click =" button?click_1 "/> <Button Content =" Suspend "Height =" 28 "HorizontalAlignment =" left "Margin =" 333,428, 0, 0 "Name =" button3 "verticalignment =" Top "Width =" 99 "Click =" button3_Click "/> <Button Content =" stop "Height =" 28 "HorizontalAlignment =" left "Margin =" 468,428, 0, 0 "Name =" button2 "verticalignment =" Top "Width =" 99 "Click =" button2_Click "/> <Button Content =" loading video "Height =" 28 "HorizontalAlignment =" left "Margin =" 57,428, 0, 0 "Name =" button4 "verticalignment =" Top "Width =" 99 "Click =" button#click "/>
Finally, write the following code to load the video and control the video playback:
# Region playback video private void button#click (object sender, RoutedEventArgs e) {// load the video this. showVideo. source = new Uri (GetURL () + "/sampleVideo. wmv ");} /// <summary> /// obtain the Url prefix of the current website /// </summary> /// <returns> </returns> public static string GetURL () {ScriptObject location = (HtmlPage. window. getProperty ("location") as ScriptObject); object r = location. getProperty ("href"); string URL = r. toString (). substring (0, r. toString (). lastIndexOf ('/'); // returns the prefix of the network URL stored in the current SILVERLIGHT program;} private void button3_Click (object sender, RoutedEventArgs e) {// pause this. showVideo. pause ();} private void button2_Click (object sender, RoutedEventArgs e) {// stop this. showVideo. stop ();} private void button1_Click_1 (object sender, RoutedEventArgs e) {// play this. showVideo. play () ;}# endregion
Ii. Print documents
First, read the XAML document. Add a Canvas element. All content in the element is to be printed soon (you can also set the content of the elements such as Grid to be printed ).
<Canvas Height = "376" HorizontalAlignment = "Left" Margin = "611,32, 369" Name = "canvas1" verticalignment = "Top" Width = ""> <sdk: label Width = "85" Canvas. left = "9" Content = "first print program" Canvas. top = "27"/> <sdk: Label Canvas. left = "11" Canvas. top = "60" Height = "16" Content = "second print program" Name = "label1" Width = "86"/> </Canvas> <Button Content = "Print "Height =" 34 "HorizontalAlignment =" Left "Margin =" 747,426, 110 "Name =" btnPrint "verticalignment =" Top "Width =" "Click =" btnPrint_Click "/>
In the Button event processing program, add the Canvas Element for code printing.
# Region print Document PrintDocument print; private void btnPrint_Click (object sender, RoutedEventArgs e) {print = new PrintDocument (); // Add a print page event to set the print control. printPage + = new EventHandler <PrintPageEventArgs> (print_PrintPage); print. print ("canvas1");} void print_PrintPage (object sender, PrintPageEventArgs e) {// set to Print this. all content of canvas1 e. pageVisual = this. canvas1 ;}# endregion