在 WP 上使用 MvvmCross 拍摄图像并显示图像时出现问题

2024-01-04

我想用相机拍照并将其显示在我所在的页面上

所以我有一个 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();
    }
}

这使用了 CameraService 来拍摄照片并将照片保存在独立的存储中(我知道,问题分离不好)

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 上显示图像的视图是

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

但是当我尝试拍照时,转换器中的这一点会返回 null

if (contents == null)
        return null;

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


我认为您需要单步执行并调试照片代码。

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

一种猜测是您的基于 GUID 的文件名包含无效字符 - 请参阅http://msdn.microsoft.com/en-us/library/system.io.path.getinvalidpathchars%28v=vs.95%29.aspx http://msdn.microsoft.com/en-us/library/system.io.path.getinvalidpathchars%28v=vs.95%29.aspx Try Guid.ToString("N") + ".jpg"一个稍微不那么混乱的文件名

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 WP 上使用 MvvmCross 拍摄图像并显示图像时出现问题 的相关文章

随机推荐

  • 如何注释 Django JSONField (对象数组)数据的总和?

    我有这样的模型 models py class MyModel models Model orders models JsonField null True blank True default list category models F
  • 通过 IClipboardDataPasteEventImpl 发生内存泄漏

    我注意到我的一项活动中的记忆力出现了奇怪的增长 因此我进行了一些测试 我多次打开对话框 打开 关闭 打开 关闭 并且内存不断增加 所以我使用 DDMS 转储 HPROF 文件并在MAT http www eclipse org mat 内存
  • 要使用的 Hibernate 或 JPA 注释

    我在我们的项目中使用 Hibernate 并为 Hibernate 域 Pojo 对象使用基于注释的配置 对于基于注释的配置 我们有两个选项 基于 JPA 的注释使用javax persistence 使用 Hibernate 本机注释or
  • 在javascript中通过给定的电话号码检测国家/地区代码[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我有一个包含所有可用国家 地区代码的对象 我想知道如何通过给定的电话号码获取国家 地区代码并显示相应的国家 地区名称 电话号码将类似于 1
  • 我需要一个用于 Win/Linux 的二进制比较工具 [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 首先 我不需要文本比较 因此 Beyond Compare 不能满足我的需要 我正在寻找一个可以在字节级别报告两个文件之间差异的实用程序
  • 我可以在没有公共 IP 的情况下为 Azure 虚拟机提供 Internet 访问权限吗?

    我在azure上有3个debian VM 其中一个有一个可以上网的公共IP VM 1 其他只有内部网络 VM 2 VM 3 我可以通过 VM 1 授予对 VM 2 或 VM 3 的访问权限吗 让我崩溃的是看到 VM 1 有 2 个网络接口
  • 制作 Laravel 集合的副本

    我正在尝试提供一份集合的副本users到一个雄辩的模型jobs 所以我实际上有 jobs 1 users 1 2 3 2 users 1 2 3 一旦我得到这个 我将对另一个查询中的一些数字进行求和 本质上为每个作业的每个用户提供一个总数
  • 使用 SQL 查询在 DB2 中插入 BLOB 数据

    我遇到了这样的情况 我需要通过从 DB2 Windows 7 上的 DB2 Express C 中的文件系统读取文件来将数据插入到 blob 列中 我在互联网上的某个地方找到了这个INSERT INTO VALUES readfile fi
  • Windows 身份验证混合

    我正在对 Intranet MVC 应用程序使用 Windows 身份验证 我想在身份验证过程中添加额外的逻辑 换句话说 用户除了存在于 AD 中之外 还必须存在于自定义数据库中才能进行身份验证 他们还应该注销 MVC 应用程序 然后使用相
  • RegexBuddy 的免费替代品 [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 是否有任何好的替代方案支持以不同风格编写正则表达式并允许您测试它们 以下是线程中提到的正则表达式工具的
  • 当使用 grunt 构建项目时,Fontawesome 无法工作

    我正在使用很棒的字体库字体 当项目不是用 grunt 构建 丑化时它可以工作 但是当我用 grunt 构建项目时 它不起作用 我在控制台中收到此错误 fonts fontawesome webfont woff v 4 0 3 404 未找
  • Visual Studio 2013团队项目已被删除

    在 vs 2013 中向源代码管理添加新的 Web 解决方案并首次签入后 我收到此错误 TF402484 The PROJECTNAME team project has been deleted Undo any pending chan
  • 将 JAX-WS 2.2.5 客户端与 JDK/JRE 1.5 结合使用

    Java 6 附带 JAX WS 2 0 据我所知 Java 5 并未附带 JAX WS 我能够将 JAX WS 2 2 5 与 Java 1 6 结合使用 通过使用Java认可的覆盖机制 https docs oracle com jav
  • PHP 生成的不完整时区列表

    我尝试生成中指定的完整时区集http php net manual en timezones php http php net manual en timezones php UTC 除外 使用以下代码 zones timezone ide
  • 如何在 Windows 上设置 Eclipse + StatET + Rcpp

    当我知道我可以使用 Rcpp 用 C 创建 R 包时 我感到很兴奋 并渴望了解它的开发环境 并感谢秋季统计博客 http blog fellstat com p 170 我可以使用 Eclipse 和 StatET 它的 R 插件 快速建立
  • 如何将 v-for 的值绑定到 v-if

    我正在使用 BootstrapVue 对于我的问题 我有一个v for在我的模板中有两个buttons 循环我的v for my v if不生成唯一的IDs单击一个按钮后 每个按钮都会被触发 from Open me to Close me
  • Heroku Godaddy 裸域 [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我有一个 Heroku 应用程序 并且添加了 CNAME www 到 herokuapp 以将其从 GoDaddy 重定向到 Heroku
  • 嵌套 VB (VBA) 枚举

    好吧 伙计们 我想实现嵌套枚举的效果 以便轻松对一些常量字符串进行分组 类似于下面的伪代码 Enum gKS Colby Hello Hays World end Enum Enum gMA Dodge Seven Muscatine Po
  • jQuery,未捕获的类型错误

    我的网页上有一些 javascript 代码 正在将一些 div 加载到页面上 我还想向每个 div 添加 onmouseenter 和 onmouseleave 事件处理程序 我正在使用 jquery 添加这些处理程序 但出现错误 对象
  • 在 WP 上使用 MvvmCross 拍摄图像并显示图像时出现问题

    我想用相机拍照并将其显示在我所在的页面上 所以我有一个 ViewModel 我可以在其中拍照并显示它 public class CamViewModel MvxViewModel IMvxServiceConsumer