[英]how to take current screen shot in mvvm windows phone app
How do we take screenshot in mvvm windows phone application. 我们如何在mvvm Windows Phone应用程序中截屏。
I tried the below code but it doesn't work in mvvm app as it requires uielement .Any suggests how to do this in mvvm app ? 我尝试了以下代码,但由于需要uielement而无法在mvvm应用程序中运行。任何人都建议如何在mvvm应用程序中执行此操作?
var screenshot = new WriteableBitmap(this, null);
var screenshotname = String.Format("Screenshooter_{0}", DateTime.Now.Ticks);
using (var ms = new MemoryStream())
{
screenshot.SaveJpeg(ms, 480, 800, 0, 85);
ms.Seek(0, SeekOrigin.Begin);
var library = new MediaLibrary();
var pic = library.SavePicture(screenshotname, ms);
}
UtilityLib.ShowMessageDialog(string.Concat("Screenshot saved as ", screenshotname));
Try this: 尝试这个:
We should register each service in ViewModelLocator, as following: 我们应该在ViewModelLocator中注册每个服务,如下所示:
using Cimbalino.Phone.Toolkit.Services;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using Microsoft.Practices.ServiceLocation;
/// <summary>
/// This class contains static references to all the view models in the
/// application and provides an entry point for the bindings.
/// </summary>
public class ViewModelLocator
{
/// <summary>
/// Initializes a new instance of the ViewModelLocator class.
/// </summary>
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
if (!SimpleIoc.Default.IsRegistered<IScreenshotService>())
{
SimpleIoc.Default.Register<IScreenshotService, ScreenshotService>();
}
SimpleIoc.Default.Register<MainViewModel>();
}
/// <summary>
/// Gets the main view model.
/// </summary>
/// <value>
/// The main view model.
/// </value>
public MainViewModel MainViewModel
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
public static void Cleanup()
{
// TODO Clear the ViewModels
var viewModelLocator = (ViewModelLocator)App.Current.Resources["Locator"];
viewModelLocator.MainViewModel.Cleanup();
}
}
Then we should implement the MainViewModel as following: 然后,我们应该实现MainViewModel,如下所示:
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using Cimbalino.Phone.Toolkit.Services;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight;
/// <summary>
/// This class contains properties that the main View can data bind to.
/// </summary>
public class MainViewModel : ViewModelBase
{
/// <summary>
/// The screenshot service
/// </summary>
private readonly IScreenshotService _screenshotService;
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel(IScreenshotService screenshotService)
{
_screenshotService = screenshotService;
TakeScreenshotCommand = new RelayCommand(TakeScreenshot);
}
/// <summary>
/// Gets the take screenshot command.
/// </summary>
/// <value>
/// The take screenshot command.
/// </value>
public ICommand TakeScreenshotCommand { get; private set; }
/// <summary>
/// Calls to.
/// </summary>
private void TakeScreenshot()
{
_screenshotService.TakeScreenshot("CimbalinoScreenshot");
}
}
for connect view model with the page we should add the ViewModelLocator instance in App.xaml: 为了将视图模型与页面连接,我们应该在App.xaml中添加ViewModelLocator实例:
XAML XAML
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
and add the binding in main page like: 并在主页中添加绑定,例如:
XAML XAML
DataContext="{Binding MainViewModel,
Source={StaticResource Locator}}"
The MainPage.xaml can be the following: MainPage.xaml可以是以下内容:
XAML XAML
<phone:PhoneApplicationPage x:Class="CimbalinoSample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
DataContext="{Binding MainViewModel,
Source={StaticResource Locator}}"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
Orientation="Portrait"
SupportedOrientations="Portrait"
shell:SystemTray.IsVisible="True"
mc:Ignorable="d">
<!-- 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 Margin="12,0"
Style="{StaticResource PhoneTextTitle2Style}"
Text="Cimbalino Sample" />
<TextBlock Margin="9,-7,0,0"
Style="{StaticResource PhoneTextTitle2Style}"
Text="ScreenshotService" />
</StackPanel>
<!-- ContentPanel - place additional content here -->
<Grid x:Name="ContentPanel"
Grid.Row="1"
Margin="12,0,12,0">
<TextBlock TextWrapping="Wrap">This samples has the goal to show how to use Cimbalino Windows Phone Toolkit Media Library - ScreenshotService</TextBlock>
<Button Margin="0,219,0,293"
Command="{Binding TakeScreenshotCommand}"
Content="Take Screenshot" />
</Grid>
</Grid>
</phone:PhoneApplicationPage>
Source : http://code.msdn.microsoft.com/wpapps/How-to-use-Cimbalino-749562db 来源: http : //code.msdn.microsoft.com/wpapps/How-to-use-Cimbalino-749562db
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.