Thứ Sáu, 23 tháng 9, 2016

Managing Windows & Web Authentication (MW&WA)

  Không có nhận xét nào

Hướng dẫn code


Tạo project mới có tên ContactLive:


Mở MainPage.xaml ra code giao diện :)

Copy/paste
<Page
x:Class="ConnectLive.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ConnectLive"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Page.Resources>
<DataTemplate x:Key="contactTemp">
<Grid Width="600" Height="300">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300"/>
<ColumnDefinition Width="300"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="75"></RowDefinition>
<RowDefinition Height="75"></RowDefinition>
<RowDefinition Height="75"></RowDefinition>
<RowDefinition Height="75"></RowDefinition>

</Grid.RowDefinitions>
<TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding ContactName}"/>
<TextBlock Grid.Column="0" Grid.Row="1" Text="Contact Number" Visibility="{Binding CanShow}"/>
<TextBlock Grid.Column="0" Grid.Row="2" Text="Email" Visibility="{Binding CanShow}"/>
<TextBlock Grid.Column="0" Grid.Row="3" Text="Image"/>
<ListBox Grid.Column="1" Grid.Row="1" ItemsSource="{Binding PhoneNumber}" Visibility="{Binding CanShow}"></ListBox>
<ListBox Grid.Column="1" Grid.Row="2" ItemsSource="{Binding ContactEmails}" Visibility="{Binding CanShow}"></ListBox>
<Image Grid.Column="1" Grid.Row="3" Source="{Binding ContactImage}"/>
</Grid>
</DataTemplate>
</Page.Resources>
<Grid Background="ForestGreen">
<Button x:Name="btnContact" Content="View Contact" Margin="50,75,0,650" Click="btnContact_Click"/>
<ListView x:Name="lstContact" Margin="600,80,90,90" ItemTemplate="{StaticResource contactTemp}"/>
</Grid>
</Page>

Đảm bảo rằng khi code xong design sẽ trông như thế này:



Mở MainPage.xaml.cs ra code phần xử lý:

Copy/paste
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.Contacts;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace ConnectLive
{
///

/// An empty page that can be used on its own or navigated to within a Frame.
///

public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}

List s;
public List Details { get { return s; } set { s = value; } }

private async void btnContact_Click(object sender, RoutedEventArgs e)
{
Details = new List();
var p = new ContactPicker();
p.CommitButtonText = "Pick Contact";
var selectedContact = await p.PickSingleContactAsync();
Details.Add(new ContactDetails(selectedContact));
lstContact.ItemsSource = Details;
}


public class ContactDetails
{
public string ContactName { get; private set; }
public BitmapImage ContactImage { get; private set; }
public Visibility CanShow { get; set; }
public List PhoneNumbers { get; set; }
public List ContactEmails { get; set; }

public ContactDetails(ContactInformation c)
{
PhoneNumbers = new List();
ContactEmails = new List();
CanShow = Visibility.Visible;
ContactName = c.Name;
foreach (var item in c.PhoneNumbers)
PhoneNumbers.Add(item.Value);
foreach (var item in c.Emails)
ContactEmails.Add(item.Value);
}
}
}
}

Run thôi:






Nhận xét

- Qua ví dụ trên, chúng ta đã được biết cách quản lí ứng dụng People có sẵn của Windows và cách lấy thông tin từ nó. Trong thực tế ta cần lấy những thông tin ứng dụng hệ thống rất nhiều thứ...v.v

Nội dung tham khảo

- Windows.ApplicationModel.Contacts namespace
- Managing application data in a Windows Store business app using C#, XAML, and Prism

Không có nhận xét nào :

Đăng nhận xét