paiip lab 3

10
Ministerul Educaţiei al Republicii Moldova Universitatea Tehnică a Moldovei Facultatea Calculatoare, Informatică şi Microelectronică Raport Lucrarea de laborator nr. 3 Programarea Aplicațiilor Incorporate și Independente de Platformă Tema: Elaborarea unei aplicații pe platforma Windows Phone A efectuat: St. gr. TI-114 Vasilachi Igor

description

paiip lab3 wp

Transcript of paiip lab 3

Page 1: paiip lab 3

Ministerul Educaţiei al Republicii Moldova

Universitatea Tehnică a Moldovei

Facultatea Calculatoare, Informatică şi Microelectronică

RaportLucrarea de laborator nr. 3

Programarea Aplicațiilor Incorporate și Independente de Platformă

Tema: Elaborarea unei aplicații pe platforma Windows Phone

A efectuat: St. gr. TI-114 Vasilachi Igor

A verificat: Antohi I.

Page 2: paiip lab 3

Chişinău 2013

Sarcina lucrării:

Elaborarea unei aplicații cu următoarelor funcționalități:

Crearea unui RSS reader;

Postarea unui mesaje/status pe Facebook;

Crearea unui reminder;

Efectuarea lucrării:

Pentru a crea un proiect pe platforma Windows Phone este nevoie de descărcat de pe site-ul

oficial de la Microsoft pachetul Windows Phone SDK, standart el vine în format Web Installer. După

ce se va instala acest pachet se va deschide aplicația Microsoft Visual Studio 2010 for Windows

Phone, acesta este un IDE ce ajută la dezvoltarea aplicațiilor pentru Windows Phone.

În următoarele imagini sunt reprezentate funcționalitățile aplicației, fiecare funcționalitate

utilizând o pagină separată.

Imaginea 1 Facebook și Rss reader

2

Page 3: paiip lab 3

Imaginea 2 Reminder-ul

Anexa A: RssReader.cs

namespace RFRWindowsPhone{ public partial class RssReader : PhoneApplicationPage { public RssReader() { InitializeComponent(); } private void loadFeedButton_Click(object sender, System.Windows.RoutedEven-tArgs e) { WebClient webClient = new WebClient();

webClient.DownloadStringCompleted += new DownloadStringCompletedEven-tHandler(webClient_DownloadStringCompleted);

webClient.DownloadStringAsync(new System.Uri("http://feeds.reuters.com/reuters/entertainment")); }

private void webClient_DownloadStringCompleted(object sender, Download-StringCompletedEventArgs e) { if (e.Error != null) { Deployment.Current.Dispatcher.BeginInvoke(() => {

3

Page 4: paiip lab 3

MessageBox.Show(e.Error.Message); }); } else { this.State["feed"] = e.Result;

UpdateFeedList(e.Result); } }

protected override void OnNavigatedTo(System.Windows.Navigation.Navigation-EventArgs e) { if (this.State.ContainsKey("feed")) { if (feedListBox.Items.Count == 0) { UpdateFeedList(State["feed"] as string); } } }

private void UpdateFeedList(string feedXML) { StringReader stringReader = new StringReader(feedXML); XmlReader xmlReader = XmlReader.Create(stringReader); SyndicationFeed feed = SyndicationFeed.Load(xmlReader); Deployment.Current.Dispatcher.BeginInvoke(() => { feedListBox.ItemsSource = feed.Items;

loadFeedButton.Content = "Refresh Feed"; });

}

private void feedListBox_SelectionChanged(object sender, SelectionChangedE-ventArgs e) { ListBox listBox = sender as ListBox;

if (listBox != null && listBox.SelectedItem != null) { SyndicationItem sItem = (SyndicationItem)listBox.SelectedItem;

if (sItem.Links.Count > 0) { Uri uri = sItem.Links.FirstOrDefault().Uri; WebBrowserTask webBrowserTask = new WebBrowserTask(); webBrowserTask.Uri = uri; webBrowserTask.Show(); } } } }}

4

Page 5: paiip lab 3

Anexa B: MainPage.css - Facebook

namespace RFRWindowsPhone{ public partial class MainPage : PhoneApplicationPage { private const string FBApiID = "254928747992451"; private const string ExtendedPermisions = "publish_stream"; private static FacebookClient client;

public MainPage() { client = new FacebookClient();

client.PostCompleted += (o, args) => {

if (args.Error != null) { var error = args.Error as FacebookApiException; if (error.ErrorCode == 190) { Dispatcher.BeginInvoke(() => MessageBox.Show("Eroare de aut-entificare !!")); SaveToken(null); client.AccessToken = null; return; } Dispatcher.BeginInvoke(() => MessageBox.Show(error.Message)); return; }

var result = (IDictionary<string, object>)args.GetResultData();

Dispatcher.BeginInvoke(() => { MessageBox.Show("Messajul a fost postat cu succes!"); Browser.Visibility = System.Windows.Visibility.Collapsed; }); };

if (GetToken() != null) client.AccessToken = GetToken(); InitializeComponent(); }

private void BrowserNavitaged(object sender, System.Windows.Navigation.Navi-gationEventArgs e) { FacebookOAuthResult oauthResult; if (!client.TryParseOAuthCallbackUrl(e.Uri, out oauthResult)) { return; } if (oauthResult.IsSuccess) { //Process result client.AccessToken = oauthResult.AccessToken; SaveToken(client.AccessToken); LoginSucceded();

5

Page 6: paiip lab 3

} else { //Process Error MessageBox.Show(oauthResult.ErrorDescription); Browser.Visibility = System.Windows.Visibility.Collapsed; } }

private void LoginSucceded() { Browser.Visibility = System.Windows.Visibility.Collapsed; PostToWall(); }

private void PostToWall() { var parameters = new Dictionary<string, object>(); parameters["message"] = tbMessage.Text; client.PostAsync("me/feed", parameters); }

private void SaveToken(String token) { if (!IsolatedStorageSettings.ApplicationSettings.Contains("token")) IsolatedStorageSettings.ApplicationSettings.Add("token", token); else IsolatedStorageSettings.ApplicationSettings["token"] = token;

IsolatedStorageSettings.ApplicationSettings.Save(); }

private string GetToken() { if (!IsolatedStorageSettings.ApplicationSettings.Contains("token")) return null; else return IsolatedStorageSettings.ApplicationSettings["token"] as string; }

private void postFacebook_Click(object sender, RoutedEventArgs e) { //Cheking if there's internet if (!NetworkInterface.GetIsNetworkAvailable()) { Dispatcher.BeginInvoke(() => { MessageBox.Show("No Internet Connec-tion, Try again later"); }); return; } if (client.AccessToken == null) { var parameters = new Dictionary<string, object>(); parameters["client_id"] = FBApiID; parameters["redirect_uri"] = "https://www.facebook.com/connect/login_success.html"; parameters["response_type"] = "token"; parameters["display"] = "touch"; parameters["scope"] = ExtendedPermisions; Browser.Visibility = System.Windows.Visibility.Visible; string t = client.GetLoginUrl(parameters).AbsoluteUri; Browser.Navigate(client.GetLoginUrl(parameters));

6

Page 7: paiip lab 3

} else PostToWall(); }

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e) { if (Browser.Visibility == System.Windows.Visibility.Visible) { Browser.Visibility = System.Windows.Visibility.Collapsed; e.Cancel = true; } base.OnBackKeyPress(e); }

private void reminder_Click(object sender, RoutedEventArgs e) { this.NavigationService.Navigate(new Uri("/ShowReminder.xaml", UriKind.Relative)); }

private void button3_Click(object sender, RoutedEventArgs e) { this.NavigationService.Navigate(new Uri("/RssReader.xaml", UriKind.Rela-tive)); } }}

Anexa C: RssGetTextTrim.cs

using System;using System.Net;using System.Windows.Data;using System.Globalization;using System.Text.RegularExpressions;

namespace RFRWindowsPhone{ public class RssGetTextTrim : IValueConverter { public object Convert(object value, Type targetType, object parameter, Cul-tureInfo culture) { if (value == null) return null;

int maxLength = 200; int strLength = 0; string fixedString = "";

// Remove HTML tags. fixedString = Regex.Replace(value.ToString(), "<[^>]+>", string.Empty);

// Remove newline characters fixedString = fixedString.Replace("\r", "").Replace("\n", "");

// Remove encoded HTML characters fixedString = HttpUtility.HtmlDecode(fixedString);

7

Page 8: paiip lab 3

strLength = fixedString.ToString().Length;

if (strLength == 0) {return null; } else if (strLength >= maxLength) { fixedString = fixedString.Substring(0, maxLength); fixedString = fixedString.Substring(0, fixedString.LastIndexOf(" ")); } fixedString += "...";

return fixedString; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }}

Anexa D: ShowReminder.cs

namespace RFRWindowsPhone{ public partial class ShowReminder : PhoneApplicationPage { public ShowReminder() { InitializeComponent(); } private void btnAdd_Click(object sender, RoutedEventArgs e) { string reminderName = Guid.NewGuid().ToString(); Reminder reminder = new Reminder(reminderName); reminder.Title = this.txtTitle.Text; reminder.Content = this.txtContent.Text; double seconds = 10.0; double.TryParse(this.txtSeconds.Text, out seconds); reminder.BeginTime = DateTime.Now.AddSeconds(seconds); reminder.ExpirationTime = reminder.BeginTime.AddSeconds(5.0); reminder.RecurrenceType = RecurrenceInterval.Daily; ScheduledActionService.Add(reminder); this.NavigationService.GoBack(); } private void btnCancel_Click(object sender, RoutedEventArgs e) { this.NavigationService.GoBack(); } }}

Concluzii:

8

Page 9: paiip lab 3

Efectuând această lucrare de laborator m-am familizarizat cu pachetul SDK de la Windows

Phone ce mi-a oferit posibiliatea de a crea aplicații rapid și ușor. Am creat o simplă aplicație care

efectuează următoarele operații ca Postarea unui mesaj pe Facebook, citirea unui Rss și afișarea lui

într-o listă, crearea unui reminder care lucrează în background.

9