Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WP上使用MvvmCross拍摄图像并显示的问题_C#_Mvvmcross - Fatal编程技术网

C# 在WP上使用MvvmCross拍摄图像并显示的问题

C# 在WP上使用MvvmCross拍摄图像并显示的问题,c#,mvvmcross,C#,Mvvmcross,我想用相机拍一张照片,并把它显示在我所在的页面上 因此,我有一个ViewModel,在这里我既拍摄了照片,又展示了它 public class CamViewModel : MvxViewModel, IMvxServiceConsumer<IInstalledMeter>, IMvxServiceConsumer<ICamaraService> { public CamViewModel() { this.

我想用相机拍一张照片,并把它显示在我所在的页面上

因此,我有一个ViewModel,在这里我既拍摄了照片,又展示了它

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();
    }
}

因此,我认为这意味着图像不可访问/保存或文件名错误,但不确定错误发生在哪里

我认为您需要逐步完成并调试照片代码

您还可以尝试向异常捕获处理程序添加一些跟踪

一种猜测是,基于GUID的文件名包含无效字符-请参阅Try
GUID.ToString(“N”)+”.jpg“
,以了解稍微不那么混乱的文件名


但实际上,您需要跟踪代码并找出错误发生的位置。

使用var fileName=Guid.NewGuid().ToString(“N”)+“.jpg”;给我一个像Image\1240aacf08144a3ea3548f4d068dfdb6.jpg这样的文件名,如果我调试它,它也会出现在转换器中,但它仍然不工作,有没有办法可以看到文件中发生了什么。TryReadBinaryFile((字符串)值,out contents);我似乎无法在调试过程中介入它,因为它返回null,所以一定是因为发生了一些错误。有iso文件存储查看器-它们有帮助吗?还要注意在WP8中使用WP7构建二进制文件-我看到了一些奇怪的效果。
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 "";
        }
    }
}
<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>
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;
        }
    }
if (contents == null)
        return null;