European Silverlight 4 & Silverlight 5 Hosting BLOG

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

Silverlight 6 with Free ASP.NET Hosting - HostForLIFE.eu :: How to Get User's Geo Location

clock June 3, 2015 06:19 by author Rebecca

In this tutorial, I'm going to tell you about getting the Geo-location of the user of your Silverlight Application. We will use Javascript as the core of our scenario.

Step 1

You will be calling following JavaScript API here in the aspx page:

 <script language="JavaScript" type="text/javascript" src="http://j.maxmind.com/app/geoip.js"></script>

Now there are functions in that API that will return the Latitude, Longitude, country name and whatever we demand. Some of the function are viz:

  •     geoip_country_code()
  •     geoip_country_name()
  •     geoip_latitude()
  •     geoip_longitude()

Instead of just calling these functions directly in Silverlight, we will write our functions that will call these functions and return whatever these functions return. For example here is the custom script written just beneath the API call:

<script type="text/javascript">

   function GetCountryCode() {

   return geoip_country_code();

      }

  function GetCountryName() {

  return geoip_country_name();

      }       

  function GetLatitude() {

  return geoip_latitude();

       }

   function GetLongitude() {

   return geoip_longitude();

        }
 </script>

So finally, here is what the aspx page with API call and the scripts that you have written:

 <script type="text/javascript" src="Silverlight.js"></script>

 <script language="JavaScript" type="text/javascript" src="http://j.maxmind.com/app/geoip.js"></script>

 <script type="text/javascript">

 function GetCountryCode() {

 return geoip_country_code();

   }

 function GetCountryName() {

 return geoip_country_name();

   }

 function GetLatitude() {

 return geoip_latitude();

    }

 function GetLongitude() {

return geoip_longitude();

      }   
 </script>

Step 2

Now let's move toward your mainpage. You must make the JavaScript function call from the main page. As you all know, it is a simple method call:

HtmlPage.Window.Invoke("<JavaScriptMethod>");

The XAML of the main page consists of four Text Blocks, each to pursue the respective value for Country code, Country Name, Latitude and Longitude.

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

 <TextBlock x:Name="txtCountryCode" Margin="20 20 0 0"/>

 <TextBlock x:Name="txtCountryName" Margin="20 40 0 0"/>

 <TextBlock x:Name="txtLatitude" Margin="20 60 0 0"/>

  <TextBlock x:Name="txtLongitude" Margin="20 80 0 0"/>

  </Grid>


For giving the corresponding values to the Blocks, you need to write some C# in the code behind and in the constructor of the main page.

    public MainPage()

    {

   InitializeComponent();

  txtCountryCode.Text = HtmlPage.Window.Invoke("GetCountryCode").ToString();

  txtCountryName.Text = HtmlPage.Window.Invoke("GetCountryName").ToString();

  txtLatitude.Text = HtmlPage.Window.Invoke("GetLatitude").ToString();

  txtLongitude.Text = HtmlPage.Window.Invoke("GetLongitude").ToString();  


That's it. Just hit F5 and run the project. It will provide you all the information you've requested in the code.

Happy coding!

Silverlight 6 with Free ASP.NET Hosting
Try our Silverlight 6 with Free ASP.NET Hosting today and your account will be setup soon! You can also take advantage of our Windows & ASP.NET Hosting support with Unlimited Domain, Unlimited Bandwidth, Unlimited Disk Space, etc.



Silverlight 6 with Free ASP.NET Hosting - HostForLIFE.eu :: How to Drag & Drop a Rich TextBox in Silverlight

clock May 27, 2015 05:41 by author Rebecca

In this post, I will show you how to drag and drop a RichTextBox in Silverlight. For your information, the code below also could be adapted to drag any element around.

The XAML that you are going to use here consists of one Border control which contains your Rich TextBox.  The Border control has several event handlers defined to capture the Mouse Buttons and Movement:

<UserControl x:Class="TreeViewDragAndDrop.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:liquidTreeView="clr-namespace:Liquid;assembly=Liquid.TreeView"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White" VerticalAlignment="Top" HorizontalAlignment="Left">
        <liquidTreeView:Tree x:Name="tree" EnableDragAndDrop="true" Drop="Tree_Drop" Width="300" Height="151" Margin="4">
            <liquidTreeView:Tree.Nodes>
                <liquidTreeView:Node ID="0" Title="Root" Icon="images/folder.png" IconExpanded="images/folderOpen.png">
                    <liquidTreeView:Node.Nodes>
                        <liquidTreeView:Node ID="1" Title="Folder 1" Icon="images/folder.png" IconExpanded="images/folderOpen.png">
                            <liquidTreeView:Node.Nodes>
                                <liquidTreeView:Node ID="10" Title="File 1.doc" Icon="images/doc.png" />
                                <liquidTreeView:Node ID="11" Title="File 2.doc" Icon="images/doc.png" />
                            </liquidTreeView:Node.Nodes>
                        </liquidTreeView:Node>
                        <liquidTreeView:Node ID="2" Title="Folder 2" Icon="images/folder.png" IconExpanded="images/folderOpen.png">
                            <liquidTreeView:Node.Nodes>
                                <liquidTreeView:Node ID="20" Title="File 3.doc" Icon="images/doc.png" />
                                <liquidTreeView:Node ID="21" Title="File 4.doc" Icon="images/doc.png" />
                                <liquidTreeView:Node ID="21" Title="File 5.doc" Icon="images/doc.png" />
                            </liquidTreeView:Node.Nodes>
                        </liquidTreeView:Node>
                    </liquidTreeView:Node.Nodes>
                </liquidTreeView:Node>
            </liquidTreeView:Tree.Nodes>
        </liquidTreeView:Tree>
    </Grid>
</UserControl>

The C# for this tutorial contains the 3 event handlers which do the actual work here. Remember your Border control has been placed on a Canvas object which allows you to specify using absolute coordinates where the Border should be rendered:

using System.Windows.Controls;

using Liquid;

namespace TreeViewDragAndDrop
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
        }

        private void Tree_Drop(object sender, TreeEventArgs e)
        {
            e.DropAction = Tree.DropActions.InsertAfter;
        }
    }
}

As you can see, you have detected when the mouse is clicked over the border (using the Border_MouseLeftButtonDown event) and set _mouseDown = true, you also call myBorder.CaptureMouse(), this is important as it tells Silverlight to route allmouse events to the Border control.

When the user moves the mouse the new position of the Border is calculated by adding the amount of pixels the cursor has moved since the last MouseMove event was called.  This is all handled in the Border_MouseMove event. And the final mouse event handler, Border_MouseLeftButtonUp clears the _mouseDown flag releases the capture lock we set previously.

Silverlight 6 with Free ASP.NET Hosting
Try our Silverlight 6 with Free ASP.NET Hosting today and your account will be setup soon! You can also take advantage of our Windows & ASP.NET Hosting support with Unlimited Domain, Unlimited Bandwidth, Unlimited Disk Space, etc.



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