ToastPrompt在Windows Phone開(kāi)發(fā)中的用法
譯文在這篇文章中,我們將會(huì)探討如何在Windows Phone開(kāi)發(fā)中使用ToastPrompt控件。ToastPrompt控件是開(kāi)源類(lèi)庫(kù)Coding4Fun中的一種。同時(shí),在文章中也會(huì)介紹一些ToastPrompt的主要特點(diǎn),提供一些公共的API等。
步驟1:首先,我們要?jiǎng)?chuàng)建一個(gè)簡(jiǎn)單的Windows Phone應(yīng)用程序,讓我們來(lái)看看如何創(chuàng)建:
◆打開(kāi)Visual Studio 2010
◆文件 - >新建 - >項(xiàng)目
◆選擇Silverlight for Windows Phone模板
◆選擇Windows Phone application
◆給這個(gè)項(xiàng)目起一個(gè)你想要的名字
步驟2:這一步,我們將會(huì)添加Coding4Fun里的引用(reference),具體添加步驟請(qǐng)看下圖。
步驟3:在此步驟中,我們將會(huì)在MainPage.xaml.cs文件中添加一個(gè)命名空間(namespace)。
步驟4:這里,你會(huì)看到MainPage.xaml.cs文件的代碼。
- 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 Coding4Fun.Phone.Controls;
- using System.Windows.Media.Imaging;
- namespace Myapp
- {
- public partial class MainPage : PhoneApplicationPage
- {
- // Constructor
- public MainPage()
- {
- InitializeComponent();
- }
- void MyTP_Completed(object sender, PopUpEventArgs<string, PopUpResult> e)
- {
- //add some code here
- }
- private void Mybutton1_Click(object sender, RoutedEventArgs e)
- {
- ToastPrompt MyTP = new ToastPrompt();
- MyTP.Title = "MySimpleToastPromt";
- MyTP.Message = "It's Simply a Toast Prompt";
- MyTP.FontSize = 40;
- MyTP.TextOrientation = System.Windows.Controls.Orientation.Vertical;
- MyTP.ImageSource = new BitmapImage(new Uri("ApplicationIcon.png", UriKind.RelativeOrAbsolute));
- MyTP.Completed += MyTP_Completed;
- MyTP.Show();
- }
- private void Mybutton2_Click(object sender, RoutedEventArgs e)
- {
- var MyTP = new ToastPrompt
- {
- Title = "MyPrompt",
- Message = "Hiiiii THis is a ToastPrompt",
- ImageSource = new BitmapImage(new Uri("..\\ApplicationIcon.png", UriKind.RelativeOrAbsolute)),
- Background=new SolidColorBrush(Colors.LightGray)
- };
- MyTP.Show();
- }
- }
- }
步驟5:這里,你將會(huì)看到MainPage.xaml文件的代碼。
- <phone:PhoneApplicationPage
- x:Class="Myapp.MainPage"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
- xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
- FontFamily="{StaticResource PhoneFontFamilyNormal}"
- FontSize="{StaticResource PhoneFontSizeNormal}"
- Foreground="{StaticResource PhoneForegroundBrush}"
- SupportedOrientations="Portrait" Orientation="Portrait"
- shell:SystemTray.IsVisible="True">
- <!--LayoutRoot is the root grid where all page content is placed-->
- <Grid x:Name="LayoutRoot" Background="Transparent">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="*"/>
- </Grid.RowDefinitions>
- <!--TitlePanel contains the name of the application and page title-->
- <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
- <TextBlock x:Name="PageTitle" Text="My Toast App" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" FontFamily="Comic Sans MS" FontSize="48">
- <TextBlock.Foreground>
- <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
- <GradientStop Color="Black" Offset="0" />
- <GradientStop Color="#FFF8C1BE" Offset="1" />
- </LinearGradientBrush>
- </TextBlock.Foreground>
- </TextBlock>
- </StackPanel>
- <!--ContentPanel - place additional content here-->
- <StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
- <Button Content="Click to see Toast Prompt" Click="Mybutton1_Click" FontFamily="Comic Sans MS" FontSize="28" />
- <Button Content="Simple usage" Click="Mybutton2_Click" FontFamily="Comic Sans MS" FontSize="26">
- <Button.Background>
- <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
- <GradientStop Color="Black" Offset="0" />
- <GradientStop Color="#FFE4D4E5" Offset="1" />
- </LinearGradientBrush>
- </Button.Background>
- </Button>
- </StackPanel>
- </Grid>
- </phone:PhoneApplicationPage>
步驟6:在這一步,你將會(huì)看到MainPage.xaml文件的設(shè)計(jì)頁(yè)面。
步驟7:現(xiàn)在,我們將要運(yùn)行這個(gè)程序(F5),輸出畫(huà)面如下。
輸出1:這是默認(rèn)的輸出畫(huà)面:
輸出2:在這個(gè)畫(huà)面中,你會(huì)看到點(diǎn)擊***個(gè)按鈕之后,ToastPrompt的顯示效果。
輸出3:在這個(gè)畫(huà)面中,你會(huì)看到點(diǎn)擊第二個(gè)按鈕之后,ToastPrompt的顯示效果。
原文鏈接:http://www.c-sharpcorner.com/UploadFile/74f20d/working-with-toastprompt-in-windows-phone-7/
【編輯推薦】