European Silverlight 4 & Silverlight 5 Hosting BLOG

BLOG about Silverlight 5 Hosting and Its Techologies - Dedicated to European Windows Hosting Customer

Silvelight Hosting - HostForLIFE.eu :: Analog Clock Using .NET Silverlight

clock August 2, 2024 08:11 by author Peter

Step 1
Create a new project in Visual Studio and select "Silverlight Application".

Step 2
Open MainPage.xaml and add the following code.
    <UserControl x:Class="SilverlightAnalogClock.MainPage"  
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
        mc:Ignorable="d"  
        d:DesignHeight="300" d:DesignWidth="400">  
      
        <Grid x:Name="LayoutRoot" Background="White">  
             
        </Grid>  
    </UserControl>  

Step 3
Open MainPage.xaml.cs and add the following code.
    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.Media.Imaging;  
    using System.Windows.Shapes;  
      
    namespace SilverlightAnalogClock  
    {  
        public partial class MainPage : UserControl  
        {  
      
            public Canvas ClockArea = null;  
            public Rectangle secondHand = null;  
            public Rectangle minuteHand = null;  
            public Rectangle hourHand = null;  
      
            public RotateTransform secondHandRotate = null;  
            public RotateTransform minuteHandRotate = null;  
            public RotateTransform hourHandRotate = null;  
      
            public Ellipse outerCircle = null;  
      
            public Point centerPoint;  
            public double HEIGHT  = 0;  
            public double WIDTH  = 0;  
            public double RADIUS = 0;  
      
            public MainPage()  
            {  
                InitializeComponent();  
      
                ClockArea = new Canvas()  
                {  
      
                    Width = 300,  
                    Height = 300,  
                    HorizontalAlignment = HorizontalAlignment.Left,  
                    VerticalAlignment = VerticalAlignment.Top  
      
                };  
      
                ClockArea.SetValue(Grid.RowProperty, 0);  
                ClockArea.SetValue(Grid.ColumnProperty, 0);  
                ClockArea.Margin = new Thickness(0, 0, 0, 0);  
                this.LayoutRoot.Children.Add(ClockArea);  
      
                WIDTH = ClockArea.Width;  
                HEIGHT = ClockArea.Height;  
                centerPoint.X = (WIDTH/2);  
                centerPoint.Y = (HEIGHT/2);  
                       
                RADIUS = 400;  
                DrawClockFace();  
      
                Point TOPPOINT = new Point(0, 0);  
      
                DrawMinuteHand();  
                DrawSecondHand();  
                DrawHourHand();  
                DrawCenterCircle();  
      
      
                //Start the Clock  
                ClockStart();  
                  
      
            }  
      
            public void ClockStart()  
            {  
                // Create and Start the Thread Timer  
                System.Windows.Threading.DispatcherTimer clockTimer = new System.Windows.Threading.DispatcherTimer();  
                clockTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000);  
                clockTimer.Tick += new EventHandler(Clock_Tick);  
                clockTimer.Start();  
            }  
      
            // Get and Set the Angles of Each Hand at every Clock Ticks  
            public void Clock_Tick(object o, EventArgs sender)  
            {  
                double hourRotateValue = Convert.ToDouble(DateTime.Now.Hour.ToString());  
                double minuteRotateValue = Convert.ToDouble(DateTime.Now.Minute.ToString());  
                double secondRotateValue = Convert.ToDouble(DateTime.Now.Second.ToString());  
                hourRotateValue = (hourRotateValue + minuteRotateValue / 60) * 30;  
                minuteRotateValue = (minuteRotateValue + secondRotateValue / 60) * 6;  
                secondRotateValue = Convert.ToDouble(DateTime.Now.Second.ToString()) * 6;  
                minuteHandRotate.Angle = minuteRotateValue;  
                hourHandRotate.Angle = hourRotateValue;  
                secondHandRotate.Angle = secondRotateValue;  
            }  
      
            // Draw Center Circle  
            public void DrawCenterCircle()  
            {              
                Ellipse centerCircle = new Ellipse()  
                {  
      
                    Width = 10,  
                    Height = 10,  
                    Stroke = new SolidColorBrush(Colors.Red),  
                    Fill = new SolidColorBrush(Colors.Red),  
                    HorizontalAlignment = HorizontalAlignment.Center,  
                    VerticalAlignment = VerticalAlignment.Center  
      
                };  
      
                centerCircle.SetValue(Grid.RowProperty, 0);  
                centerCircle.SetValue(Grid.ColumnProperty, 0);  
                Canvas.SetLeft(centerCircle, (WIDTH / 2) - (centerCircle.Width / 2));  
                Canvas.SetTop(centerCircle, (HEIGHT / 2) - (centerCircle.Height / 2));  
                ClockArea.Children.Add(centerCircle);  
            }  
      
            // Draw Clock Face  
            public void DrawClockFace()  
            {  
                          
                int smallCircle = 5;  
      
                Color c = Colors.Blue;  
                int p = 0;  
      
                // Draw Shadow of Outer Circle  
                Ellipse outerCircleShadow = new Ellipse()  
                {  
                    Width = (WIDTH),  
                    Height = (WIDTH),  
                    Stroke = new SolidColorBrush(Colors.Gray),  
                    StrokeThickness = 5,  
                    HorizontalAlignment = HorizontalAlignment.Center,  
                    VerticalAlignment = VerticalAlignment.Center  
                      
                };  
                 
                outerCircleShadow.SetValue(Grid.RowProperty, 0);  
                outerCircleShadow.SetValue(Grid.ColumnProperty, 0);  
                Canvas.SetLeft(outerCircleShadow, (WIDTH / 2) - (outerCircleShadow.Width / 2) + 6.5);  
                Canvas.SetTop(outerCircleShadow, (HEIGHT / 2) - (outerCircleShadow.Height / 2) + 6.5);  
                ClockArea.Children.Add(outerCircleShadow);  
                 
                //  Draw Outer Circle  
                outerCircle = new Ellipse()  
                {  
                    Width = (WIDTH ),  
                    Height = (WIDTH),  
                    Stroke = new SolidColorBrush(Colors.Black),  
                    StrokeThickness = 5,  
                    HorizontalAlignment = HorizontalAlignment.Center,  
                    VerticalAlignment = VerticalAlignment.Center  
                };              
                outerCircle.SetValue(Grid.RowProperty, 0);  
                outerCircle.SetValue(Grid.ColumnProperty, 0);  
                Canvas.SetLeft(outerCircle, (WIDTH / 2) - (outerCircle.Width / 2) + 4.5);  
                Canvas.SetTop(outerCircle, (HEIGHT / 2) - (outerCircle.Height / 2) + 4.5);  
                ClockArea.Children.Add(outerCircle);  
      
      
                outerCircle.Fill = new LinearGradientBrush()  
                    {  
                        EndPoint = new Point(1, 0),  
                        GradientStops = new GradientStopCollection()  
                        {  
                                new GradientStop() { Color = Colors.White, Offset = 0 },  
                                new GradientStop() { Color = Colors.Gray, Offset = 0.5 },  
                                 new GradientStop() { Color = Colors.White, Offset = 1 }  
                        }  
                    };  
      
                int clockDigits = 3;  
                double rad = (WIDTH/2) - 10.0f;  
                // 60 Innner Dots as Small Circle  
                for (double i = 0.0; i < 360.0; i += 6)   
                {   
      
                double angle = i * System.Math.PI / 180;  
      
                int x = (int)(centerPoint.X + rad * System.Math.Cos(angle));  
                int y = (int)(centerPoint.Y + rad * System.Math.Sin(angle));  
      
                if (p % 5 == 0)  
                {  
                    smallCircle = 10;  
                    c = Colors.Orange;                  
                }  
                else  
                {  
                    smallCircle = 5;  
                    c = Colors.Blue;  
                }  
                if (p % 15 == 0)  
                {  
                    TextBlock tb = new TextBlock();  
                    tb.Text = clockDigits.ToString();  
                    tb.FontSize = 24;  
                      
                    tb.SetValue(Grid.RowProperty, 0);  
                    tb.SetValue(Grid.ColumnProperty, 0);  
                    Canvas.SetLeft(tb, x );  
                    Canvas.SetTop(tb, y);  
                    if (clockDigits == 3)  
                    {  
                        Canvas.SetLeft(tb, x - 20);  
                        Canvas.SetTop(tb, y - 10);  
                    }  
                    if (clockDigits == 6)  
                    {  
                        Canvas.SetLeft(tb, x);  
                        Canvas.SetTop(tb, y - 30);  
                    }  
                    if (clockDigits == 9)  
                    {  
                        Canvas.SetLeft(tb, x + 15);  
                        Canvas.SetTop(tb, y - 10);  
                    }  
                    if (clockDigits == 12)  
                    {  
                        Canvas.SetLeft(tb, x - 10);  
                        Canvas.SetTop(tb, y + 5 );  
                    }   
                    
                      
                    ClockArea.Children.Add(tb);  
                    clockDigits = clockDigits + 3;  
                }  
      
                p++;  
                 
                            Ellipse innerPoints = new Ellipse()  
                            {  
                                Width = smallCircle,  
                                Height = smallCircle,  
                                Stroke = new SolidColorBrush(c),  
                                Fill = new SolidColorBrush(c),  
                                HorizontalAlignment = HorizontalAlignment.Center,  
                                VerticalAlignment = VerticalAlignment.Center  
                            };  
                            innerPoints.SetValue(Grid.RowProperty, 0);  
                            innerPoints.SetValue(Grid.ColumnProperty, 0);  
                            Canvas.SetLeft(innerPoints, x);  
                            Canvas.SetTop(innerPoints, y);  
                            ClockArea.Children.Add(innerPoints);  
      
                }  
      
                  
            }  
            // Draw the Second Hand  
            public void DrawSecondHand()  
            {  
      
                double handLength = (HEIGHT / 2) - 20;  
                secondHand = new Rectangle()  
                {  
                    Width = 1,  
                    Height = handLength,  
                    Stroke = new SolidColorBrush(Colors.Red),  
                    Fill = new SolidColorBrush(Colors.Red),  
                    HorizontalAlignment = HorizontalAlignment.Center,  
                    VerticalAlignment = VerticalAlignment.Center  
                };  
                  
                secondHand.SetValue(Grid.RowProperty, 0);  
                secondHand.SetValue(Grid.ColumnProperty, 0);  
                //Add Rotate Transformation  
                secondHandRotate = new RotateTransform();  
                secondHandRotate.Angle = 0;  
                //Set Center for Rotation  
                secondHandRotate.CenterX = Canvas.GetLeft(secondHand);  
                secondHandRotate.CenterY = secondHand.Height;  
                secondHand.RenderTransform = secondHandRotate;  
                //Set Initial Position of Hand  
                Canvas.SetTop(secondHand, centerPoint.Y - handLength);  
                Canvas.SetLeft(secondHand, WIDTH/2);             
                ClockArea.Children.Add(secondHand);  
                 
            }  
      
            public void DrawMinuteHand()  
            {  
                double handLength = (HEIGHT / 2) - 50;  
                minuteHand = new Rectangle()  
                {  
                    Width = 4,  
                    Height = handLength,  
                    Stroke = new SolidColorBrush(Colors.Black),  
                    Fill = new SolidColorBrush(Colors.Black),  
                    HorizontalAlignment = HorizontalAlignment.Center,  
                    VerticalAlignment = VerticalAlignment.Center  
                };  
      
                minuteHand.SetValue(Grid.RowProperty, 0);  
                minuteHand.SetValue(Grid.ColumnProperty, 0);  
      
                minuteHandRotate = new RotateTransform();  
                minuteHandRotate.Angle = 0;  
                minuteHandRotate.CenterX = Canvas.GetLeft(minuteHand);  
                minuteHandRotate.CenterY = minuteHand.Height;  
                minuteHand.RenderTransform = minuteHandRotate;  
                Canvas.SetTop(minuteHand, centerPoint.Y - handLength);  
                Canvas.SetLeft(minuteHand, WIDTH / 2);  
                ClockArea.Children.Add(minuteHand);  
      
            }  
            public void DrawHourHand()  
            {  
                double handLength = (HEIGHT / 2) - 80;  
                hourHand = new Rectangle()  
                {  
                    Width = 4,  
                    Height = handLength,  
                    Stroke = new SolidColorBrush(Colors.Black),  
                    Fill = new SolidColorBrush(Colors.Black),  
                    HorizontalAlignment = HorizontalAlignment.Center,  
                    VerticalAlignment = VerticalAlignment.Center  
                };  
      
                hourHand.SetValue(Grid.RowProperty, 0);  
                hourHand.SetValue(Grid.ColumnProperty, 0);  
      
                hourHandRotate = new RotateTransform();  
                hourHandRotate.Angle = 0;  
                hourHandRotate.CenterX = Canvas.GetLeft(hourHand);  
                hourHandRotate.CenterY = hourHand.Height;  
                hourHand.RenderTransform = hourHandRotate;  
                Canvas.SetTop(hourHand, centerPoint.Y - handLength);  
                Canvas.SetLeft(hourHand, WIDTH / 2);     
                ClockArea.Children.Add(hourHand);  
      
            }  
      
        }  
    }  

Output

Now execute and you will get a fully drawn Analog Clock.




Silvelight 6 Hosting - HostForLIFE.eu :: How to pass init parameters in Silverlight?

clock March 7, 2024 07:27 by author Peter
In your aspx page where object tag is present..Add init param tag of your own.

<object data="data:application/x-silverlight-2,"  ID="Silverlight2"  type="application/x-silverlight-2" width="100%" height="100%">
          <param name="source" value="ClientBin/ProductProDemo.xap"/>
          <param name="onError" value="onSilverlightError" />
          <param name="background" value="white" />
          <param name="minRuntimeVersion" value="4.0.41108.0" />
          <param name="autoUpgrade" value="true" />  
                    
          <param name="initParams"  value="startPage='<asp:Literal id="id" runat="server"/>'"></param>
           
          <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.41108.0" style="text-decoration:none">
               <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
          </a>
        </object>

In the code behind file of the same aspx page ie aspx.cs file..

On page load provide the value to your init param
protected void Page_Load(object sender, EventArgs e)
{        
    id.Text = "Page1".ToString();        
}


In your App.xaml.cs file you can access the value as 
private void Application_Startup(object sender, StartupEventArgs e)
{
    string startPage = e.InitParams["startPage"];             
    this.RootVisual = new MainPage();
}

 



Silvelight 6 Hosting - HostForLIFE.eu :: Implementing INotifyPropertyChanged in Silverlight

clock May 26, 2023 08:15 by author Peter

Data binding is one of the best features the human race has ever devised. Binding a property of a UI Element to a property in the code behind can accomplish any task. It is magic, in a nutshell. Once the properties are bound, we must continue to notify the UI whenever the property's value is modified in the code. INotifyPropertyChanged is useful in this situation.


Because it is an interface, it must first be implemented. However, the procedure is not arduous. Here is the code for my primary page in my new Silverlight project:

publicpartialclassMainPage : UserControl
{
    privatestring _names;
     publicstring Names
    {
        get
        {
            return _names;
        }
        set
        {
            _names = value;
        }
    }

    public MainPage()
    {
        InitializeComponent();
    }

    privatevoid MainPage_OnLoaded(object sender, RoutedEventArgs e)
    {
        Names = "This is the Text";
    }
}


The property "Name" I have here is bound with the textblock in XAML, here is the code:
<UserControlx:Class="PropertyChangedDescribed.MainPage"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="d"
 Loaded="MainPage_OnLoaded"
 x:Name="TestingPropertyChanged"
 d:DesignHeight="300"d:DesignWidth="400">

 <Gridx:Name="LayoutRoot"Background="White">
  <TextBlockText="{Binding Names, ElementName=TestingPropertyChanged}"/>
  </Grid>

</UserControl>

As you can see, the textblock has it's "text" property bound with our code behind's property "Name". Right now, no matter what you set the value of "Name", it will never be reflected onto the UI. So, what we want is, every time we change the value of our property "Name," the text block has its value changed too. In order to do this, we need to implement the interface INotifyPropertyChanged. Here is the modified main page's code to do so:
publicpartialclassMainPage : UserControl, INotifyPropertyChanged
{
    privatestring _names;
    publicstring Names
    {
        get
        {
            return _names;
        }
        set
        {
            _names = value;
            OnPropertyChanged("Names");
        }
    }
    public MainPage()
    {
        InitializeComponent();

    }

    privatevoid MainPage_OnLoaded(object sender, RoutedEventArgs e)
    {
        Names = "This is the Text";

    }

    publicevent PropertyChangedEventHandler PropertyChanged;
    privatevoid OnPropertyChanged(string propertyName)

    {
        if (this.PropertyChanged != null)
        {
            PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
        }
    }
}


So this is how you can implement INotifyPropertyChanged in Silverlight.



Silvelight 6 Hosting - HostForLIFE.eu :: How to Move Image or Object in Silverlight ?

clock December 18, 2020 08:25 by author Peter

The control that you just like drag or move with the mouse is embedded among a Border control then handle the mouse down, up and move events to create the object move among your layout panel.

See sample .xaml code:
<Canvas x:Name="LayoutRoot" Background="White">
<Border x:Name="border1"
Canvas.Top="100"
Canvas.Left="10"
MouseLeftButtonDown="border1_MouseLeftButtonDown"
MouseLeftButtonUp="border1_MouseLeftButtonUp"
MouseMove="border1_MouseMove"> 
<Image x:Name="MyImage" Source="images/Basket.png" Stretch="Uniform" ></Image>           
</Border>
</Canvas>


In the above code, a Border control is placed within the Canvas. The foremost necessary code to notice is:
MouseLeftButtonDown="border1_MouseLeftButtonDown"
MouseLeftButtonUp="border1_MouseLeftButtonUp"
MouseMove="border1_MouseMove"


The above lines outline 3 events that we tend to like to handle. because the name indicates, we are handling the mouse button down, mouse button up and mouse move events for the left mouse.

In the code behind, once the left button is pressed, we are going to set a global variable to point that user has started moving. within the mouse move event, we are going to get the current location of the mouse pointer and then set the new position for the border control. once the left mouse button is discharged, we are going to reset the global variable in order that we are going to not move the item from now on.
See the code for the code behind class:
public partial class Page : UserControl
{
// Global variable to indicate if user has clicked border
// and started/stopped moving.
private bool moving = false;
private double offSetX;
private double offSetY;
public Page()
{
InitializeComponent();
}

private void border1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Left mouse button clicked within border. start moving.
moving = true;

Point offset = e.GetPosition(border1);
offSetX = offset.X;
offSetY = offset.Y;
}

private void border1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
// Left mouse button release. Stop moving.
moving = false;
}

private void border1_MouseMove(object sender, MouseEventArgs e)
{
if (moving)
{
    // Get the new mouse pointer position
    Canvas parent = (Canvas)this.border1.Parent;
    Point p = e.GetPosition(parent);
    double x = p.X - offSetX;
    double y = p.Y - offSetY;
    // Set the new position for the border control.
    this.border1.SetValue(Canvas.LeftProperty, x);
    this.border1.SetValue(Canvas.TopProperty, y);
}
}
}

HostForLIFE.eu Silverlight 6 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.



Silverlight Hosting France - HostForLIFE.eu :: Use ScrollViewer Layout Panel in Silverlight

clock May 17, 2019 11:36 by author Peter

In this post allow us to understand how to use ScrollViewer panel Layout inside a Silverlight application. ScrollViewer is an additional layout container, that we don’t use constantly. It's chiefly useful showing contents in an exceedingly scrollable panel such as ListBox or Editor window. ListBox, TextBox, RichTextBox internally uses ScrollViewer to implement the scrolling functionality. Allow us to discuss the implementation in this post.

As usual, open up visual studio and choose Silverlight project. We will discover there's a Grid layout in your MainPage. xaml. Eliminate the default Grid layout and merely drag and drop the Stack panel Layout into our application. The code for this looks such as:
<StackPanel x:Name="LayoutRoot" > </StackPanel>

Inside the stack panel I am just defining 12 different rectangles.  And this is the code that I used:
<StackPanel x:Name="LayoutRoot" Orientation="Vertical" Width="100">
            <Rectangle Height="50" Width="100" Fill="Red" />
            <Rectangle Height="50" Width="100" Fill="Green" />
            <Rectangle Height="50" Width="100" Fill="Orange" />
            <Rectangle Height="50" Width="100" Fill="Tomato" />
            <Rectangle Height="50" Width="100" Fill="WhiteSmoke" />
            <Rectangle Height="50" Width="100" Fill="Green" />
            <Rectangle Height="50" Width="100" Fill="Blue" />
            <Rectangle Height="50" Width="100" Fill="Yellow" />
            <Rectangle Height="50" Width="100" Fill="Azure" />
            <Rectangle Height="50" Width="100" Fill="Gold" />
            <Rectangle Height="50" Width="100" Fill="Blue" />
            <Rectangle Height="50" Width="100" Fill="Violet" />
        </StackPanel>


In case we compile the above code as it's, we will notice all of the rectangles however no scroll bar result. Thus in an effort to get scroll bar effect we ought to put the above stack panel inside scroll viewer and ought to offer fixid width towards the scroll viewer. And this is the code snippet:
<ScrollViewer Height="200" >
        <StackPanel x:Name="LayoutRoot" Orientation="Vertical" Width="100">
            <Rectangle Height="50" Width="100" Fill="Red" />
            <Rectangle Height="50" Width="100" Fill="Green" />
            <Rectangle Height="50" Width="100" Fill="Orange" />
            <Rectangle Height="50" Width="100" Fill="Tomato" />
            <Rectangle Height="50" Width="100" Fill="WhiteSmoke" />
            <Rectangle Height="50" Width="100" Fill="Green" />
            <Rectangle Height="50" Width="100" Fill="Blue" />
            <Rectangle Height="50" Width="100" Fill="Yellow" />
            <Rectangle Height="50" Width="100" Fill="Azure" />
            <Rectangle Height="50" Width="100" Fill="Gold" />
            <Rectangle Height="50" Width="100" Fill="Blue" />
            <Rectangle Height="50" Width="100" Fill="Violet" />
        </StackPanel>
    </ScrollViewer>

Finally, Run the code and here is the result:



European Silverlight 6 Hosting HostForLIFE.eu :: How to access controls in DataGrid TemplateColumn header?

clock April 25, 2019 11:20 by author Peter

A data grid view is a rectangular control made of columns and rows. I have a DataGrid where I have included some controls in column header. Each column is a Template column. These controls appear just below the column header which are used for entering filter information. Here's the issue on my code on Silverlight 5.


VisualTreeHelper class helps to iterate through the visual tree of the xaml. Using it we can find the child and parent controls of the rendered controls. Lets check the Visual Tree of the rendered control using Silverlight Spy.

The Following Method do a search over the child controls with in a control recursively and returns the control based on Name.

private object GetChildControl(DependencyObject parent, string controlName)

    Object tempObj = null;
    int count = VisualTreeHelper.GetChildrenCount(parent);
    for (int counter = 0; counter < count; counter++)
    {
        //Get The Child Control based on Index
        tempObj = VisualTreeHelper.GetChild(parent, counter);
        //If Control's name Property matches with the argument control
        //name supplied then Return Control
        if ((tempObj as DependencyObject).GetValue(NameProperty).ToString() == controlName)
            return tempObj;
        else //Else Search Recursively
        {
            tempObj = GetChildControl(tempObj as DependencyObject, controlName);
            if (tempObj != null)
                return tempObj;
        }
    }
    return null;
}

Make sure that the same has to be delegated to UI thread using Dispatcher. As the controls created using UI Thread can not be accessed from other thread.
//Access the Grid Header Controls
Dispatcher.BeginInvoke(delegate
{
    var hyperlinkControl = GetChildControl(dataGrid1, "hlSort");
    var checkControl = GetChildControl(dataGrid1, "chkSelectAll");
});



European Silverlight 6 Hosting :: Retrieving Data in Silverlight: Where is my data?

clock March 27, 2019 09:46 by author Peter

So I was plugging right along in Silverlight using LINQ to asynchronously pull data from our database into my C# code. Everything was going great until I attempted to pull data from one table and its related tables all in one query. Here is what I found which resolved my data problem.

In my Library class I have the following code that enables my ASP.NET code to query a User by UserID and return a User object along with their UserFavorites and Illustration objects. This gives me everything I need to know about the user and their favorite illustrations.
public IQueryable<MyLibrary.User> GetUserByID(int userID)

{
    return myContext.Users.Include("UserFavorites").Include("UserFavorites.Illustration")
        .Where(u => u.UserID == userID);
}

In Silverlight I had a need to perform the same query using LINQ. After much searching on the web I found the two things that were needed to make this happen.
1. Use "Expand" instead of "Include"

2. Instead of "UserFavorites.Illustration" replace the "." with a "/" to get "UserFavorites/Illustration".

    int userID = 0;
    var qUser = ((DataServiceQuery<User>)(from myUser in service.Users
                  where myUser.UserID.Equals(userID)
                  select myUser))
                .Expand("UserFavorites")
                .Expand("UserFavorites/Illustration");

Now I have all of my data and I am happy once again. Using the Expand on my query is very nice in that I can get all of my data in one asynchronous call.

HostForLIFE.eu Silverlight 6 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.



European Silverlight 6 Hosting - Nederland :: Silverlight 5 Viewbox Control

clock March 15, 2019 09:45 by author Peter

This article will explore how to use the ViewBox control in Silverlight 6. The ViewBox control allows you to place a child control such as Image within it in such a way that it will be scaled appropriately to fit the available without any distortion. It is typically used in 2D graphics.

We will begin with creating a new Silverlight 6 project. Modify the existing XAML code of MainPage.xaml so that a Grid of 1 column and three rows is created. The code for the same is shown below:

<UserControl x:Class="SilverlightDemo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006 mc:Ignorable="d" xmlns:sdk=http://schemas.microsoft.com/winfx/2006/xaml/presentation/ sdk HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Grid x:Name="LayoutRoot" Background="White" Height="300" Width="300">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
    </Grid>
</UserControl>

Drag and drop the Viewbox control from the Toolbox into the XAML code between the <Grid></Grid> tags. Specify its row and column in the grid to be 0. The resulting code is seen below.

<UserControl x:Class="SilverlightDemo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006 mc:Ignorable="d" xmlns:sdk=http://schemas.microsoft.com/winfx/2006/xaml/presentation/ sdk HorizontalAlignment="Stretch" VerticalAlignment="Stretch">        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
         <controls:Viewbox Grid.Row="0" Grid.Column="0" Height="120" Width="120">
  </controls:Viewbox
    </Grid>
</UserControl>

Drag and drop the Viewbox control from the Toolbox into the XAML code between the <Grid></Grid> tags. Specify its row and column in the grid to be 0. The resulting code is seen below.

<UserControl x:Class="SilverlightDemo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006 mc:Ignorable="d" xmlns:sdk=http://schemas.microsoft.com/winfx/2006/xaml/presentation/ sdk HorizontalAlignment="Stretch" VerticalAlignment="Stretch">        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
         <controls:Viewbox Grid.Row="0" Grid.Column="0" Height="120" Width="120">
  </controls:Viewbox
    </Grid>
</UserControl>

Right click on the project name in the Solution Explorer pane and select Add Existing Item option. Choose the image "Winter.jg" from the My Documents\My Pictures\Sample Pictures folder.

Drag and drop an Image control in between the <controls:ViewBox> and </controls:ViewBox> tag and modify its code as shown below, to specify its source and size.

    <Grid x:Name="LayoutRoot" Background="White" Height="300" Width="300">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
         <controls:Viewbox Grid.Row="0" Grid.Column="0" Height="120" Width="120">
            <Image Source="Winter.jpg" Height="40" Width="40"></Image>
        </controls:Viewbox>
    </Grid>

Drag and drop another ViewBox and then an Image control in between the second <controls:ViewBox> and </controls:ViewBox> tag.

Modify the XAML as shown below:

    <Grid x:Name="LayoutRoot" Background="White" Height="300" Width="300">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
         <controls:Viewbox Grid.Row="0" Grid.Column="0" Height="120" Width="120">
            <Image Source="Winter.jpg" Height="40" Width="40"></Image>
        </controls:Viewbox>
<controls:Viewbox Grid.Row="1" Grid.Column="0" Height="70" Width="90">
    <Image Source="Winter.jpg" Height="40" Width="40"></Image></controls:Viewbox
    </Grid>

Save the solution, build and execute it. When you see the output, you will observe that the two images show no distortion whatsoever though their height and width are not the same. This has happened because of the ViewBox.

HostForLIFE.eu Silverlight 6 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.



Silverlight 6 Hosting Netherland - HostForLIFE.eu :: Image Brush in Silverlight

clock March 1, 2019 11:07 by author Peter

This article demonstrates how to create and use an image brush in Silverlight using XAML and C#.

z

Image Brush
An image brush paints an area with an image. The ImageSource property represents the image to be used during the painting by an image brush. The ImageBrush object represents an image brush.

Creating an Image Brush
The ImageBrush element in XAML creates an image brush. The ImageSource property of the ImageBrush represents the image used in the painting process.

The following code snippet creates an image brush and sets the ImageSource property to an image.
<ImageBrush ImageSource="dock.jpg" />


We can fill a shape with an image brush by setting a shape's Fill property to the image brush. The code snippet in Listing 1 creates a rectangle shape sets the Fill property to an ImageBrush.
<Rectangle
    Width="200"
    Height="100"
    Stroke="Black"
    StrokeThickness="4">
    <Rectangle.Fill>
        <ImageBrush ImageSource="dock.jpg" />
    </Rectangle.Fill>
</Rectangle>

Listing 1
The CreateAnImageBrush method listed in Listing 2 draws same rectangle with an image brush in Figure 1 dynamically.
/// <summary>
/// Fills a rectangle with an ImageBrush
/// </summary>
public void CreateAnImageBrush()
{
    // Create a Rectangle

    Rectangle blueRectangle = new Rectangle();
    blueRectangle.Height = 100;
    blueRectangle.Width = 200;

     // Create an ImageBrush
    ImageBrush imgBrush = new ImageBrush();
     imgBrush.ImageSource =
        new BitmapImage(new Uri(@"Dock.jpg", UriKind.Relative));
     // Fill rectangle with an ImageBrush

    blueRectangle.Fill = imgBrush;

    // Add Rectangle to the Grid.
    LayoutRoot.Children.Add(blueRectangle);
}

HostForLIFE.eu Silverlight 6 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.

 



Silverlight 6 Hosting Netherland - HostForLIFE.eu :: How to Display a Pop Up Layer in Web Page using Silverlight

clock January 25, 2019 11:55 by author Peter

In this tutorial, you will learn how to show a non-annoying popup layer within a Silverlight web page.

Let's follow these steps:

Step 1

Add a button to your xaml page as shown below:

<Grid x:Name="LayoutRoot" Background="White" >
<Button Width="100" Height="50" x:Name="showPopup"
Click="showPopup_Click" Content="Show Popup" />
</Grid>

Step 2

Then, add the following code to your code behind file (page.xaml.cs):

Popup p = new Popup();
private void showPopup_Click(object sender, RoutedEventArgs e)
{

1. Create a panel control to host other controls
    StackPanel panel1 = new StackPanel();
    panel1.Background = new SolidColorBrush(Colors.Gray);

2. Create a button
    Button button1 = new Button();
    button1.Content = "Close";
    button1.Margin = new Thickness(5.0);
    button1.Click += new RoutedEventHandler(button1_Click);

3. Create a text label
    TextBlock textblock1 = new TextBlock();
    Textblock1.Text = "The popup control";
    Textblock1.Margin = new Thickness(5.0);

4. Add text label and button to the panel
    panel1.Children.Add(textblock1);
    panel1.Children.Add(button1);

Step 3

Now, make the panel a child of the popup so that the panel will be shown within the Popup when displayed:

   p.Child = panel1;

And you can set a position:

 p.VerticalOffset = 25;
   p.HorizontalOffset = 25;

Use this code to show the popup:

p.IsOpen = true;
}

void button1_Click(object sender, RoutedEventArgs e)
{

Then, to close the popup, follow this code:

// Close the popup.
   p.IsOpen = false;
}

Step 4

Now run the application. You can see the page with a button. When you click on the button, a popup layer will appear with a text label and a button in it. When you click on the button in the popup, it will close the popup.

HostForLIFE.eu Silverlight 6 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.



About HostForLIFE.eu

HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.

We have offered the latest Windows 2016 Hosting, ASP.NET Core 2.2.1 Hosting, ASP.NET MVC 6 Hosting and SQL 2017 Hosting.


Tag cloud

Sign in