Windows Phone 8.1 开发会用到的方法

转自http://www.bcmeng.com/jiqiao/

 

1. 屏幕方向控制

DisplayOrientations onEntryOrientations;
 protected override void OnNavigatedTo(NavigationEventArgs e)
 {
     onEntryOrientations = DisplayInformation.AutoRotationPreferences;
     Windows.Graphics.Display.DisplayInformation.AutoRotationPreferences = Windows.Graphics.Display.DisplayOrientations.Portrait;
 }

 protected override void OnNavigatedFrom(NavigationEventArgs e)
 {
     Windows.Graphics.Display.DisplayInformation.AutoRotationPreferences = onEntryOrientations;
 }

2. 重写Back Key

using Windows.Phone.UI.Input
...
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    //This should be written here rather than the contructor
    HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}

void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
    //This is where all your 'override backkey' code goes
    //You can put message dialog and/or cancel the back key using e.Handled = true;

    /*Frame frame = Window.Current.Content as Frame;
    if (frame == null)
        return;
    if (frame.CanGoBack)
    {
        frame.GoBack();
        e.Handled = true;
    }*/

}

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
    //remove the handler before you leave!
    HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
}

3. 退出应用程序

Application.Current. Exit();

4. 显示/隐藏状态栏Status Bar

Windows.UI.ViewManagement.StatusBar.GetForCurrentView().ShowAsync();

Windows.UI.ViewManagement.StatusBar.GetForCurrentView().HideAsync();

5. 隐藏命令栏CommandBar(没有成功,CommandBar没有Visibility属性,待继续试)

CommandBar.Visibility = Windows.UI.Xaml.Visibility.Collapsed;

6. 状态栏显示进度条

Windows.UI.ViewManagement.StatusBar.GetForCurrentView().ShowAsync();
 
var progInd = Windows.UI.ViewManagement.StatusBar.GetForCurrentView().ProgressIndicator;
 
progInd.Text = "Fetching...";
 
progInd.ShowAsync();

7. 跳转各个开关设置页面

// 跳转到“蓝牙设置”页 
bool success = await Launcher.LaunchUriAsync(new Uri("ms-settings-bluetooth:")); 
// 跳转到“手机网络设置”页
bool success = await Launcher.LaunchUriAsync(new Uri("ms-settings-cellular:"));  
// 跳转到“WiFi设置”页 
bool success = await Launcher.LaunchUriAsync(new Uri("ms-settings-wifi:"));  
// 跳转到“定位设置”页 
bool success = await Launcher.LaunchUriAsync(new Uri("ms-settings-location:")); 
// 跳转到“电子邮件+账户设置”页 
bool success = await Launcher.LaunchUriAsync(new Uri("ms-settings-emailandaccounts:")); 
// 跳转到“锁屏设置”页 
bool success = await Launcher.LaunchUriAsync(new Uri("ms-settings-lock:")); 
//屏幕旋转 
var success = await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings-screenrotation:")); 
// 拨打指定的电话号码 
bool success = await Launcher.LaunchUriAsync(new Uri("tel:1391234567")); 
bool success = await Launcher.LaunchUriAsync(new Uri("callto:1391234567")); 
// 用浏览器打开一个指定的 http 链接 
bool success = await Launcher.LaunchUriAsync(new Uri("http://webabcd.cnblogs.com/"));
// 向指定的地址发送邮件 
bool success = await Launcher.LaunchUriAsync(new Uri("mailto:aaa@xxx.com"));

 8. App Bar浮出

<Page.BottomAppBar>
        <CommandBar>
            <AppBarButton Icon="List" Label="ingredients">
                <AppBarButton.Flyout>
                    <Flyout>
                        <Grid Height="400">
                            <ListBox x:Name="IngredientsListBox" />
                        </Grid>
                    </Flyout>
                </AppBarButton.Flyout>
            </AppBarButton>
        </CommandBar>
    </Page.BottomAppBar>
 List<string> ingredients = new List<string> {
                "Shortcrust Pastry: ","220g plain flour","120g cold butter",
                "1 tablespoon very cold water","1 large egg",
                "separated into yolk and white","Filling: ","1 teaspoon oil",
                "1 small onion","220g bacon","finely chopped 5 large eggs",
                "125ml cream","1/4 teaspoon ground nutmeg Salt and pepper",
                "110g grated tasty cheddar cheese"
            };
 
            IngredientsListBox.ItemsSource = ingredients;

9. 页面间导航动画

<Page >
 
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.ChildrenTransitions>
            <TransitionCollection>
                <EntranceThemeTransition/>
            </TransitionCollection>
        </Grid.ChildrenTransitions>
 
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions></Grid>
</Page>

10. 语音识别API

if (jaRecognizers.Count() > 0)
{
//指定识别器
 speechRecognizerUI.Recognizer.SetRecognizer(jaRecognizers.First());
//听写整句,基于网络识别
 speechRecognizerUI.Recognizer.Grammars.AddGrammarFromPredefinedType("webSearch", SpeechPredefinedGrammar.WebSearch);
//预加载全部语法
 await speechRecognizerUI.Recognizer.PreloadGrammarsAsync();
 
  // 带 UI 的语音识别器的监听页上显示的标题
 speechRecognizerUI.Settings.ListenText = "正在聆听…";
 // 带 UI 的语音识别器的监听页上显示的示例文本
 speechRecognizerUI.Settings.ExampleText = "语音转文字";
// 是否用语音朗读识别结果
 speechRecognizerUI.Settings.ReadoutEnabled = true;
speechRecognizerUI.Settings.ShowConfirmation = true;
 try
 {
// 开始识别
SpeechRecognitionUIResult result = await speechRecognizerUI.RecognizeWithUIAsync();
 ContentBox.Text += result.RecognitionResult.Text;
}
catch
{
//错误处理
}
 
}

11. 获得证书凭证

void SaveCredential(string username, string password) {
     PasswordVault vault = new PasswordVault();
     PasswordCredential cred = new PasswordCredential(“MyAppResource”, username, password);
     vault.Add(cred);
}
 
IReadOnlyList<PasswordCredential> RetrieveCredential(string resource) {
     PasswordVault vault = new PasswordVault();
     return vault.FindAllByResource(resource);
}

 12. 检查网络状态

bool CheckNetwork()
        {
            bool isOnline = false;
            ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
            if (InternetConnectionProfile==null)
 
            {
 
                txtMag.Text = "亲:断网情况下无法显示词典内容!";
 
            }
           else
            {
                isOnline = true;
            }
            return isOnline;
        }

13. ListView和GridView多选和拖动

MyListView.ReorderMode = ListViewReorderMode.Enabled;
 
MyListView.SelectionMode = ListViewSelectionMode.Multiple;

 

posted @ 2015-06-10 16:01  Kunkka_An  阅读(298)  评论(0编辑  收藏  举报