Skip to content

Commit 494a7f5

Browse files
committed
Add Barcode Sample
1 parent 04e3e6b commit 494a7f5

4 files changed

Lines changed: 124 additions & 0 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ Install-Package DevWinUI.SourceGenerator
124124
### NTPClient
125125
![NTPClient](https://raw.githubusercontent.com/ghost1372/DevWinUI-Resources/refs/heads/main/DevWinUI-Docs/NTPClient.png)
126126

127+
### Barcode
128+
![Barcode](https://raw.githubusercontent.com/ghost1372/DevWinUI-Resources/refs/heads/main/DevWinUI-Docs/Barcode.png)
129+
127130
### QRCode
128131
![QRCode](https://raw.githubusercontent.com/ghost1372/DevWinUI-Resources/refs/heads/main/DevWinUI-Docs/QRCode.png)
129132

dev/DevWinUI.Gallery/Assets/NavViewMenu/AppData.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
"Title": "Common",
2020
"ImagePath": "ms-appx:///Assets/Fluent/Extensions.png",
2121
"Items": [
22+
{
23+
"UniqueId": "DevWinUIGallery.Views.BarcodePage",
24+
"Title": "BarCode",
25+
"IsNew": true,
26+
"Subtitle": "Generate BarCode",
27+
"ImagePath": "ms-appx:///Assets/Fluent/QRCode.png"
28+
},
2229
{
2330
"UniqueId": "DevWinUIGallery.Views.PasswordGeneratorPage",
2431
"Title": "Password Generator",
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<Page x:Class="DevWinUIGallery.Views.BarcodePage"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:dev="using:DevWinUI"
7+
xmlns:local="using:DevWinUIGallery"
8+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9+
mc:Ignorable="d">
10+
<ScrollViewer>
11+
<StackPanel Margin="10" dev:PanelAttach.ChildrenTransitions="Default" Spacing="10">
12+
<local:ControlExample DocPage="common/barcode" DocType="Core" HeaderText="Barcode">
13+
<local:ControlExample.CSharp>
14+
<x:String>
15+
var barcode = Barcode.CreateCode128(TxtInput.Text);
16+
</x:String>
17+
</local:ControlExample.CSharp>
18+
<local:ControlExample.Pane>
19+
<StackPanel Spacing="10">
20+
<TextBox Name="TxtInput" PlaceholderText="Input" Text="DevWinUI" TextChanged="TxtInput_TextChanged" />
21+
<ComboBox x:Name="CmbType" Header="Barcode Type" ItemsSource="{x:Bind BarcodeTypeItems, Mode=OneWay}" SelectedIndex="0" SelectionChanged="CmbType_SelectionChanged" />
22+
</StackPanel>
23+
</local:ControlExample.Pane>
24+
<Image x:Name="OutPutImg" />
25+
</local:ControlExample>
26+
</StackPanel>
27+
</ScrollViewer>
28+
</Page>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System.Data;
2+
using Microsoft.UI.Xaml.Media.Imaging;
3+
using WinRT;
4+
5+
namespace DevWinUIGallery.Views;
6+
7+
public sealed partial class BarcodePage : Page
8+
{
9+
public ObservableCollection<BarcodeType> BarcodeTypeItems { get; set; } = new ObservableCollection<BarcodeType>(Enum.GetValues<BarcodeType>());
10+
11+
public BarcodePage()
12+
{
13+
InitializeComponent();
14+
Loaded += OnLoaded;
15+
}
16+
17+
private void OnLoaded(object sender, RoutedEventArgs e)
18+
{
19+
Generate();
20+
}
21+
22+
private void TxtInput_TextChanged(object sender, TextChangedEventArgs e)
23+
{
24+
Generate();
25+
}
26+
27+
private void Generate()
28+
{
29+
if (string.IsNullOrEmpty(TxtInput.Text))
30+
return;
31+
32+
Barcode barcode = null;
33+
var item = CmbType.SelectedItem.As<BarcodeType>();
34+
switch (item)
35+
{
36+
case BarcodeType.Code128:
37+
barcode = Barcode.CreateCode128(TxtInput.Text);
38+
break;
39+
case BarcodeType.Code93:
40+
barcode = Barcode.CreateCode93(TxtInput.Text);
41+
break;
42+
case BarcodeType.Code39:
43+
barcode = Barcode.CreateCode39(TxtInput.Text);
44+
break;
45+
case BarcodeType.Ean13:
46+
barcode = Barcode.CreateEan13(TxtInput.Text);
47+
break;
48+
case BarcodeType.Ean8:
49+
barcode = Barcode.CreateEan8(TxtInput.Text);
50+
break;
51+
case BarcodeType.Itf:
52+
barcode = Barcode.CreateItf(TxtInput.Text);
53+
break;
54+
case BarcodeType.Codabar:
55+
barcode = Barcode.CreateCodabar(TxtInput.Text);
56+
break;
57+
case BarcodeType.UpcA:
58+
barcode = Barcode.CreateUpcA(TxtInput.Text);
59+
break;
60+
}
61+
62+
var png = barcode.ToPng();
63+
using (var stream = new MemoryStream(png))
64+
{
65+
var bitmapImage = new BitmapImage();
66+
bitmapImage.SetSource(stream.AsRandomAccessStream());
67+
OutPutImg.Source = bitmapImage;
68+
}
69+
}
70+
71+
private void CmbType_SelectionChanged(object sender, SelectionChangedEventArgs e)
72+
{
73+
Generate();
74+
}
75+
}
76+
public enum BarcodeType
77+
{
78+
Code128,
79+
Code93,
80+
Code39,
81+
Ean13,
82+
Ean8,
83+
Itf,
84+
Codabar,
85+
UpcA
86+
}

0 commit comments

Comments
 (0)