合約:合約就像一個或多個程序之間的協議。合約定義了應用程序在與其它程序或Windows 進行交互時必須遵循的一些約定。我們對一個應用的一些設置,常用的可以放到AppBar上面,如果不是很常用的,比如應用程序的主題顏色,可以放到設置合約,也就是通過超級菜單調出來的設置面板上。下面就介紹如何把我們的設置選項添加的設置面板中。
1.引入命名空間:Windows.UI.ApplicationSettings;
2.注冊設置事件
在程序啟動的時候,App.xaml.cs文件中有一個名為OnLaunched的方法會被調用。在OnLaunched的開頭,我創建了一個event handler,當用戶打開SettingsPane時,會被調用。你自定義的設置畫面直到用戶打開Settings Charm時,才會被加載。當你“暫停”你的程序或者游戲,如果設置面板被打開了,那么這個event也會被調用。
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
SettingsPane.GetForCurrentView().CommandsRequested += App_CommandsRequested; //注冊設置事件
Frame rootFrame = Window.Current.Content as Frame;/p>
p> // 不要在窗口已包含內容時重復應用程序初始化,
// 只需確保窗口處于活動狀態
if (rootFrame == null)
{
// 創建要充當導航上下文的框架,并導航到第一頁
rootFrame = new Frame();/p>
p> if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: 從之前掛起的應用程序加載狀態
}/p>
p> // 將框架放在當前窗口中
Window.Current.Content = rootFrame;
}/p>
p> if (rootFrame.Content == null)
{
// 當未還原導航堆棧時,導航到第一頁,
// 并通過將所需信息作為導航參數傳入來配置
// 參數
if (!rootFrame.Navigate(typeof(MainPage), args.Arguments))
{
throw new Exception("Failed to create initial page");
}
}
// 確保當前窗口處于活動狀態
Window.Current.Activate();
}
void App_CommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
{
SettingsCommand cmd = new SettingsCommand("關于", "關于應用", (handle) =>
{
Popup popup = CreatePopup.Create(new AboutPage(),346);
popup.IsOpen = true;
});
SettingsCommand cmd1 = new SettingsCommand("主題", "主題顏色", (handle) =>
{
Popup popup = CreatePopup.Create(new ThemColor(), 346);
popup.IsOpen = true;
});
args.Request.ApplicationCommands.Add(cmd);
args.Request.ApplicationCommands.Add(cmd1);
}
如上代碼所示,我創建了一個SettingsCommand對象,并提供了三個值。除非你需要在程序運行時修改SettingsPane,否則第一個參數值并不太重要。它就是一個簡單ID,隨后可以通過這個ID可以引用到SettingsCommand。標簽“主題顏色”可以是任意的字符串,不過我建議不要超過40個字符,否則會被截斷。最后一個參數值是這個command的handler。當用戶輕觸lable 時,會執行該handler。在這里,我用lambada 表達式來簡化該處理。表達式里面,我創建了一個Popup控件,并將其IsOpen 屬性設置為true。改變這個屬性,可以使Popup顯示在屏幕上。
3. 創建Popup控件
首先是一個自定義的UserControl,之后我會介紹到該UserControl,現在只需要明白AboutPage.xaml是一個UserControl即可,AboutPage.xaml是用于Popup控件中的。
public class CreatePopup
{
public static Popup Create(UserControl element, double width)
{
Popup p = new Popup();
p.Child = element;
p.IsLightDismissEnabled = true;
p.ChildTransitions = new TransitionCollection();
p.ChildTransitions.Add(new PaneThemeTransition() //聲明邊緣 UI(如應用程序欄)的邊緣轉換位置。
{
Edge = (SettingsPane.Edge == SettingsEdgeLocation.Right) ?
EdgeTransitionLocation.Right :
EdgeTransitionLocation.Left
});//檢查SettingsPane的edge,有些國家的超級菜單在左邊。/p>
p> element.Width = width;
element.Height = Window.Current.Bounds.Height;
p.SetValue(Canvas.LeftProperty, SettingsPane.Edge == SettingsEdgeLocation.Right ? (Window.Current.Bounds.Width - width) : 0);//設置距離左邊的邊距
p.SetValue(Canvas.TopProperty, 0);
return p;
}
}
上面定義了UserControl的高度和寬度,并將element賦值給Popup控件p。(這里建議的寬度是346或者646,高度應該是用戶屏幕的完整高度,參考:設計指南)。最后,設置了popup的left 和top 屬性,這樣popup將會出現在適當的位置,然后將p返回給SettingsCommand。
注解:Windows 8可以根據機器的本地化設置而有不同的變化。如果有的國家的語言是從右往左讀的,那么Charms Bar的實際位置是在屏幕的左邊,而不是右
邊。這就是為什么我在給popup的LeftProperty賦值時,檢查SettingsPane的“edge”。
4. 創建UserControl
添加一個新的UserControl項到工程中。為了跟之前的代碼想匹配,我將其命名為AboutPage.xaml。這個文件中代碼的內容完全取決于你。沒有任何的官方文檔約束。在UserControl中,可以通過在MainPage中定義一個共有字段public static MainPage Current來訪問MainPage頁面中控件,實現對頁面里面控件的屬性進行修改。
如需源代碼,點擊SettingPanel_jb51net.zip下載