Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

An Introduction to the Silverlight samples in the All-In-One Framework

, 12 Dec 2009
This article introduces several Silverlight samples in the All-In-One Framework.
SilverlightIntroduction_src.zip
CSSL3LocalMessage
Properties
CSSL3MediaElement
CSSL3MediaElement
Properties
CSSL3MediaElement.Web
App_Data
ClientBin
CSSL3MediaElement.xap
Properties
ReadMe.mht
CSSL3PixelShader
cyclewatery.ps
Humpback Whale.jpg
Properties
CSSL3WriteableBitmap
Heart Empty.png
Properties
VBSL3Animation
My Project
VBSL3DeepZoom
VBSL3DeepZoom
images
home_hover.png
home_pressed.png
home_rest.png
My Project
Service References
DeepZoomServiceReference
configuration.svcinfo
configuration91.svcinfo
GenerateDeepZoomService.disco
GenerateDeepZoomService.wsdl
Reference.svcmap
ServiceReferences.ClientConfig
VBSL3DeepZoom.Web
App_Code
App_Data
Bin
DeepZoomTools.DLL
DeepZoomTools.DLL.refresh
ClientBin
GeneratedImages
GenerateDeepZoomService.svc
SourceImages
atom.png
background.jpg
cell.png
dna.png
Molecular.png
neutron.png
seed.png
wrap in bamboo.png
VBSL3HTMLBridge
VBSL3HTMLBridge
My Project
VBSL3HTMLBridge.Web
App_Data
ClientBin
VBSL3HTMLBridge.xap
My Project
Application.myapp
MyExtensions
Settings.settings
VBSL3Input
My Project
VBSL3LocalMessage
My Project
VBSL3MediaElement
ReadMe.mht
VBSL3MediaElement
My Project
VBSL3MediaElement.Web
App_Data
ClientBin
VBSL3MediaElement.xap
My Project
Application.myapp
MyExtensions
Settings.settings
VBSL3PixelShader
cyclewatery.ps
Humpback Whale.jpg
My Project
CSSL3Animation
Properties
CSSL3DeepZoom
CSSL3DeepZoom
images
home_hover.png
home_pressed.png
home_rest.png
Properties
Service References
DeepZoomServiceReference
configuration.svcinfo
configuration91.svcinfo
GenerateDeepZoomService.disco
GenerateDeepZoomService.wsdl
GenerateDeepZoomService1.wsdl
Reference.svcmap
ServiceReferences.ClientConfig
CSSL3DeepZoom.Web
App_Code
Bin
DeepZoomTools.DLL
DeepZoomTools.DLL.refresh
ClientBin
GeneratedImages
GenerateDeepZoomService.svc
SourceImages
atom.png
background.jpg
cell.png
dna.png
Molecular.png
neutron.png
seed.png
wrap in bamboo.png
ReadMe.mht
CSSL3HTMLBridge
CSSL3HTMLBridge
Properties
CSSL3HTMLBridge.Web
App_Data
ClientBin
CSSL3HTMLBridge.xap
Properties
CSSL3Input
Properties
/****************************** Module Header ******************************\
* Module Name:                 MainPage.xaml.cs
* Project:                     CSSL3LocalMessage
* Copyright (c) Microsoft Corporation.
* 
* Local message whiteboard's code behind file.
* 
* This source is subject to the Microsoft Public License.
* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
* All other rights reserved.
* 
* History:
* * 9/22/2009 6:34 PM Mog Liang Created
\***************************************************************************/

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.Messaging;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Windows.Ink;

namespace CSSL3LocalMessage
{
    public partial class MainPage : UserControl
    {
        // Preassign two names for LocalMessageReceiver.
        readonly string[] clientnames = { "{36BF6178-53A3-4d0b-8E0D-1E7BD9FBBE62}", "{E62BF131-B5EA-4f1d-B80D-DC083C7F0851}" };
        // Preassign two colors for distinguishing two applications.
        readonly Color[] colors = { Colors.Red, Colors.Green };
        int clientid;

        LocalMessageReceiver _localreceiver;
        LocalMessageSender _localsender;
        DataContractJsonSerializer _jsonserializer;

        public MainPage()
        {
            InitializeComponent();

            _jsonserializer =new DataContractJsonSerializer(inkP.Strokes.GetType());

            // First try.
            // Create receiver with one preset name.
            _localreceiver = new LocalMessageReceiver(clientnames[clientid]);
            _localreceiver.MessageReceived += localreceiver_MessageReceived;
            try
            {
                _localreceiver.Listen();
            }
            catch(Exception e)
            {
                // Second try.
                // Create receiver with another preset name.
                _localreceiver.MessageReceived -= localreceiver_MessageReceived;
                clientid = 1;
                _localreceiver = new LocalMessageReceiver(clientnames[clientid]);
                _localreceiver.MessageReceived += localreceiver_MessageReceived;
                try
                {
                    _localreceiver.Listen();
                }
                catch (Exception e1)
                {
                    // Already has two apllication instance.
                    // Pop messagebox, disable drawing area.
                    MessageBox.Show("Only allow 2 clients.");
                    disablegrid.Visibility = Visibility.Visible;
                    return;
                }
            }

            // Create sender targeting to another application's receiver
            _localsender = new LocalMessageSender(clientnames[(clientid + 1) % 2]);
            _localsender.SendCompleted += localsender_SendCompleted;

            drawbr.Background = new SolidColorBrush(colors[clientid]);
        }

        void localsender_SendCompleted(object sender, SendCompletedEventArgs e)
        {
            // If send message got error, pop messagebox.
            if(e.Error!=null)
                MessageBox.Show("Cannot connect to another client.");
        }

        void localreceiver_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            // Deserialize json string to stroke object.
            var stream = new MemoryStream();
            var streamwriter = new StreamWriter(stream);
            streamwriter.Write(e.Message);
            streamwriter.Flush();
            var receivedstroke = _jsonserializer.ReadObject(stream) as Stroke;
            stream.Close();

            // Add received stroke to inkPresenter.
            inkP.Strokes.Add(receivedstroke);
        }

        // Attaching MouseLeftButtonDown/MouseMove/MouseLeftButtonUp event
        // to implement drawing functionality.
        Stroke _newStroke;
        private void InkPresenter_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            inkP.CaptureMouse();
            _newStroke = new System.Windows.Ink.Stroke();
            _newStroke.DrawingAttributes.Color = colors[clientid];
            _newStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(inkP));
            inkP.Strokes.Add(_newStroke);
        }

        // Sending message when finishing one stroke.
        private void InkPresenter_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            inkP.ReleaseMouseCapture();

            // Serialize stroke object to string.
            var stream = new MemoryStream();
            _jsonserializer.WriteObject(stream, _newStroke);
            stream.Flush();
            stream.Position = 0;
            var obstring = new StreamReader(stream).ReadToEnd();
            stream.Close();

            // Send Serialized stroke.
            if (_localsender != null)
            {
                try
                {
                    _localsender.SendAsync(obstring);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            _newStroke = null;
        }

        private void InkPresenter_MouseMove(object sender, MouseEventArgs e)
        {
            if (_newStroke != null)
            {
                _newStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(inkP));
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

Microsoft All-In-One Code Framework delineates the framework and skeleton of Microsoft development techniques through typical sample codes in three popular programming languages (Visual C#, VB.NET, Visual C++). Each sample is elaborately selected, composed, and documented to demonstrate one frequently-asked, tested or used coding scenario based on our support experience in MSDN newsgroups and forums. If you are a software developer, you can fill the skeleton with blood, muscle and soul. If you are a software tester or a support engineer like us, you may extend the sample codes a little to fit your specific test scenario or refer your customer to this project if the customer's question coincides with what we collected.
http://cfx.codeplex.com/

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 12 Dec 2009
Article Copyright 2009 by All-In-One Code Framework
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid