Winphone7之如何防止锁屏

  winphone7 锁屏再恢复后,静态变量会变成null,等等,给使用带来极大的不便。为此,禁止锁屏对于这类应用是必需的,解决方法如何:

<phone:PhoneApplicationPage
    x:Class="WPAntiScreenSaver.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="ApplicationTitle" Text="WP Anti ScreenSaver - by Little Elf" Style="{StaticResource PhoneTextNormalStyle}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Button Content="Start" FontSize="48" Height="150" HorizontalAlignment="Center" Margin="0,400,0,0" Name="btnStart" VerticalAlignment="Top" Width="380" Click="btnStart_Click"/>
            <TextBlock Height="100" FontSize="56" HorizontalAlignment="Center" Margin="0,215,0,0" Name="tbTimeElepsed" Text="00:00:00" VerticalAlignment="Top" />
        </Grid>
    </Grid>

</phone:PhoneApplicationPage>

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 System.Diagnostics;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Xna.Framework.GamerServices;
using System.Windows.Threading;

namespace WPAntiScreenSaver
{
    public partial class MainPage : PhoneApplicationPage
    {
        private bool _isRunning;
        private DateTime _startTime = DateTime.Now;
        private DispatcherTimer _tmr = new DispatcherTimer();

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            this._tmr.Interval = TimeSpan.FromMilliseconds(100.0);
            this._tmr.Tick += new EventHandler(this._tmr_Tick);
        }

        private void _tmr_Tick(object sender, EventArgs e)
        {
            TimeSpan span = (TimeSpan)(DateTime.Now - this._startTime);
            this.tbTimeElepsed.Text = string.Format("{0}:{1}:{2}", span.Hours.ToString("00"), span.Minutes.ToString("00"), span.Seconds.ToString("00"));
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            this._isRunning = !this._isRunning;
            this.UpdateButtonContent();
            Guide.IsScreenSaverEnabled = !this._isRunning;
            if (PhoneApplicationService.Current.ApplicationIdleDetectionMode ==  IdleDetectionMode.Disabled)
            {
                PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Enabled;
            }
        }

        private void PhoneApplicationPage_Unloaded(object sender, RoutedEventArgs e)
        {
            Guide.IsScreenSaverEnabled = true;
        }

        private void UpdateButtonContent()
        {
            this.btnStart.Content = this._isRunning ? "Stop" : "Start";
            if (this._isRunning)
            {
                this._startTime = DateTime.Now;
                this._tmr.Start();
            }
            else
            {
                this._tmr.Stop();
                this.tbTimeElepsed.Text = "00:00:00";
            }
        }


    }
}

posted on 2011-05-11 17:35  chuncn  阅读(1195)  评论(0编辑  收藏  举报

导航