有些时候,我们可能会想到不要让应用程序界面上显示的内容被截屏,要阻止应用界面呈现在截图上,可以在 ApplicationView 类上找答案,因为这个行为是跟应用程序视图有直接关系的。
我们只需要设置一个属性就成了——IsScreenCaptureEnabled,如果允许程序被捕捉到,就设置为 true,要禁止被捕捉,设置为 false 就好了。简单吧。
看一个例子,先看XAML布局。
<Grid Margin="15"> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition/> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <TextBlock Text="中国高富帅的形象代表" FontSize="25" HorizontalAlignment="Center" Margin="0,6"/> <Image Grid.Row="1" Margin="3" Source="Assets\1.png" /> <CheckBox Grid.Row="2" Margin="0,8" Content="禁止被截屏" FontSize="18" HorizontalAlignment="Center" Checked="OnChecked" Unchecked="OnUnChecked"/> </Grid>
重点是 CheckBox 控件,我处理了它的 Checked 和 UnChecked 事件,如果它被选中,就禁止应用界面被捕捉;如果没选中,就允许捕捉。
下面是实现代码。
ApplicationView appView = ApplicationView.GetForCurrentView(); private void OnChecked(object sender, RoutedEventArgs e) { appView.IsScreenCaptureEnabled = false; } private void OnUnChecked(object sender, RoutedEventArgs e) { appView.IsScreenCaptureEnabled = true; }