Windows Phone uses concept of Local Database and by implementing LINQ to SQL you can create 100% relational database driven application.
To understand more on this refer http://msdn.microsoft.com/en-us/library/hh202860(VS.92).aspx
So what you need is the good old tie up between Silverlight and LINQ to SQL. Here we go.
You need to refer System.Data.Linq assembly and use the below two namespaces to define the model.
using System.Data.Linq; using System.Data.Linq.Mapping;
After that you need to define the model by hand.
//Model Class [Table] public class EmailClass { [Column(IsDbGenerated = true, IsPrimaryKey = true)] public int Id { get; set; } [Column()] public string EmailAddress { get; set; } }
Once the model is defined you then need the DataContext
public class EmailContext : DataContext { public EmailContext(string sConnectionString) : base(sConnectionString) { } public Table<EmailClass> Emails { get { return this.GetTable<EmailClass>(); } } }
Connection string would look like,
string ConnectionString = "Data Source=isostore:/EmailDB.sdf";
Some reusable methods to Add and Display
public IList<EmailClass> GetEmails() { List<EmailClass> emails = new List<EmailClass>(); using (var db = new EmailContext(ConnectionString)) { var query = from e in db.Emails select e; emails = query.ToList(); } return emails; } public void AddEmail(EmailClass _email) { using (var db = new EmailContext(ConnectionString)) { db.Emails.InsertOnSubmit(_email); db.SubmitChanges(); } }
Then comes UI which would help you to save and display data,
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <Grid.RowDefinitions> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="*"></RowDefinition> </Grid.RowDefinitions> <StackPanel Grid.Row="0" Orientation="Horizontal"> <TextBox Width="300" x:Name="txtEmail"></TextBox> <Button HorizontalAlignment="Right" x:Name="btnSave" Content="Save" Width="120" Click="btnSave_Click"></Button> </StackPanel> <ListBox x:Name="lstEmails" Grid.Row="1" > <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding Path=EmailAddress}"></TextBlock> <TextBlock Text="-----"></TextBlock> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid>
The complete code is as below,
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 Microsoft.Phone.Controls; using System.Data.Linq; using System.Data.Linq.Mapping; namespace WP7_Samples { public partial class LocalDB_WP7 : PhoneApplicationPage { const string ConnectionString = "Data Source=isostore:/EmailDB.sdf"; public LocalDB_WP7() { InitializeComponent(); InputScope _scope = new InputScope(); InputScopeName _scopeName = new InputScopeName(); _scopeName.NameValue = InputScopeNameValue.EmailNameOrAddress; _scope.Names.Add(_scopeName); txtEmail.InputScope = _scope; using (var db = new EmailContext(ConnectionString)) { if (!db.DatabaseExists()) db.CreateDatabase(); LoadData(); } } public void LoadData() { lstEmails.ItemsSource = GetEmails(); } public IList<EmailClass> GetEmails() { List<EmailClass> emails = new List<EmailClass>(); using (var db = new EmailContext(ConnectionString)) { var query = from e in db.Emails select e; emails = query.ToList(); } return emails; } public void AddEmail(EmailClass _email) { using (var db = new EmailContext(ConnectionString)) { db.Emails.InsertOnSubmit(_email); db.SubmitChanges(); } } private void btnSave_Click(object sender, RoutedEventArgs e) { if (txtEmail.Text.Trim() != "") { var emailObj = new EmailClass() { EmailAddress = txtEmail.Text.Trim() }; AddEmail(emailObj); } LoadData(); txtEmail.Text = ""; } } //Model Class [Table] public class EmailClass { [Column(IsDbGenerated = true, IsPrimaryKey = true)] public int Id { get; set; } [Column()] public string EmailAddress { get; set; } } public class EmailContext : DataContext { public EmailContext(string sConnectionString) : base(sConnectionString) { } public Table<EmailClass> Emails { get { return this.GetTable<EmailClass>(); } } } }
Namoskar!!!
Launching browser from your Phone application is often required. The common term could be “Shell” for those who came to classic VB age like me. Later in .NET it was wrapped under System.Diagnostic.Process. In Windows Phone 7 we might need such functionality while working with RSS kind of application where user can click on the link and open it in browser.
WebBrowserTask wbTask = new WebBrowserTask(); //URL = http://create.msdn.com wbTask.Uri = new Uri(textBox1.Text, UriKind.RelativeOrAbsolute); wbTask.Show();
Windows Phone 7 Silverlight project generally starts by opening MainPage.xaml. How to change this?
Under solution explorer, expand Properties and open WMAppManifest.xml and there we can change it
<Tasks> <DefaultTask Name="_default" NavigationPage="LocalDB_WP7.xaml" /> </Tasks>
Namoskar!!!!
You may choose email application to send an email from your phone.
EmailComposeTask MyEmailComposer = new EmailComposeTask(); MyEmailComposer.To = "wriju@contoso.com"; MyEmailComposer.Subject = "Mango Email"; MyEmailComposer.Body = "Welcome to the Magic!!!"; MyEmailComposer.Show();
Sending SMS from application is as simple as below
SmsComposeTask MySMS = new SmsComposeTask(); MySMS.Body = "Hello from Mango"; MySMS.To = "111111"; MySMS.Show();
You may choose photo from your phone through the application
PhotoChooserTask MyPhotoChooser = new PhotoChooserTask(); MyPhotoChooser.Completed += new EventHandler<PhotoResult>(MyPhotoChooser_Completed); MyPhotoChooser.ShowCamera = true; MyPhotoChooser.Show();
If you are composing a text and wanted to send it to the people listed in your contact you can choose from the list. Here is how you can do it.
EmailAddressChooserTask emailAddress = new EmailAddressChooserTask(); emailAddress.Completed += new EventHandler<EmailResult>(emailAddress_Completed); emailAddress.Show();
And to capture the email address you can have this small code.
void emailAddress_Completed(object sender, EmailResult e) { if (e.TaskResult == TaskResult.OK) { MessageBox.Show(e.Email.ToString()); } }
Here is how we can launch the in-built phone and call a number specified by us.
PhoneCallTask PhoneTask = new PhoneCallTask();
PhoneTask.PhoneNumber = "1111111111";
PhoneTask.Show();
Note: You might need to register yourself before downloading. This will be prompted.
If you let people quickly open Bing search with the selected word while navigating your application in your Windows Phone 7.
//Curtsey WP7 Training Kit - MUST HAVE IT!!! WebBrowserTask wbTask = new WebBrowserTask(); string BingURL = @"http://m.bing.com/search?q={0}"; string searchKeyword = textBox2.Text; wbTask.Uri = new Uri(string.Format(BingURL, searchKeyword), UriKind.Absolute); wbTask.Show();
Namosakar!!!
I am happy to announce the upcoming event for Microsoft MPN Partner on
Windows Phone 7 Mango - Platform Overview for Application Development
Date & Time: Aug 29, 11 Pacific time via Office Live Meeting
Registration: https://training.partner.microsoft.com/learning/app/management/LMS_ActDetails.aspx?UserMode=0&ActivityId=752004
Presenter: Wriju Ghosh, Lead Partner Technical Consultant, Microsoft.
Abstract: This session will walk you through the windows Phone Mango platform application development. Explore new platform features and to understand the next application development for it.
See you all in the session.