Windows Phone 8 APP 中禁用截圖

---
日期: Oct 15, 2014
標籤:
- WP8
- WP8.1
- C#
語言: 正體中文
---

Windows Phone 8 有系統自帶的截圖功能,快捷鍵:電源鍵+Win鍵,可以隨意截圖。

Windows Phone 更新GDR2後新增了一個隱藏功能,允許APP禁用截圖功能。
PhoneApplicationPage.IsScreenCaptureEnabled這個隱藏的屬性需要通過反射來訪問和修改狀態。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public static class PhoneApplicationPageExtensionMethods
    {
    public static bool CanSetScreenCaptureEnabled(this PhoneApplicationPage page)
        {
return Environment.OSVersion.Version >= new Version(8010322);
        }
        public static void SetScreenCaptureEnabled(this PhoneApplicationPage page, bool enabled)
        {
var propertyInfo = typeof(PhoneApplicationPage).GetProperty("IsScreenCaptureEnabled");
if (propertyInfo == null)
{
    throw new NotSupportedException("Not supported in this Windows Phone version!");
}
propertyInfo.SetValue(page, enabled);
        }
        public static bool GetScreenCaptureEnabled(this PhoneApplicationPage page)
        {
var propertyInfo = typeof(PhoneApplicationPage).GetProperty("IsScreenCaptureEnabled");
if (propertyInfo == null)
{
    throw new NotSupportedException("Not supported in this Windows Phone version!");
}
return (bool)propertyInfo.GetValue(page);
        }
    }
}

呼叫CanSetScreenCaptureEnabled()方法檢測Windows Phone版本是否符合要求(version 8.0.10322以上)。符合條件,然後就通過擴充套件方法GetScreenCaptureEnabled()SetScreenCaptureEnabled()來修改PhoneApplicationPage.IsScreenCaptureEnabled屬性。
使用:

1
2
3
4
5
6
7
8
9
// 建構函式
public MainPage()
{
InitializeComponent();
if (this.CanSetScreenCaptureEnabled())
{
    this.SetScreenCaptureEnabled(false);
}
}

效果
效果

評論

導覽