2016-08-01 26 views
0

最终目标:打印使用WPF和MVVM一个二维条码:WritableBitmap到的ImageSource在MVVM

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

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

问题: 我试图到可写的位图数据绑定到我的模板图像控制。

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它在某种程度上打破这一点。

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

我也曾尝试的WritableBitmap转换成字节数组,tried using this solution, but that also did not work

+0

哪里'lblBarCodeImage'财产宣称你在XAML中的'Source'绑定中使用?将'wb'分配给该属性。除此之外,不要在后面的代码中创建UI元素(如'System.Windows.Controls.Image')。声明它们并在XAML中绑定它们的属性。 – Clemens

+0

@Clemens我在后面的执行过程中将lblBarCodeImage设置为this.BarCodeImage。 FooBar调用返回一个包含我想要绑定的所有值的对象。然后我将该对象传递给我的视图模型,在该模型中它被设置为lblBarCodeImage属性。我认为这是正常工作,因为我有其他字符串,我没有正常工作的例子。你认为我的问题是在后面的代码中创建Controls.image吗? – Brandon

+1

查看我的回答... – Clemens

回答

1

不要并不落后在代码中创建一个Image控件!

取而代之,声明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; 
+0

我已经解决了,谢谢你让我走上正轨。我将控件的来源数据绑定到控件上。图像。 (即Foo1.Bar = Foo2,而不是Foo1.Bar = Foo2.Bar)一旦我经历并纠正这一切都很好。我没必要打电话给财产改变,但我会给你信贷的答案,因为它可能会帮助别人!谢谢你,先生 – Brandon

0

你应该有像这样的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从解决方案开始水平。例如,这是我的解决方案的树看起来像达到这一点:

Solution Tree

XAML用于绑定:

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

在Source绑定上设置'Mode = TwoWay'完全没有意义。它没有效果,因为Image控件永远不会更改其Source属性的值。除此之外,为什么不简单地写'PhotoSelected = wb;'来试图引用这个问题呢?因此'PhotoSelected'属性的类型应该是'ImageSource',而不是'BitmapImage'。 'ImageSource'是基类,因此该属性可以更灵活地使用。 – Clemens

+0

这是我的一个项目的一个片段。我需要TwoWay,因为整个项目不完全是MVVM,所以后面的代码中有一些操作可能会改变图像,并且我需要在虚拟机中可访问的图像。我的目标不是给他提供一个完整的代码答案,而是分享我的经验,以指向正确的方向。 – Kikootwo

+0

你为什么不花两秒钟将它编辑出来? – Clemens