【问题标题】:WritableBitmap to ImageSource in MVVMWriteableBitmap 到 MVVM 中的图像源
【发布时间】:2016-08-02 02:04:12
【问题描述】:

最终目标:使用 WPF 和 MVVM 打印二维条码:

背景信息(可能不相关)我的解决方案中有两个项目。一个项目控制我的业务逻辑,第二个项目控制标签的打印逻辑、创建和打印。我正在为 IPC 使用命名管道。我正在使用 MVVM 并有一个 Xaml 模板来设计标签,并在运行时填充它的属性并打印它。这一切正常。

更多信息:(可能相关)我正在使用创建二维条码的第三方库。这是有效的,使条形码返回一个可写位图的调用

问题: 我正在尝试将可写位图数据绑定到模板上的 Image 控件。

public void FooBar(string[] LabelProperties)
{
    try
    {
        BarcodeWriter writer = new BarcodeWriter()
        {
           Format = BarcodeFormat.PDF_417,
           Options = new ZXing.Common.EncodingOptions
           {
              Height = 50,
              Width = 132,
              Margin = 0
           }
        };

        var wb = writer.Write("Some String");

        System.Windows.Controls.Image newImage = new System.Windows.Controls.Image()
        {
           Height = 50,
           HorizontalAlignment = HorizontalAlignment.Left,
           Name = "image",
           Stretch = Stretch.None,
           VerticalAlignment = VerticalAlignment.Top,
           Width = 132,
           Source = wb,
        };

        this.BarCodeImage = newImage;
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message.ToString().Trim());
    }
}

值得注意的是,我不能直接将 WritableBitmap 放到 BarCodeImage.Source 中

this.BarCodeImage.Source = wb;

因为我使用的是 MVVM 设计,所以 BarCodeImage 没有被实例化,所以如果我尝试为其源设置一些东西,它会抛出一个空引用。

模板中的 XAML

<Image Height="50" 
       HorizontalAlignment="Left" 
       Margin="10,2,0,0" 
       Name="image" 
       Stretch="None" 
       VerticalAlignment="Top" 
       Width="132" 
       Source="{Binding lblBarCodeImage}" />

我的想法因为我必须实例化一个新的 Controls.Image() 然后将其设置为 BarCodeImage 它以某种方式打破了这一点。

其他事情我可以显示其他类和设置以证明我的 MVVM 设置正确,但所有其他控件都正确数据绑定 - 尽管它们都是我正在数据绑定的字符串 - 没有其他图像控制。

我也试过将 WritableBitmap 转换为字节数组和tried using this solution, but that also did not work

【问题讨论】:

  • 在 XAML 的 Source 绑定中声明的 lblBarCodeImage 属性在哪里?将 wb 分配给该属性。除此之外,不要在后面的代码中创建 UI 元素(如 System.Windows.Controls.Image)。在 XAML 中声明它们并绑定它们的属性。
  • @Clemens 我在稍后的执行中将 lblBarCodeImage 设置为 this.BarCodeImage。 FooBar 调用返回一个对象,其中包含我要绑定的所有值。然后我将该对象传递给我的视图模型,在该模型中它被设置为 lblBarCodeImage 属性。我认为这工作正常,因为我在示例中遗漏了其他正常工作的字符串。你认为我的问题是在后面的代码中创建 Controls.image 吗?

标签: c# wpf mvvm


【解决方案1】:

不要在后面的代码中创建图像控件!

相反,声明一个 ImageSource 类型的视图模型属性,并在 XAML 中将 Image 控件的 Source 属性绑定到该视图模型属性。

查看模型:

public class YourViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private ImageSource barCodeImage;

    public ImageSource BarCodeImage
    {
        get { return barCodeImage; }
        set
        {
            barCodeImage = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("BarCodeImage"));
        }
    }

    ...
}

XAML:

<Image ... Source="{Binding BarCodeImage}"/>

在后面的代码中,将 WriteableBitmap 分配给 BarCodeImage 属性:

yourViewModel.BarCodeImage = wb;

【讨论】:

  • 我已经解决了,谢谢你让我走上正轨。我将控件的源数据绑定到 control.Image。 (即 Foo1.Bar = Foo2,而不是 Foo1.Bar = Foo2.Bar)一旦我经历并纠正了这一切都很好。我不必打电话给物业改变,但我会给你答案,因为它可能会帮助别人!非常感谢楼主
【解决方案2】:

你应该有一个像这样的 BitmapImage 属性:

    private BitmapImage photoSelected;

    public BitmapImage PhotoSelected
    {
        get { return photoSelected; }
        set { photoSelected = value; OnPropertyChanged("PhotoSelected"); }
    }

然后在您希望执行的操作上:

PhotoSelected = new BitmapImage(new Uri(@"pack://application:,,,/Images/4.png"));

将 /Images/4.png 替换为从解决方案级别开始的图像路径。例如,这就是我的解决方案树达到这一点的样子:

XAML 用于绑定:

<Image x:Name="BitMapImage" Source="{Binding PhotoSelected, Mode=TwoWay}" RenderOptions.BitmapScalingMode="HighQuality"/>

【讨论】:

  • 在 Source 绑定上设置 Mode=TwoWay 绝对没有意义。它没有任何作用,因为 Image 控件永远不会更改其 Source 属性的值。除此之外,为什么不简单地写PhotoSelected = wb; 来尝试参考这个问题呢?因此PhotoSelected 属性的类型应该是ImageSource,而不是BitmapImageImageSource 是基类,因此该属性将更灵活地使用。
  • 这是我的一个项目中的一个 sn-p。我需要 TwoWay,因为整个项目并不完全是 MVVM,所以后面的代码中有一些操作可能会更改映像,我需要在我的 VM 中访问该映像。我的目标不是为他提供代码完整的答案,而是分享我的经验,为他指明正确的方向。
  • 你为什么不花 2 秒钟把它编辑出来?
  • 感谢您的回复,但在将其设置为我的图像的来源之前,我宁愿不保存此图像。这些条码的创建过于频繁,无法尝试保存每个条码。
  • 你不需要保存它。这就是我在示例中的做法,但是,只要您将“PhotoSelected”(或任何您的绑定属性)绑定到 BitmapImage。 Here are instructions for converting Writeable Bitmap to Bitmap.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-06
  • 2021-07-09
  • 1970-01-01
相关资源
最近更新 更多