读取包含控制字符/不可见字符的条形码.(使用c#和wpf)



要求:-创建一个Windows应用程序来读取其中包含控制字符/不可见字符的条形码。(使用c#和wpf(

我附上了一个我尝试过的样本解决方案,但我有以下问题:-

该解决方案适用于没有控制字符的条形码,也适用于具有ESC或Enter作为控制字符的条码。但对于像[TAB]、[SHIFT]、[DEL]、[BACKSPACE]这样的控制字符,它不能正常工作。

例如:-

如果我手动输入:1234[ALT]009[REASE ALT]5678[ENTER]:模拟不可见字符但是,如果我尝试通过条形码读取相同的内容(参见条形码附件(,那么所有内容都会被清除。(检入样品溶液(当我试图找出原因时,我发现不可见字符(选项卡控件(没有引发"PreviewTextInput"事件。

请有人提出一个替代解决方案,或者我分享的解决方案需要做哪些更改我实现了所需的功能。

样品溶液和条形码上传

提前感谢!

您的TextBox不接受使用的选项卡

AcceptsTab="True"

为了让它接受这一点,请跳到下一个tabstop。在WPF中,最好使用Binding而不是CodeBehind,所以我为您创建了一个ViewModel来处理输入。

因此,作为第一步,我为您创建了一个ViewModel:

public class ViewModel: INotifyPropertyChanged
{
    private String _inputText;
    private ObservableCollection<String> _resultList = new ObservableCollection<string>();
    private String _resultText;
    public string InputText
    {
        get => _inputText;
        set
        {
            if (value == _inputText) return;
            _inputText = value;
            ProcessInput();
            OnPropertyChanged();
        }
    }
    private void ProcessInput()
    {
        ResultList.Clear();
        ResultText = GetUiFriendlyBarCode(InputText);
    }
    public ObservableCollection<string> ResultList
    {
        get => _resultList;
        set
        {
            if (Equals(value, _resultList)) return;
            _resultList = value;
            OnPropertyChanged();
        }
    }
    public string ResultText
    {
        get => _resultText;
        set
        {
            if (value == _resultText) return;
            _resultText = value;
            OnPropertyChanged();
        }
    }

    private string GetUiFriendlyBarCode(String input)
    {
        StringBuilder barCodeToDisplay = new StringBuilder();
        foreach (char character in input.ToString())
        {
            var uiFriendlyCharCode = GetUiFriendlyCharCode((int)character);
            barCodeToDisplay.Append(uiFriendlyCharCode);
            ResultList.Add(uiFriendlyCharCode);
        }
        return barCodeToDisplay.ToString();
    }
//... implement GetUiFriendlyCharCode and INotifyPropertyChnaged

在Xaml 中添加绑定

    <Label Target="{Binding ElementName=Scan}">_Scan</Label>
    <TextBox AcceptsTab="True" Grid.Column="1" x:Name="Scan" Height="50" Text="{Binding InputText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
    <!-- List all characters separately -->
    <ListBox Grid.Row="2" Grid.ColumnSpan="2" x:Name="Log" ItemsSource="{Binding ResultList}"/>
    <!-- 
        Show a string to the user that clearly shows any invisible characters.
    -->
    <TextBlock Grid.Row="3">Result</TextBlock>
    <TextBox Grid.Row="3" Grid.Column="1" x:Name="Result" Text="{Binding ResultText}"/>

然后在主窗口中添加DataSource

public MainWindow()
    {
        InitializeComponent();         
        DataContext = new ViewModel();
    }

最新更新