问题上采取WP图像和显示他们与MvvmCross [英] Issues taking images and showing them with MvvmCross on WP
问题描述
我要使用相机拍摄的照片,并显示它我在
I want to take a photo with the camera and show it on the page I am at
所以我有一个视图模型,我都拍摄照片,并显示它
So I have a ViewModel where I both take the picture and show it
public class CamViewModel : MvxViewModel,
IMvxServiceConsumer<IInstalledMeter>,
IMvxServiceConsumer<ICamaraService>
{
public CamViewModel()
{
this.GetService<ICamaraService>().PhotoSavedEvent += PhotoSaved;
if (!String.IsNullOrEmpty(this.GetService<IInstalledMeter>().ImagePath))
{
ImagePath = this.GetService<IInstalledMeter>().ImagePath;
}
TakePicture();
}
private string _imagePath;
public string ImagePath
{
get { return _imagePath; }
set { _imagePath = value; FirePropertyChanged("ImagePath"); }
}
//Navigate back to InstallUnit
public IMvxCommand OpenCamaraCommand
{
get
{
return new MvxRelayCommand(TakePicture);
}
}
private void PhotoSaved(object sender, PhotoSavedResultEventArgs e)
{
ImagePath = e.ImagePath;
}
private void TakePicture()
{
this.GetService<ICamaraService>().TakePhoto();
}
}
这使用CameraService,无论拍摄照片并保存画面上的独立存储(我知道,不好的关注分离)
This uses a CameraService that both takes the picture and saves the picture on isolated storage (I know, bad seperation of concern)
public class CamaraService : IMvxServiceConsumer<IMvxPictureChooserTask>, IMvxServiceConsumer<IMvxSimpleFileStoreService>, IMvxServiceConsumer<IInstalledMeter>, ICamaraService
{
private const int MaxPixelDimension = 1024;
private const int DefaultJpegQuality = 70;
public event EventHandler<PhotoSavedResultEventArgs> PhotoSavedEvent;
private void PhotoTaken(PhotoSavedResultEventArgs e)
{
if (PhotoSavedEvent != null)
{
PhotoSavedEvent(this, e);
}
}
public string ImagePath { get; set; }
public void TakePicture()
{
this.GetService<IMvxPictureChooserTask>().TakePicture(
MaxPixelDimension,
DefaultJpegQuality,
SavePicture,
() => { /* cancel is ignored */ });
}
public void TakePhoto()
{
TakePicture();
}
public void SavePicture(Stream image)
{
var newImage = Save(image);
if (newImage != "")
{
DeleteOldImage(this.GetService<IInstalledMeter>().ImagePath);
this.GetService<IInstalledMeter>().ImagePath = newImage;
PhotoTaken(new PhotoSavedResultEventArgs {ImagePath = newImage});
}
}
public void UpdateModel(string filename)
{
}
public void DeleteOldImage(string fileName)
{
try
{
if (String.IsNullOrEmpty(fileName)) return;
var fileService = this.GetService<IMvxSimpleFileStoreService>();
fileService.DeleteFile(fileName);
}
catch
{
}
}
public string Save(Stream stream)
{
try
{
var fileName = Guid.NewGuid().ToString();
var fileService = this.GetService<IMvxSimpleFileStoreService>();
fileName = Path.Combine("Image", fileName);
fileService.WriteFile(fileName, stream.CopyTo);
return fileName;
}
catch (ThreadAbortException)
{
throw;
}
catch (Exception exception)
{
return "";
}
}
}
在Windows Phone的视图显示图像
The View on Windows Phone to display the image is
<Views:BaseCamView
x:Class="UI.WP7.Views.InstallMeter.CamView"
xmlns:Views="clr-namespace:UI.WP7.Views.InstallMeter"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
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:nativeConverters="clr-namespace:UI.WP7.NativeConverters"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">
<Views:BaseCamView.Resources>
<nativeConverters:PathToImageConverter x:Name="PathToImageConverter" />
</Views:BaseCamView.Resources>
<!--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="MY sdf" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock Text="{Binding ImagePath}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<Image Grid.Row="1" Source="{Binding ImagePath, Converter={StaticResource PathToImageConverter}}" Height="200" Width="700" Stretch="Uniform" />
</Grid>
和这个视图使用一个转换器将路径转换为BitmapImage的
And this view uses a converter to convert the path to a bitmapimage
public class PathToImageConverter : IValueConverter, IMvxServiceConsumer<IMvxSimpleFileStoreService>
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
byte[] contents = null;
try
{
var file = this.GetService();
file.TryReadBinaryFile((string)value, out contents);
}
catch (Exception)
{
// masked
}
if (contents == null)
return null;
using (var stream = new MemoryStream(contents))
{
var image = new BitmapImage();
image.SetSource(stream);
return image;
}
}
不过,当过我尝试拍照此位在转炉返回null
But when ever I try to take a picture this bit in the converter returns null
if (contents == null)
return null;
所以我推测,这意味着图像不可访问/保存或文件名是错误的,但不知道当错误发生
So I presume that means the image is not accessable / saved or the filename is wrong, but not sure where the error happens
推荐答案
我认为你需要单步调试和调试图片代码。
I think you need to step through and debug the photo code.
您也可以尝试加入一些跟踪的异常捕获处理
You could also try adding some trace to the Exception catch handlers.
一个猜测是,您的GUID基于文件名包含无效字符 - 见< A HREF =http://msdn.microsoft.com/en-us/library/system.io.path.getinvalidpathchars%28v=vs.95%29.aspx相对=nofollow> HTTP:// MSDN。 microsoft.com/en-us/library/system.io.path.getinvalidpathchars%28v=vs.95%29.aspx 尝试 Guid.ToString(N)+.JPG
的略少错位的文件名
One guess is that your GUID based file name includes invalid characters - see http://msdn.microsoft.com/en-us/library/system.io.path.getinvalidpathchars%28v=vs.95%29.aspx Try Guid.ToString("N") + ".jpg"
for a slightly less mangled file name
但你真的需要通过代码来跟踪并找出错误发生。
But really you need to trace through the code and find out where the error is occurring.
这篇关于问题上采取WP图像和显示他们与MvvmCross的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!