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 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.



Silverlight 6 Hosting with Free ASP.NET Hosting - HostForLIFE.eu :: How to Use Isolated Storage Feature in Silverlight

clock May 20, 2015 07:05 by author Rebecca

In Silverlight, due to security issues, an ordinary application is NOT allowed to write to/read from arbitrary locations on the file system (except through some dialogs). How do we save those data temporarily inside the file system (such as a text file) so that the application can retrieve and use it later? Sure you can use the Isolated Storage Feature in Silverlight.

Isolated Storage allows you to access to a small segment of hard-disk space, which you will have no idea where the files are being stored. Usually you need it when you want to store small amounts of not essential information (eg: user-specific details, user preferences). Especially when you want to communicate with the web service, having the back-up storage can enable to page to reload the data without the user retyping again.

You can save DateTime into the memory and can retrieve it using Isolated Storage. Here is the example:'

Step 1

First, you need to open an Isolated Store. You can use the IsolatedStorageFile class and call the static IsolatedStorageFile.GetUserStoreForApplication() method:

IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()

With this, you can use System.IO namespace to write and read data.

Step 2

Now, you have to write DateTime into memory as follows:

using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream stream = store.CreateFile("Date.txt"))
                {
                    StreamWriter writer = new StreamWriter(stream);
                    writer.Write(DateTime.Now);
                    writer.Close();
                }
                textBox1.Text = "Data written";
            }

Step 3

Then, you can retrieve DateTime from memory, use this following code:

using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream stream = store.OpenFile("Date.txt", FileMode.Open))
                {
                    StreamReader reader = new StreamReader(stream);
                    textBox2.Text = reader.ReadLine();
                    reader.Close();
                }
            }

Simple right?

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 Create Web Service in SIlverlight

clock May 13, 2015 06:35 by author Rebecca

Have you ever wonder, how do you actually program on the server side to handle the requests by the client? And how do a client know that his uploading request has been complete before proceeding to the another one? There are several approaches for the solution. Here, I introduce how to create WCF (Windows Communication Foundation) web service in Silverlight, which involves lesser code.

 

How to Create a WCF Web Service:

a) Right-click your ASP.NET website, add the Silverlight-enabled WCF Service template.
b) Create a new method (with the OperationContract attribute)
c) Right-click your Silverlight project -> Add Service. Click Discover button. Then Click OK button.

After you have add a service reference, Visual Studio actually creates a proxy class that you can interface with the Web Service. The proxy class is named after the original Service class name + the world Client at the end.

How to use or call the WCF Web Service:

Import the namespace for the service reference. In Silverlight, all web services are asynchronous. That means after you have called the method to fire it, it returns immediately and proceed to the next line of code. When the response is received, you handle the event inside the MethodNameCompleted.

A standard order of using the web service is as follow:

    Service1Client myService = new Service1Client();
    myService.BeginUploadCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(myService_BeginUploadCompleted);
    myService.BeginUploadAsync(...);

That's all, simple, right?

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 Add Data into a DataGrid Dynamically

clock May 6, 2015 06:48 by author Rebecca

The DataGrid control in the Silverlight is one of the very important and useful controls. It is highly customizable with support of sorting, editing, grouping and paging. Here, this tutorial will talk about how to add data dynamically into the DataGrid. Specifically, it lets the user to input the Staff data during run time, as many as he/she likes.

Here the example of output in Silverlight that I have made:

Add a DataGrid by Drag and Drop, then add 3 columns

Note that each column has a binding path to the variable it displays.

    <sdk:DataGrid AutoGenerateColumns="False" Height="206" HorizontalAlignment="Left" Margin="12,65,0,0" Name="myDataGrid" VerticalAlignment="Top" Width="352" >
                <sdk:DataGrid.Columns>
                    <sdk:DataGridTextColumn Header="Name" Binding="{Binding Path=staffName, Mode=OneWay}" Width="120"></sdk:DataGridTextColumn>
                    <sdk:DataGridTextColumn Header="ID" Binding="{Binding Path=staffID, Mode=OneWay}" Width="100"></sdk:DataGridTextColumn>
                    <sdk:DataGridTextColumn Header="Age" Binding="{Binding Path=staffAge, Mode=OneWay}" Width="100"></sdk:DataGridTextColumn>
                </sdk:DataGrid.Columns>
            </sdk:DataGrid>

Create a class for the Data Object. Eg: Staff.cs

Define the variables which used in the data binding in the class

            public string staffName { get; set; }
            public string staffID { get; set; }
            public string staffAge { get; set; }

            public Staff(string _name, string _id, string _age)
            {
                staffName = _name;
                staffID = _id;
                staffAge = _age;
            }

Add Button

This is the button that fires the event assigning the values. Here, I use ObservableCollection<Staff> to store the data inside the DataGrid. (Define it as a global variable).

            private void AddButton_Click(object sender, RoutedEventArgs e)
            {
                //Get the text from the textboxes
                string _name = textBox1.Text;
                string _id = textBox2.Text;
                string _age = textBox3.Text;
              
                //Add them inside the ObservableCollection variable: collectionInRow
                collectionInRow.Add(new Staff(_name, _id, _age));

                //This is the Most Vital line: assign the Source of the dataGrid to the ObservableCollection
                myDataGrid.ItemsSource = collectionInRow;
            }

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