cookieChoices = {};
Showing posts with label Silverlight. Show all posts
Showing posts with label Silverlight. Show all posts

Friday, 25 April 2014

Handling Mouse Right Click Event Example

Main.Xaml



<Grid x:Name="LayoutRoot" Background="Black">
        <Border x:Name="BorderCtrl" Height="400" Width="400" MouseLeftButtonDown="BorderCtrl_MouseLeftButtonDown"
                MouseRightButtonDown="BorderCtrl_MouseRightButtonDown" MouseRightButtonUp="BorderCtrl_MouseRightButtonUp" >
            <Border.BorderBrush>
                <LinearGradientBrush EndPoint="0,0" StartPoint="1,1">
                    <GradientStop Color="#333333" Offset="0"/>
                    <GradientStop Color="#999333" Offset="1"/>
                </LinearGradientBrush>
            </Border.BorderBrush>
            <Border.Background>
                <LinearGradientBrush EndPoint="0.5,0" StartPoint="0.5,1">

                    <GradientStop Color="#1F1F1F" Offset="0"/>
                    <GradientStop Color="#060606" Offset="1"/>
                </LinearGradientBrush>

            </Border.Background>

        </Border>
        <TextBlock Text="Right Click on App" IsHitTestVisible="False" Foreground="White" FontSize="15" VerticalAlignment="Center" HorizontalAlignment="Center" />
        <Canvas>
            <Popup x:Name="ContextMenu" Visibility="Collapsed">
                <Border CornerRadius="5" BorderBrush="White" Background="Black" BorderThickness="1">
                    <StackPanel>
                        <HyperlinkButton BorderBrush="White" Visibility="Visible" Click="Green" Content="Green" Foreground="White" ></HyperlinkButton>
                        <HyperlinkButton BorderBrush="White" Visibility="Visible" Click="Red" Content="Red" Foreground="White" ></HyperlinkButton>
                        <HyperlinkButton BorderBrush="White" Visibility="Visible" Click="Blue" Content="Blue" Foreground="White" ></HyperlinkButton>
                        <HyperlinkButton BorderBrush="White" Visibility="Visible" Click="White" Content="White" Foreground="White" ></HyperlinkButton>


                    </StackPanel>
                </Border>
            </Popup>

        </Canvas>



    </Grid>

Main.Xaml.cs


 private void BorderCtrl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ContextMenu.Visibility = Visibility.Collapsed;
            ContextMenu.IsOpen = true;
        }

        private void BorderCtrl_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }

        private void BorderCtrl_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            Point p = e.GetPosition(null);
            e.Handled = true;
            ContextMenu.Visibility = Visibility.Visible;
            ContextMenu.IsOpen = true;
            ContextMenu.SetValue(Canvas.LeftProperty, (double)p.X);
            ContextMenu.SetValue(Canvas.TopProperty, (double)p.Y);
        }

        private void Red(object Sender, RoutedEventArgs e)
        {
                   
            Brush br = new SolidColorBrush(Color.FromArgb(100,250,0,0));
            LayoutRoot.Background = br;
        }

        private void Green(object Sender, RoutedEventArgs e)
        {

            Brush br = new SolidColorBrush(Color.FromArgb(100, 0, 250, 0));
            LayoutRoot.Background = br;
        }

        private void Blue(object Sender, RoutedEventArgs e)
        {

            Brush br = new SolidColorBrush(Color.FromArgb(100, 0, 0, 250));
            LayoutRoot.Background = br;
        }

        private void White(object Sender, RoutedEventArgs e)
        {

            Brush br = new SolidColorBrush(Color.FromArgb(100,250, 250, 250));
            LayoutRoot.Background = br;
        }
        

Thursday, 24 April 2014

Share Data Between Applications

Main Page.Xaml:

 <Grid x:Name="LayoutRoot" Background="White">

        <Button Content="Set To File" Height="57" HorizontalAlignment="Left" Margin="70,92,0,0" Name="btnSetFile" VerticalAlignment="Top" Width="98" Click="btnSetFile_Click" />
        <Button Content="Get From File" Height="57" HorizontalAlignment="Right" Margin="0,92,90,0" Name="btnGetFile" VerticalAlignment="Top" Width="100" Click="btnGetFile_Click" />

    </Grid>
MainPage.Xaml.cs:

private void btnSetFile_Click(object sender, RoutedEventArgs e)
        {
            IsolatedStorageFile myStorage = IsolatedStorageFile.GetUserStoreForApplication();

            using (StreamWriter sw = new StreamWriter(new IsolatedStorageFileStream("ISData.txt", FileMode.Create, FileAccess.Write, myStorage))) 
            {
                sw.WriteLine("Isolated Storage Demo");
                sw.Close();
            }
        }

        private void btnGetFile_Click(object sender, RoutedEventArgs e)
        {
            IsolatedStorageFile myStorage = IsolatedStorageFile.GetUserStoreForApplication();

            IsolatedStorageFileStream fs = myStorage.OpenFile("ISData.txt", FileMode.Open, FileAccess.Read);

            using (StreamReader sr = new StreamReader(fs))
            {
               MessageBox.Show(sr.ReadLine());
            }


        }

Tuesday, 22 April 2014

SilverLight Interact with Html and JS

Interact with Html 


Create new Project of SilverLight Applicatioon with host website



Add Following code to the  .html page 


.htmlContainer
        {
        margin-top:115px;
        margin-left:0px;
        float:left;
        font-size:1.4em;
        }

<div class="htmlContainer">
        <input type="text" name="txtHtml" />
        <input type="button" name="btnHtml" value="Demo" onclick="CallSL()" />
    </div>

Main.Xaml:

 <Grid x:Name="LayoutRoot" Background="White">
        <Canvas>
            <TextBox Canvas.Left="211" Canvas.Top="99" Height="23" Name="txtSV" Width="120" />
            <Button Canvas.Left="211" Canvas.Top="149" Content="InteractHTML" Height="23" Name="btnInteractHtml" Width="75" Click="btnInteractHtml_Click" />
            <Button Canvas.Left="218" Canvas.Top="32" Content="Call JS" Height="26" Name="btnJS" Width="113" Click="btnJS_Click" />
        </Canvas>
    </Grid>

Main.Xaml.cs:

Add namespace--> using System.Windows.Browser;

 public MainPage()
        {
            InitializeComponent();
            HtmlPage.RegisterScriptableObject("Page", this);
        }

        private void btnInteractHtml_Click(object sender, RoutedEventArgs e)
        {
            
            HtmlDocument doc = HtmlPage.Document;
            HtmlElement element = doc.GetElementById("txtHtml");

            if (element != null)
                element.SetProperty("value", txtSV.Text);
        }


Interact with JavaScript

Add Code to Html and Main.xaml page and see

In Html Page

       function ShowMsg(message) {
            alert(message);
        }

        function CallSL() {

            var slcontrol = document.getElementById('silverlightControlHost');
            slcontrol.Content.Page.Msg();

        }


    <div class="htmlContainer">
        <input type="text" name="txtHtml" />
        <input type="button" name="btnHtml" value="Demo" onclick="CallSL()" />
    </div>


Main.Xaml.cs      

private void btnJS_Click(object sender, RoutedEventArgs e)
        {
            HtmlPage.Window.Invoke("ShowMsg", "Hello SLight");
        }

        [ScriptableMember]
        public void Msg()
        {
            MessageBox.Show("From SL");
        }


SilverLight Life Cycle

SilverLight Life Cycle


1. User Request webpage.


2.Html Page <Object> tag loads silverlight plug in.


3.Plugin downloads Xap file and reads Manifest.


4.Create instance of Application Class.


5. Fires Application Start up event.


6. Complete page rendering.



Introduction Program


Select New project --> SliverLight -->

New Silverlight Dialog will open -- Check the Host Silverlight Application in new website

By checking  the check box , The Silverlight Application is new hosted in to the website by default with same silverlight Application Name.

--> Ok

Two projects are Created:
1.silverlight Application(with name u have given)
2. A new website application

In Silverlight Application 

Properties Folder AppManiFest.Xaml is required to generate Application pacakage (.Xap file). This file created .xap file in web application when you build.

In Silverlight : App.xaml works same as Global.asax in web forms

Copy and Paste code below in Main.xaml and Main.Xaml.cs

Build the Application and see the Output.



Main.Xaml:

<Grid x:Name="LayoutRoot" Background="White">
        <Button Content="Hello" Height="58" HorizontalAlignment="Left" Margin="95,130,0,0" Name="btnHello" VerticalAlignment="Top" Width="83" Click="btnHello_Click" />

    </Grid>

Main.Xaml.cs:

 private void btnHello_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Hello SilverLight");
        }