Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Posts Tagged ‘Visual Studio 2010’

File Drag and Drop in Silverlight4

Posted by Viral Sarvaiya on November 2, 2010


Silverlight 4 have drag and drop functionality. here i demostrate that functionality.
first create new project named “DragAndDropImage”.
You have to add references “Microsoft.Expression.Interactions”, “system.windows.interactivity” and “system.windows.browser”

Silverlight is too fun, when coding gets this simple. So the image can now be dragged around. Now lets add the drop feature.

For making everything very simple, I will just now add AllowDrop=”True” and then add a drop handler to handle any file that is being dropped.

Now, lets move to the code behind. For demonstration purpose, I will use images from deviantArt gallery.

In DragAndDropImageTest.xaml file


<UserControl x:Class="SilverLightTest.DragAndDropImageTest"
 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:i="http://schemas.microsoft.com/expression/2010/interactivity"
 xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
 d:DesignHeight="300" d:DesignWidth="400">

 <Grid x:Name="LayoutRoot" AllowDrop="True" Drop="LayoutRoot_Drop" Background="Black">
 <StackPanel x:Name="DropCollection" Height="100" Margin="10,30,10,10" VerticalAlignment="Top" HorizontalAlignment="Center" Orientation="Horizontal">
 </StackPanel>
 <Image x:Name="DropImage" Margin="0,100,0,0" Width="400" Cursor="Hand">
 <i:Interaction.Behaviors>
 <ei:MouseDragElementBehavior ConstrainToParentBounds="True"></ei:MouseDragElementBehavior>
 </i:Interaction.Behaviors>
 </Image>
 </Grid>
</UserControl>

In DragAndDropImageTest.xaml.cs file


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.Media.Imaging;
using System.Collections.ObjectModel;
using System.Windows.Media;
using System.Windows.Browser;
using System.IO;

namespace SilverLightTest
{
 public partial class DragAndDropImageTest : UserControl
 {
 public DragAndDropImageTest()
 {
 InitializeComponent();
 }

 private void LayoutRoot_Drop(object sender, DragEventArgs e)
 {
 if (e.Data != null)
 {
 //reading the drop file
 FileInfo file = ((FileInfo[])e.Data.GetData(DataFormats.FileDrop)).FirstOrDefault();

 //creating a bitmapImage
 BitmapImage img = new BitmapImage();

 using (Stream s = file.OpenRead())
 {
 img.SetSource(s);
 }

 //setting the image in xaml
 DropImage.Source = img;

 Image newImage = new Image { Source = img };
 newImage.MouseLeftButtonDown += (a, b) =>
 {
 DropImage.Source = newImage.Source;
 };

 DropCollection.Children.Add(newImage);
 }
 }
 }
}

Run the application and check with dropping the image file in browser.

 

Enjoy Coding…..

Posted in ASP.NET, Silverlight | Tagged: , , , , , | 1 Comment »

Beginners Guide to Visual Studio LightSwitch

Posted by Viral Sarvaiya on October 26, 2010


SIMPLEST WAY TO BUILD BUSINESS APPLICATIONS FOR THE DESKTOP, WEB AND CLOUD

Microsoft Visual Studio LightSwitch gives you a simpler and faster way to create professional-quality business applications for the desktop, the web, and the cloud. LightSwitch is a new addition to the Visual Studio family. Visit this page often to learn more about this exciting product.

Visual Studio LightSwitch is a new tool for building data-driven Silverlight Application using Visual Studio IDE. It automatically generates the User Interface for a DataSource without writing any code. You can write a small amount of code also to meet your requirement.

Fore More Details :

http://www.microsoft.com/visualstudio/en-us/lightswitch

http://msdn.microsoft.com/hi-in/lightswitch/default%28en-us%29.aspx

Make a simple application demo…

http://www.silverlightshow.net/items/Beginners-Guide-to-Visual-Studio-LightSwitch-Part-1.aspx

http://www.silverlightshow.net/items/Beginners-Guide-to-Visual-Studio-LightSwitch-Part-2.aspx

Posted in ASP.NET | Tagged: , , , , | Leave a Comment »

Simple Data Binding in Silverlight

Posted by Viral Sarvaiya on October 1, 2010


Simple Data Binding in Silverlight
To help explain data binding in Silverlight, let’s build a very simple application. The application will include a Book object that contains two properties: Title and ISBN. These properties will be bound to two TextBox controls.

1.  Create a new Silverlight application in Visual Studio 2010. Name the project BasicDataBinding, and allow Visual Studio to create a Web application project to host your application.

2. Edit the MainPage.xaml file to define two columns and six grid rows. Place a TextBlock in each row in column 1 and a TextBox in each row in column 2. Also add some margins and some alignment assignments to improve the layout.

The code for the page follows:


<Grid x:Name="LayoutRoot" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="Book Title"
VerticalAlignment="Center"
Margin="5" />
<TextBlock Text="ISBN-13"
VerticalAlignment="Center"
Margin="5"
Grid.Row="1" />
<TextBox Text="{Binding Title}"
Height="24"
Margin="5"
Grid.Column="1" />
<TextBox Text="{Binding ISBN}"
Height="24"
Margin="5"
Grid.Column="1" Grid.Row="1" />
<TextBlock Text="Book Title"
VerticalAlignment="Center"
Margin="5"
Grid.Row="2" />
<TextBlock Text="ISBN-13"
VerticalAlignment="Center"
Margin="5"
Grid.Row="3" />

<TextBox Text="{Binding Title}"
Height="24"
Margin="5"
Grid.Column="1" Grid.Row="2" />
<TextBox Text="{Binding ISBN}"
Height="24"
Margin="5"
Grid.Column="1" Grid.Row="3" />

</Grid>

3. Next, edit the code behind, MainPage.xaml.cs. Add a Loaded event handler for the application, which will fire when the application is loaded by the client. This is accomplished with the following source code:


public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Page_Loaded);
}

void Page_Loaded(object sender, RoutedEventArgs e)
{

}
}

Now you need to add a class to define a Book object. Below the MainPage class, add the following class definition:


namespace BasicDataBinding
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Page_Loaded);
}
void Page_Loaded(object sender, RoutedEventArgs e)
{
}
}
public class Book
{
public string Title { get; set; }
public string ISBN { get; set; }
}
}

4. Now that you have Book defined, you need to create an instance of Book and set it to the LayoutRoot’s DataContext, as follows:


void Page_Loaded(object sender, RoutedEventArgs e)
{
Book b = new Book()
{
Title = "Beginning Silverlight 4: From Novice to Professional",
ISBN = "978-1430229889"
};
this.LayoutRoot.DataContext = b;
}

When you set up binding definitions for different controls, the controls do not know where they are going to get their data. The DataContext property sets the data context for a control that is participating in data binding.The  DataContext property can be set directly on the control. If a given control does not have a DataContext property specified, it will look to its parent for its data context. The nice thing about this model is that if you look above in the XAML for the page, you will see little indication of where the controls are getting their data. This provides an extreme level of code separation, allowing designers to design XAML UIs and developers to work alongside the designers, defining the specifics of how the controls are bound to their data sources.

5. At this point, you can go ahead and start debugging the application. If all goes well, you will see the four text boxes populated with the data from the Book’s instance

6.  With the application running, change the book title in the first text box to just “Beginning Silverlight,” by removing the “From Novice to Professional.” You might expect that, since the third text box is bound to the same data, it will automatically update to reflect this change. However, a couple of things need to be done to get this type of two-way binding to work. One problem is that, currently, the Book class does not support notifying bound clients of changes to its properties. In other words, when a property changes in Book, the class will not notify the TextBox instances that are bound to the class of the change. You could take care of this by creating a change event for each property. This is far from ideal; fortunately, there is an interface that a class can implement that handles this for you. This interface is known as INotifyPropertyChanged. Let’s use it.

7.  Modify the Book class definition to inherit from INotifyPropertyChanged. Notice that when you inherit from INotifyPropertyChanged, you need to add using System.ComponentModel. Luckily, Visual Studio will help you with this.

Next, you can let Visual Studio do some more work for you. After adding the using System.ComponentModel statement, right-click INotifyPropertyChanged and select Implement Interface ➤ Implement Interface from the pop-up menu

Now Visual Studio has added a new public event to your class:


public class Book : INotifyPropertyChanged
{
public string Title { get; set; }
public string ISBN { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}

8. Next, you need to create a convenience method that will fire the PropertyChanged event. Call it FirePropertyChanged, as shown in the following code:


public class Book : INotifyPropertyChanged
{
public string Title { get; set; }
public string ISBN { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
void FirePropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(property));
}
}
}

9. Now you need to extend the simplified properties by adding private members and full get/set definitions to define the get and set operations, as shown in the following code. The get is just like a normal get operation, where you simply return the internal member value. For the set, you first set the internal member value, and then call the FirePropertyChanged method, passing it the name of the property.


public class Book : INotifyPropertyChanged
{
private string _title;
private string _isbn;
public string Title
{
get
{
return _title;
}
set
{
_title = value;
FirePropertyChanged("Title");
}
}
public string ISBN
{
get
{
return _isbn;
}
set
{
_isbn = value;
FirePropertyChanged("ISBN");
}
}
public event PropertyChangedEventHandler PropertyChanged;
void FirePropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(property));
}
}
}

With this completed, your class is set up to notify bound clients of changes to the Title and ISBN properties. But you still need to take one more step. By default, when you bind a source to a target, the BindingMode is set to OneWay binding, which means that the source will send the data to the target, but the target will not send data changes back to the source. In order to get the target to update the source, you need to implement two-way (TwoWay) binding.

10. To change to two-way binding, add the Mode=TwoWay parameter when defining the {Binding} on a control, as follows:


<TextBlock Text="Book Title"
VerticalAlignment="Center"
Margin="5" />
<TextBlock Text="ISBN-13"
VerticalAlignment="Center"
Margin="5"
Grid.Row="1" />
<TextBox Text="{Binding Title, Mode=TwoWay}"
Height="24"
Margin="5"
Grid.Column="1" />
<TextBox Text="{Binding ISBN, Mode=TwoWay}"
Height="24"
Margin="5"
Grid.Column="1" Grid.Row="1" />
<TextBlock Text="Book Title"
VerticalAlignment="Center"
Margin="5"
Grid.Row="2" />
<TextBlock Text="ISBN-13"
VerticalAlignment="Center"
Margin="5"
Grid.Row="3" />
<TextBox Text="{Binding Title, Mode=TwoWay}"
Height="24"
Margin="5"
Grid.Column="1" Grid.Row="2" />
<TextBox Text="{Binding ISBN, Mode=TwoWay}"
Height="24"
Margin="5"
Grid.Column="1" Grid.Row="3" />

11.  Rebuild and run your application. Update any of the fields, and leave the focus on the control. You’ll see that the two-way binding is triggered, and the corresponding field is also updated,

Congratulations! You have just created a Silverlight application that allows for two-way data binding.

Posted in ASP.NET, Silverlight | Tagged: , , , , , | Leave a Comment »

Absolute Beginners Guide to Entity Framework

Posted by Viral Sarvaiya on July 22, 2010


The Entity Framework bridges the gap between how developers commonly manipulate conceptual objects (customers, orders, products; posts, tags, members; wall posts, private messages, friend connections) and the way data is actually stored (records in database tables).  The technical term for a tool that provides this abstraction is object relational mapper (ORM).  ORMs help developers be more efficient and focused, since they don’t need to spend brain cycles thinking about how to communicate with the database.  It also means that the code is more portable – switching database software requires changing a setting in the ORM, not a rewrite of the whole codebase to match the new database’s dialect.  As someone who has programmed using the ORM in Django, I can tell you how such a tool makes development less tedious and more enjoyable when you don’t have to consider SELECTS and INSERTS.  In fact, I’ve never before written a line of SQL, yet I was able to build a rich web application thanks to an ORM

for more details :

http://blogs.msdn.com/b/adonet/archive/2010/07/19/absolue-beginners-guide-to-entity-framework.aspx

Posted in ASP.NET | Tagged: , , | Leave a Comment »

New Features in Visual Studio 2010 and the .NET Framework 4.0

Posted by Viral Sarvaiya on May 29, 2010


Visual Studio 2008 may be better than sliced bread, but the development team at Microsoft has already been working on the next release. They have recently given us Visual Studio 2010 and the .NET Framework 4.0 as a Community Technology Preview (CTP); it boasts several features that would appeal to developers.

This article won’t go into every single feature, but will go into features that are the most relevant to .NET developers. Please note that because this is a CTP, it doesn’t mean that the final release will be exactly as you see in the CTP or as is described here. I can go over the features roughly as follows:

New Features in the Visual Studio 2010 IDE and .NET Framework 4.0

  • Call Hierarchy of methods
  • A New Quick Search
  • Multi-targeting more accurate
  • Parallel Programming and Debugging
  • XSLT Profiling and Debugging
  • The XSD Designer

New ASP.NET features

  • Static IDs for ASP.NET Controls
  • The Chart control
  • Web.config transformation

New VB.NET features

  • Auto Implemented Properties for VB.NET
  • Collection Initializers
  • Implicit Line Continuations
  • Statements in Lambda Expressions

New C# features

  • Dynamic Types
  • Optional parameters
  • Named and Optional Arguments

For more details :

http://www.codeguru.com/vb/vb_internet/aspnet/article.php/c15645__1/New-Features-in-Visual-Studio-2010-and-the-NET-Framework-40.htm

http://msdn.microsoft.com/en-us/library/bb383815.aspx

http://msdn.microsoft.com/en-us/library/bb386063.aspx

Posted in Visual Studio | Tagged: , , , , , , | Leave a Comment »

 
%d bloggers like this: