Datawedge扫描应用程序Xamarin无法正常工作

时间:2016-02-08 14:46:26

标签: c# android xamarin

我正在尝试使用Xamarin和Datawedge制作应用程序,但我遇到了很多困难。

首先,我试过这样:

[Activity(Label = "Scanner.App", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Portrait)]
[IntentFilter(new string[] { "barcodescanner.RECVR" }, Categories = new[] { Intent.CategoryDefault })]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
    // This intent string contains the source of the data as a string  
    private static string SOURCE_TAG = "com.motorolasolutions.emdk.datawedge.source";
    // This intent string contains the barcode symbology as a string  
    private static string LABEL_TYPE_TAG = "com.motorolasolutions.emdk.datawedge.label_type";
    // This intent string contains the captured data as a string  
    // (in the case of MSR this data string contains a concatenation of the track data)  
    private static string DATA_STRING_TAG = "com.motorolasolutions.emdk.datawedge.data_string";
    // Intent Action for our operation
    private static string ourIntentAction = "barcodescanner.RECVR";

    // Let's define the API intent strings for the soft scan trigger
    private static String ACTION_SOFTSCANTRIGGER = "com.motorolasolutions.emdk.datawedge.api.ACTION_SOFTSCANTRIGGER";
    private static String EXTRA_PARAM = "com.motorolasolutions.emdk.datawedge.api.EXTRA_PARAMETER";
    private static String DWAPI_TOGGLE_SCANNING = "TOGGLE_SCANNING";

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        global::Xamarin.Forms.Forms.Init(this, bundle);
        LoadApplication(new App());
    }

    private void handleDecodeData(Intent i)
    {
        // check the intent action is for us  
        if (i.Action.Equals(ourIntentAction))
        {
            // define a string that will hold our output  
            String Out = "";
            // get the source of the data  
            String source = i.GetStringExtra(SOURCE_TAG);
            // save it to use later  
            if (source == null) source = "scanner";
            // get the data from the intent  
            String data = i.GetStringExtra(DATA_STRING_TAG);
            // let's define a variable for the data length  


            int data_len = 0;
            // and set it to the length of the data  
            if (data != null) data_len = data.Length;
            // check if the data has come from the barcode scanner  
            if (source.Equals("scanner"))
            {
                // check if there is anything in the data  
                if (data != null && data.Length > 0)
                {
                    // we have some data, so let's get it's symbology  
                    String sLabelType = i.GetStringExtra(LABEL_TYPE_TAG);
                    // check if the string is empty  
                    if (sLabelType != null && sLabelType.Length > 0)
                    {                        // format of the label type string is LABEL-TYPE-SYMBOLOGY  
                        // so let's skip the LABEL-TYPE- portion to get just the symbology  
                        sLabelType = sLabelType.Substring(11);
                    }
                    else
                    {
                        // the string was empty so let's set it to "Unknown"  
                        sLabelType = "Unknown";
                    }


                    // let's construct the beginning of our output string  
                    Out = "Source: Scanner, " + "Symbology: " + sLabelType + ", Length: " + data_len.ToString() + ", Data: ...\r\n";
                }
            }
            // check if the data has come from the MSR  
            if (source.Equals("msr"))
            {
                // construct the beginning of our output string  
                Out = "Source: MSR, Length: " + data_len.ToString() + ", Data: ...\r\n";
            }

            // we need to put the edit box text into a spannable string builder  
            SpannableStringBuilder stringbuilder = new SpannableStringBuilder();
            // add the output string we constructed earlier  
            stringbuilder.Append(Out);
            // now let's highlight our output string in bold type  
            //stringbuilder.SetSpan(new StyleSpan(Typeface.DefaultBold), et.Text.Length, stringbuilder.Length, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);  
            // then add the barcode or msr data, plus a new line, and add it to the string builder  
            stringbuilder.Append(data + "\r\n");
        }
    }

    protected override void OnNewIntent(Intent intent)
    {
        handleDecodeData(intent);
    }

当我扫描某些东西时,导致我遇到困难的是什么,onCreate被调用,导致我的应用程序的新实例,这是不正常的。通常应该调用OnNewIntent,但从未触发过。{/ p>

第二次尝试这个问题就像这样

[Activity(Label = "Scanner.App", Name ="com.creonis.scanner.MainActivity", Icon = "@drawable/icon", MainLauncher = true, 
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Portrait)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        ScanReceiver _broadcastReceiver = new ScanReceiver();

        global::Xamarin.Forms.Forms.Init(this, bundle);
        var my_application = new App();
        _broadcastReceiver.scanDataReceived += (s, scanData) =>
        {

        };

        // Register the broadcast receiver
        IntentFilter filter = new IntentFilter(ScanReceiver.IntentAction);
        filter.AddCategory(ScanReceiver.IntentCategory);
        this.RegisterReceiver(_broadcastReceiver, filter);

        LoadApplication(my_application);

    }

[BroadcastReceiver(Enabled = true)]
public class ScanReceiver : BroadcastReceiver
{
    // This intent string contains the source of the data as a string  
    private static string SOURCE_TAG = "com.motorolasolutions.emdk.datawedge.source";
    // This intent string contains the barcode symbology as a string  
    private static string LABEL_TYPE_TAG = "com.motorolasolutions.emdk.datawedge.label_type";
    // This intent string contains the captured data as a string  
    // (in the case of MSR this data string contains a concatenation of the track data)  
    private static string DATA_STRING_TAG = "com.motorolasolutions.emdk.datawedge.data_string";
    // Intent Action for our operation
    public static string IntentAction = "barcodescanner.RECVR";
    public static string IntentCategory = "android.intent.category.DEFAULT";

    public event EventHandler<string> scanDataReceived;

    public override void OnReceive(Context context, Intent i)
    {
        // check the intent action is for us  
        if (i.Action.Equals(IntentAction))
        {
            // define a string that will hold our output  
            String Out = "";
            // get the source of the data  
            String source = i.GetStringExtra(SOURCE_TAG);
            // save it to use later  
            if (source == null)
                source = "scanner";
            // get the data from the intent  
            String data = i.GetStringExtra(DATA_STRING_TAG);
            // let's define a variable for the data length  
            int data_len = 0;
            // and set it to the length of the data  
            if (data != null)
                data_len = data.Length;
            // check if the data has come from the barcode scanner  
            if (source.Equals("scanner"))
            {
                // check if there is anything in the data  
                if (data != null && data.Length > 0)
                {
                    // we have some data, so let's get it's symbology  
                    String sLabelType = i.GetStringExtra(LABEL_TYPE_TAG);
                    // check if the string is empty  
                    if (sLabelType != null && sLabelType.Length > 0)
                    {
                        // format of the label type string is LABEL-TYPE-SYMBOLOGY  
                        // so let's skip the LABEL-TYPE- portion to get just the symbology  
                        sLabelType = sLabelType.Substring(11);
                    }
                    else {
                        // the string was empty so let's set it to "Unknown"  
                        sLabelType = "Unknown";
                    }

                    // let's construct the beginning of our output string  
                    Out = "Scanner  " + "Symbology: " + sLabelType + ", Length: " + data_len.ToString() + ", Data: " + data.ToString() + "\r\n";
                }
            }
            // check if the data has come from the MSR  
            if (source.Equals("msr"))
            {
                // construct the beginning of our output string  
                Out = "Source: MSR, Length: " + data_len.ToString() + ", Data: " + data.ToString() + "\r\n";
            }


            if (scanDataReceived != null)
            {
                scanDataReceived(this, Out);
            }
        }
    }
}

这导致我在扫描某些东西时没有发生任何问题。

有人能指出我正确的方向,因为我不想尝试。

0 个答案:

没有答案