wpf中的条码生成器
在wpf中生成各种类型条形码的最佳方法。最简单的方法可能是使用条形码字体。例如:wpf中的条码生成器,wpf,prism,Wpf,Prism,在wpf中生成各种类型条形码的最佳方法。最简单的方法可能是使用条形码字体。例如: <Window x:Class="BarcodeTest.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Bar Code Test" Height="209"
<Window x:Class="BarcodeTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Bar Code Test" Height="209" Width="426">
<Grid>
<TextBlock FontFamily="IDAutomationHC39M" FontSize="30"
SnapsToDevicePixels="True"
VerticalAlignment="Center"
HorizontalAlignment="Center">*0123456789*</TextBlock>
</Grid>
</Window>
<ContentPresenter Content="123456789"
ContentTemplate="{StaticResource BarcodeTemplate}" />
本例使用IDAutomationHC39M字体。生成的条形码如下所示:
此网页列出了各种免费软件或共享软件条形码字体:。条形码字体是一种简单的方法,但它们可能很难部署,因为客户可能需要在运行应用程序的每台计算机上安装该字体。这可能是不切实际的,特别是对于XBAP、Silverlight和WinPhone7部署 在WPF中自己生成条形码非常容易。虽然可以在纯XAML中完成,但我发现混合使用XAML和代码是最简单的 从一个转换器开始,该转换器接受一个字符并返回条形刷和宽度:
public class BarcodeConverter : IValueConverter
{
public static readonly BarcodeConverter Instance = new BarcodeConverter();
Dictionary<char, string> _codes = new Dictionary<char, string>
{
{ '0', "nnnwwnwnn" },
{ '1', "wnnwnnnnw" },
{ '2', "nnwwnnnnw" },
// etc
{ 'A', "wnnnnwnnw" },
{ 'B', "nnwnnwnnw" },
// etc
};
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string code;
if(!_codes.TryGetValue((char)value, out code)) return null;
return
from i in Enumerable.Range(0, code.Length)
select new
{
color = i%2==0 ? Brushes.Black : Brushes.Transparent,
width = code[i]=='n' ? 5 : 10,
};
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
现在,构建条形图的XAML非常简单:
<ItemsPanelTemplate x:Key="HorizontalPanel">
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
<DataTemplate x:Key="SingleBarTemplate">
<Rectangle Fill="{Binding color}" Width="{Binding width}" />
</DataTemplate>
<DataTemplate x:Key="SingleCodeTemplate">
<DockPanel>
<TextBlock DockPanel.Dock="Bottom" Text="{Binding}" /> <!-- Display character at bottom of code -->
<ItemsControl ItemsSource="{Binding Converter={x:Static my:BarcodeConverter.Instance}}"
ItemsPanel="{StaticResource HorizontalPanel}"
ItemTemplate="{StaticResource SingleBarTemplate}" />
</DockPanel>
</DataTemplate>
<DataTemplate x:Key="BarcodeTemplate">
<ItemsControl ItemsSource="{Binding}"
ItemsPanel="{StaticResource HorizontalPanel}"
ItemTemplate="{StaticResource SingleBarTemplate}"
Height="60" />
</DataTemplate>
条形码模板可与ContentPresenter一起用于显示任何字符串,例如:
<Window x:Class="BarcodeTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Bar Code Test" Height="209" Width="426">
<Grid>
<TextBlock FontFamily="IDAutomationHC39M" FontSize="30"
SnapsToDevicePixels="True"
VerticalAlignment="Center"
HorizontalAlignment="Center">*0123456789*</TextBlock>
</Grid>
</Window>
<ContentPresenter Content="123456789"
ContentTemplate="{StaticResource BarcodeTemplate}" />
或
我认为该解决方案是在WPF中生成条形码的最佳方法,因为:
它是简单而独立的,
它不依赖于计算机上安装的字体,并且
它可以在任何WPF或Silverlight实现上工作。
以下是我最后得到的:
<Window x:Class="ScratchClient.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="655.85" Width="687"
xmlns:local="clr-namespace:ScratchClient">
<Window.Resources>
<local:BarcodeConverter x:Key="barcodeConverter"/>
<ItemsPanelTemplate x:Key="HorizontalPanel">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center"/>
</ItemsPanelTemplate>
<DataTemplate x:Key="SingleBarTemplate">
<Rectangle Fill="{Binding color}" Width="{Binding width}" />
</DataTemplate>
<DataTemplate x:Key="SingleCodeTemplate">
<DockPanel>
<TextBlock DockPanel.Dock="Bottom" Text="{Binding}" />
<ItemsControl ItemsSource="{Binding Converter={StaticResource barcodeConverter}}"
ItemsPanel="{StaticResource HorizontalPanel}"
ItemTemplate="{StaticResource SingleBarTemplate}" />
</DockPanel>
</DataTemplate>
<DataTemplate x:Key="BarcodeTemplate">
<ItemsControl ItemsSource="{Binding}"
ItemsPanel="{StaticResource HorizontalPanel}"
ItemTemplate="{StaticResource SingleCodeTemplate}"
Height="100" />
</DataTemplate>
</Window.Resources>
<Grid>
<ContentPresenter Name="barcode" Content="*WIKIPEDIA*" ContentTemplate="{StaticResource BarcodeTemplate}">
<ContentPresenter.RenderTransform>
<ScaleTransform ScaleX="2" ScaleY="2"></ScaleTransform>
</ContentPresenter.RenderTransform>
</ContentPresenter>
</Grid>
我已经在github中创建了一个开源控件:,请尝试
希望这能有所帮助。+1这是WPF各个部分的巧妙运用。我没有特别的用途,但它很整洁!