Permalink
Browse files

Added screenshot enabling / disabling

Adapted from Pedro Lamas to add Attached Properties support and no
exceptions:
http://www.pedrolamas.com/2014/01/20/disabling-screenshot-functionality-in-a-windows-phone-app/
  • Loading branch information...
robertmclaws committed Jan 21, 2014
1 parent 2f012c7 commit 3e6be9c2866e80694f8d60dee6aa943b2a9bc825
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XamlEssentials.Helpers;
namespace Microsoft.Phone.Controls
{
public static class PhoneApplicationPageExtensions
{
/// <summary>
/// Disables the ability to capture the contents of the screen for a given PhoneApplicationPage.
/// </summary>
/// <param name="page">The instance of the <see cref="PhoneApplicationPage"/> to disable screenshots for.</param>
public static void DisableScreenshots(this PhoneApplicationPage page)
{
DisplayHelper.SetScreenCaptureEnabled(page, false);
}
/// <summary>
/// Enables the ability to capture the contents of the screen for a given PhoneApplicationPage.
/// </summary>
/// <param name="page">The instance of the <see cref="PhoneApplicationPage"/> to enable screenshots for.</param>
public static void EnableScreenshots(this PhoneApplicationPage page)
{
DisplayHelper.SetScreenCaptureEnabled(page, true);
}
}
}
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Info;
namespace XamlEssentials.Helpers
@@ -24,15 +25,60 @@ public static class DisplayHelper
#endregion
#region Dependency Properties
/// <summary>
/// Attached property that lets you disable screenshots from XAML.
/// </summary>
/// <remarks>Not intended to be used from your application code.</remarks>
public static readonly DependencyProperty ScreenCaptureEnabledProperty =
DependencyProperty.RegisterAttached("ScreenCaptureEnabled", typeof(Boolean), typeof(PhoneApplicationPage), new PropertyMetadata(true));
#endregion
#region Public Methods
/// <summary>
/// Detects if the phisical display size of the device is larger than 5" diagonal.
/// NOT INTENDED TO BE CALLED FROM YOUR APPLICATION CODE.
/// On GDR2 and later: Sets the "IsScreenCaptureEnabled" property on the given PhoneApplicationPage.
/// On GDR1 and earlier: Does nothing.
/// </summary>
/// <param name="page">The <see cref="PhoneApplicationPage"/> to set the value for.</param>
/// <param name="value">The value to set.</param>
/// <remarks>Adapted from code originally posted by Pedro Lamas. http://www.pedrolamas.com/2014/01/20/disabling-screenshot-functionality-in-a-windows-phone-app/ </remarks>
public static void SetScreenCaptureEnabled(PhoneApplicationPage page, Boolean value)
{
if (!PhoneVersionHelper.HasGdr2) return;
var propertyInfo = typeof(PhoneApplicationPage).GetProperty("IsScreenCaptureEnabled");
if (propertyInfo != null)
{
propertyInfo.SetValue(page, value);
}
}
/// <summary>
/// Signifies whether or not the ability to capture the screen contents is enabled. Will not throw an exception if property is not supported.
/// </summary>
/// <param name="page">The <see cref="PhoneApplicationPage"/> to get the value from.</param>
/// <returns>
/// On GDR2 and later: Returns whether or not screen captures are enabled.
/// On GDR1 and earlier: Returns false.</returns>
/// <remarks>Adapted from code originally posted by Pedro Lamas. http://www.pedrolamas.com/2014/01/20/disabling-screenshot-functionality-in-a-windows-phone-app/ </remarks>
public static Boolean GetScreenCaptureEnabled(PhoneApplicationPage page)
{
var propertyInfo = typeof(PhoneApplicationPage).GetProperty("IsScreenCaptureEnabled");
if (propertyInfo == null) return true;
return (bool)propertyInfo.GetValue(page);
}
/// <summary>
/// Detects if the physical display size of the device is larger than 5" diagonal.
/// </summary>
/// <remarks>
/// Adapted from http://developer.nokia.com/Resources/Library/Lumia/#!optimising-for-nokia-phablets/optimising-layout-for-big-screens.html
/// </remarks>
public static bool IsPhablet
public static bool IsPhablet
{
get
{
@@ -31,6 +31,17 @@ public static bool HasGdr3
}
}
/// <summary>
/// Specifies whether this phone has GDR2 or later installed.
/// </summary>
public static bool HasGdr2
{
get
{
return Environment.OSVersion.Version >= Gdr2Version;
}
}
/// <summary>
/// Gets the current GDR version currently installed on a Windows Phone 8 device.
@@ -150,6 +150,7 @@
<Compile Include="AsyncExceptionEventArgs.cs" />
<Compile Include="AsynchronizationContext.cs" />
<Compile Include="DisplayResolutions.cs" />
<Compile Include="Extensions\PhoneApplicationPageExtensions.cs" />
<Compile Include="Extensions\ShellToastExtensions.cs" />
<Compile Include="Helpers\BetaExperienceHelper.cs" />
<Compile Include="Helpers\CalculateFrom.cs" />
@@ -168,7 +169,9 @@
<Compile Include="Helpers\PhoneVersionHelper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Folder Include="Controls\" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\$(TargetFrameworkIdentifier)\$(TargetFrameworkVersion)\Microsoft.$(TargetFrameworkIdentifier).$(TargetFrameworkVersion).Overrides.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\$(TargetFrameworkIdentifier)\$(TargetFrameworkVersion)\Microsoft.$(TargetFrameworkIdentifier).CSharp.targets" />
<ProjectExtensions />
@@ -6,12 +6,14 @@
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"
xmlns:helpers="clr-namespace:XamlEssentials.Helpers;assembly=XamlEssentials.WP8"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
shell:SystemTray.IsVisible="True"
helpers:DisplayHelper.ScreenCaptureEnabled="False">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">

0 comments on commit 3e6be9c

Please sign in to comment.