1、設置應用程序的圖標
右擊Wp7應用程序的解決方案,選擇屬性,設置Icon爲自己想要的圖片,如下圖
並且要確保圖片的build方式爲Content,如下圖所示:
2、 Windows Phone 7中的啓動畫面
默認情況下,Windows Phone 7 應用程序會使用如下的默認圖片作爲程序的啓動畫面,我們可以自己設置Windows Phone 7中的啓動畫面,可以設置爲:1.使用自己的圖片作爲啓動畫面 2. 帶動畫效果的啓動畫面 3.不使用啓動畫面
對於1,我們只要將SplashScreenImage替換爲自己的圖片即可。而對於3,如果不想使用啓動畫面,我們只需將SplashScreenImage圖片移出項目即可。下面着重講一下如果實現動畫啓動畫面.
這裏使用BackgroundWorker類來實現,BackgroundWorker類是開闢一個後臺線程來處理一些操作而同時你的UI也能繼續響應用戶操作的類。更多的關於BackgroundWorker類大家可以Google其用法。在WP7的渲染線程中,如果你想要一個持續響應的用戶界面,那麼BackgroundWorker類會變得很有用。你可以監聽你想要做的操作的進程的事件以及操作完成的信號。我們使用RunWorkerAsync開啓後臺操作。
注意:我們不應該在BackgroundWorker類的DoWork去操作用戶界面。我們可以在ProgressChanged 和RunWorkerCompleted事件中去操作與用戶界面相關的操作。關於更多的可以參考MSDN http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker%28VS.95%29.aspx
下面開始創建動畫的啓動動畫的步驟
1. 創建WP7項目,並添加名爲AnimatedSplashScreen.xaml的用戶控件(UserControl)
2. 在MainPage.xaml.cs添加如下的命名空間,並且編輯後置代碼如下
using System.Threading;
using System.Windows.Controls.Primitives;
public partial class MainPage : PhoneApplicationPage
{
BackgroundWorker backroungWorker;
Popup myPopup;
// Constructor
public MainPage()
{
InitializeComponent();
myPopup = new Popup() { IsOpen = true, Child =
new AnimatedSplashScreen() };
backroungWorker = new BackgroundWorker();
RunBackgroundWorker();
}
private void RunBackgroundWorker()
{
backroungWorker.DoWork += ((s, args) =>
{
Thread.Sleep(5000);
});
backroungWorker.RunWorkerCompleted += ((s, args) =>
{
this.Dispatcher.BeginInvoke(() =>
{
this.myPopup.IsOpen = false;
}
);
});
backroungWorker.RunWorkerAsync();
}
}
3. 編輯AnimatedSplashScreen.xaml前臺代碼如下
<StackPanel x:Name="LayoutRoot" Background="Black" Height="800" Width="480">
<TextBlock Text="WindowsPhoneGeek Sample Splash Screen" x:Name="text" Foreground="Green" FontSize="65" TextWrapping="Wrap" Margin="0,20,0,0"/>
<Image Source="logo.png" x:Name="logoImage" Stretch="None" Margin="0,0,0,50">
<Image.Projection>
<PlaneProjection/>
</Image.Projection>
</Image>
<toolkit:PerformanceProgressBar IsIndeterminate="True" Foreground="Green"/>
</StackPanel>
並且添加如下的動畫資源
<UserControl.Resources>
<Storyboard x:Key="flippingAnimation" >
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationX)" Storyboard.TargetName="logoImage">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:2" Value="360"/>
</DoubleAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="text">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<SolidColorBrush Color="White"/>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame KeyTime="0:0:2">
<DiscreteObjectKeyFrame.Value>
<SolidColorBrush Color="Green"/>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
4. 最後編輯其後置代碼如下
public AnimatedSplashScreen()
{
InitializeComponent();
Storyboard flippingAnimation = this.Resources["flippingAnimation"]as Storyboard;
flippingAnimation.Begin();
}
3、Popup使用的方法:
private Popup popup;
popup = new Popup();
popup.Child = new 控件類();
//打開
popup.IsOpen = true;
//關閉
popup.IsOpen = false
或者
xaml代碼
<Popup x:Name="popup">
<Border>
<StackPanel>
……
</StackPanel>
</Border>
</Popup>
cs代碼
//打開
popup.IsOpen = true;
//關閉
popup.IsOpen = false
4、在TextBlock控件中使用<LineBreak></LineBreak>進行換行。
<TextBlock TextWrapping="Wrap">
測試
<LineBreak></LineBreak>
<LineBreak></LineBreak>
測試
<LineBreak></LineBreak>
<LineBreak></LineBreak>
測試
</TextBlock>
5、捕獲物理按鍵返回鍵,打開頁面,離開頁面。windows phone有3個物理按鍵,返回鍵,開始鍵,搜索鍵,後面兩個無法在程序中捕獲到。
//點擊返回按鈕
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
//你的代碼
e.Cancel = false;
base.OnBackKeyPress(e);
}
//從其他頁面進入該頁面
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgse)
{
//你的代碼
base.OnNavigatedTo(e);
}
//離開當前的頁面
protected override voidOnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
//你的代碼
base.OnNavigatedFrom(e);
}
6、獲取父控件裏面的子控件的方法。之前在判斷ListBox控件什麼時候滾到底的時候使用過該方法,這個方法很常用。
//獲取第一個子類型
public static TFindChildOfType<T>(DependencyObject root)
whereT : class
{
varqueue = new Queue<DependencyObject>();
queue.Enqueue(root);
while(queue.Count >
0)
{
DependencyObject current= queue.Dequeue();
for(int i =VisualTreeHelper.GetChildrenCount(current) -
1;0 <= i; i--)
{
var child = VisualTreeHelper.GetChild(current, i);
var typedChild = child
asT;
if (typedChild !=
null)
{
return typedChild;
}
queue.Enqueue(child);
}
}
returnnull;
}
//獲取所有的子類型
public staticList<T> FindAllChildOfType<T>(DependencyObject root)
where T : class
{
varqueue = new Queue<DependencyObject>();
queue.Enqueue(root);
List<T> allChild = new List<T>();
while(queue.Count >
0)
{
DependencyObject current= queue.Dequeue();
for (int i = VisualTreeHelper.GetChildrenCount(current) -
1; 0 <= i;i--)
{
var child = VisualTreeHelper.GetChild(current, i);
var typedChild = child
asT;
if (typedChild !=
null)
{
allChild.Add(typedChild);
}
queue.Enqueue(child);
}
}
returnallChild;
}
7、使用<ControlTemplate>……</ControlTemplate>來擴展控件的各種自定義化的效果,當你需要在控件上實現一些動畫的效果,或者在控件上再嵌入其他的一些控件都可以通過設計一個ControlTemplate來實現。
如實現一個按鈕的單擊效果:
<Button
Content="Button" Height="72" HorizontalAlignment="Left"
Margin="103,197,0,0" Name="button1" VerticalAlignment="Top" Width="160">
<Button.Template>
<ControlTemplate>
<Grid
Background="Transparent">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup
x:Name="CommonStates">
<VisualState
x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFramesStoryboard.TargetName="ButtonBackground"Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrameKeyTime="0" Value="YellowGreen"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames
Storyboard.TargetName="ButtonBackground" Storyboard.TargetProperty="BorderBrush">
<DiscreteObjectKeyFrameKeyTime="0" Value="YellowGreen"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border
x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBindingBorderThickness}"
Background="{TemplateBindingBackground}" Margin="{StaticResource
PhoneTouchTargetOverhang}">
<ContentControl
x:Name="ContentContainer" Foreground="{TemplateBinding Foreground}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"VerticalContentAlignment="{TemplateBinding
VerticalContentAlignment}" Padding="{TemplateBindingPadding}"Content="{TemplateBindingContent}"ContentTemplate="{TemplateBinding
ContentTemplate}"/>
</Border>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
8、顯示和隱藏手機的頂部托盤,就是頂部那個信號和電池信息那塊東西。
//顯示
SystemTray.IsVisible = true;
//隱藏
SystemTray.IsVisible = false;