如何防止屏幕超时(根据应用程序设置中的复选框选项)? [英] How to prevent screen from timing out (based on checkbox option in app's settings)?

查看:43
本文介绍了如何防止屏幕超时(根据应用程序设置中的复选框选项)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的应用实施一项设置,允许用户阻止屏幕超时,但它无效。 


Settings.xaml

<! -  LayoutRoot是放置所有页面内容的根网格 - > 
< Grid x:Name =" LayoutRoot"背景= QUOT;透明">
< Grid.RowDefinitions>
< RowDefinition Height =" Auto" />
< RowDefinition Height =" *" />
< /Grid.RowDefinitions>

<! - TitlePanel包含应用程序的名称和页面标题 - >
< StackPanel Grid.Row =" 0"余量= QUOT; 12,17,0,28">
< TextBlock Text =" APP NAME" Style =" {StaticResource PhoneTextNormalStyle}" />
< TextBlock Text =" settings"余量= QUOT; 9,-7,0,0" Style =" {StaticResource PhoneTextTitle1Style}" />
< / StackPanel>

<! - ContentPanel - 在此处添加其他内容 - >
< Grid x:Name =" ContentPanel" Grid.Row = QUOT 1 QUOT;余量= QUOT; 12,0,12,0">
< StackPanel>
< StackPanel>
< CheckBox x:Name =" settings_Checkbx"内容="保持设备清醒"选中=" Checkbx_Checked"未选中=" Checkbx_Unchecked"器isChecked = QUOT假QUOT; />
< / StackPanel>
< / StackPanel>
< / Grid>
< / Grid>

Settings.xaml.cs

 namespace AppName 
{
public partial class Settings:PhoneApplicationPage
{
DisplayRequest displayRequest;

public Settings()
{
InitializeComponent();

displayRequest = new Windows.System.Display.DisplayRequest();
displayRequest.RequestActive();
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);

var setting2 = IsolatedStorageSettings.ApplicationSettings;
尝试
{
bool status =(bool)setting2 [" checkbxScreenawake"];
if(status)
{
Checkbx_deviceawake.IsChecked = true;
}
其他
{
Checkbx_deviceawake.IsChecked = false;
}
}
抓住
{
//显示消息或做其他事情
}
}

private void Checkbx_Checked(object sender,RoutedEventArgs e)
{
Checkbx_deviceawake.IsChecked = true;

var appSettings = IsolatedStorageSettings.ApplicationSettings;
if(appSettings.Contains(" checkbxScreenawake"))
{
appSettings [" checkbxScreenawake"] = true;
}
其他
{
appSettings.Add(" checkbxScreenawake",true);
}

appSettings.Save();
}

private void Checkbx_Unchecked(对象发送者,RoutedEventArgs e)
{
Checkbx_deviceawake.IsChecked = true;

var appSettings = IsolatedStorageSettings.ApplicationSettings;
if(appSettings.Contains(" checkbxScreenawake"))
{
appSettings [" checkbxScreenawake"] = false;
}
其他
{
appSettings.Add(" checkbxScreenawake",false);
}

appSettings.Save();
}
}
}







Page1.xaml.cs(用于测试设置的应用程序页面)

 namespace AppName 
{
public partial class About:PhoneApplicationPage
{
DisplayRequest displayRequest;

public Page1()
{
InitializeComponent();
}


protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
var setting = IsolatedStorageSettings.ApplicationSettings;
尝试
{
bool status =(bool)setting [" checkbxScreenawake"];
if(status)
{
displayRequest = new Windows.System.Display.DisplayRequest();
displayRequest.RequestActive();
}
其他
{

}
}
捕获
{
//显示消息或做其他物品
}
}
}
}



解决方案

Hi m.findlay93


来自

DisplayRequest Class
,它显示在风中ows Phone 8:"此API仅在本机应用程序中受支持。"这意味着如果您需要使用此API,则需要在C ++编写的本机应用程序中使用它。


有一种方法可以解决问题。你可以参考

如何防止UWP应用中的屏幕锁定
。在这个博客中,有一个注释:"如果您仍在使用Windows Phone Silverlight开发,处理此问题的正确方法是禁用

用户空闲检测
。"
所以你可以使用

< a href ="https://msdn.microsoft.com/en-us/library/microsoft.phone.shell.phoneapplicationservice.useridledetectionmode%28v=vs.105%29.aspx?f=255&MSPPError=-2147217396"> PhoneApplicationService。 UserIdleDetectionMode属性达到目标。


你可以添加以下代码行:



  Microsoft.Phone.Shell.PhoneApplicationService.Current.ApplicationIdleDetectionMode = Microsoft.Phone.Shell.IdleDetectionMode.Disabled ; 





有关详细信息,请转到此链接:

Windows Phone 8的空闲检测


此外,我建议您尝试开发UWP应用程序而不是Windows
Phone 8应用程序。
众所周知,通用Windows平台(UWP)是一个多元化的平台.UWP不仅适用于手机。它包含运行Windows 10
的所有设备,例如笔记本电脑,Surface,Xbox, Hololens等。这意味着您可以将应用程序运行到各种设备上。这是未来趋势。



最好的问候,


罗伊


I'm trying to implement a setting for my app that allows the user to prevent the screen from timing out, but it's not working. 

Settings.xaml

    <!--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 Grid.Row="0" Margin="12,17,0,28">
            <TextBlock Text="APP NAME" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock Text="settings" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <StackPanel>
                <StackPanel>
                    <CheckBox x:Name="settings_Checkbx" Content="Keep device awake" Checked="Checkbx_Checked" Unchecked="Checkbx_Unchecked" IsChecked="false"/>
                </StackPanel>
            </StackPanel>
        </Grid>
    </Grid>

Settings.xaml.cs

namespace AppName
{
    public partial class Settings : PhoneApplicationPage
    {
        DisplayRequest displayRequest;

        public Settings()
        {
            InitializeComponent();

            displayRequest = new Windows.System.Display.DisplayRequest();
            displayRequest.RequestActive(); 
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            var setting2 = IsolatedStorageSettings.ApplicationSettings;
            try
            {
                bool status = (bool)setting2["checkbxScreenawake"];
                if (status)
                {
                    Checkbx_deviceawake.IsChecked = true;
                }
                else
                {
                    Checkbx_deviceawake.IsChecked = false;
                }
            }
            catch
            {
                //show message or do other things
            }
        }

        private void Checkbx_Checked(object sender, RoutedEventArgs e)
        {
            Checkbx_deviceawake.IsChecked = true;

            var appSettings = IsolatedStorageSettings.ApplicationSettings;
            if (appSettings.Contains("checkbxScreenawake"))
            {
                appSettings["checkbxScreenawake"] = true;
            }
            else
            {
                appSettings.Add("checkbxScreenawake", true);
            }

            appSettings.Save();
        }

        private void Checkbx_Unchecked(object sender, RoutedEventArgs e)
        {
            Checkbx_deviceawake.IsChecked = true;

            var appSettings = IsolatedStorageSettings.ApplicationSettings;
            if (appSettings.Contains("checkbxScreenawake"))
            {
                appSettings["checkbxScreenawake"] = false;
            }
            else
            {
                appSettings.Add("checkbxScreenawake", false);
            }

            appSettings.Save();
        }
    }
}



Page1.xaml.cs (Application page used for testing the setting)

namespace AppName
{
    public partial class About : PhoneApplicationPage
    {
        DisplayRequest displayRequest;

        public Page1()
        {
            InitializeComponent();
        }


        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            var setting = IsolatedStorageSettings.ApplicationSettings;
            try
            {
                bool status = (bool)setting["checkbxScreenawake"];
                if (status)
                {
                    displayRequest = new Windows.System.Display.DisplayRequest();
                    displayRequest.RequestActive();
                }
                else
                {

                }
            }
            catch
            {
                //show message or do other things
            }
        }
    }
}

解决方案

Hi m.findlay93

From the DisplayRequest Class, it shows that in windows Phone 8:"This API is supported in native apps only." It means that if you need to use this API, you need to use it in a native app written by C++.

There is a way to solve the problem. You could refer How to prevent screen locks in your UWP apps. In this blog, there is a note: "If you’re still using Windows Phone Silverlight development, the correct way to handle this is to disable user idle detection." So you could use PhoneApplicationService.UserIdleDetectionMode Property to reach your goal.

You could add this line of code:

 Microsoft.Phone.Shell.PhoneApplicationService.Current.ApplicationIdleDetectionMode =Microsoft.Phone.Shell.IdleDetectionMode.Disabled;


For more information, you could go to this link: Idle detection for Windows Phone 8.

Besides, I suggest that you could try to develop a UWP app instead of a Windows Phone 8 app. As we know, the Universal Windows Platform (UWP) is a diversified platform. UWP is not only just for phones. It contains all the devices that run Windows 10 e.g. laptop, Surface, Xbox, Hololens, etc. That means you could run the app onto a wide range of devices. This is the future trend.

Best regards,

Roy


这篇关于如何防止屏幕超时(根据应用程序设置中的复选框选项)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆