C# 数据楔扫描应用程序Xamarin不工作

C# 数据楔扫描应用程序Xamarin不工作,c#,android,xamarin,C#,Android,Xamarin,我正试图用Xamarin和Datawedge制作一个应用程序,但我遇到了很多困难 首先,我试着这样做: [Activity(Label = "Scanner.App", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Port

我正试图用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
,但从未触发

这个问题的第二次尝试是这样的

[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);
            }
        }
    }
}
[Activity(Label=“Scanner.App”,Name=“com.creonis.Scanner.MainActivity”,Icon=“@drawable/Icon”,MainLauncher=true,
ConfigurationChanges=ConfigChanges.ScreenSize | ConfigChanges.Orientation,ScreenOrientation=ScreenOrientation.Grait)]
公共类主活动:全局::Xamarin.Forms.Platform.Android.Forms应用活动
{
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
ScanReceiver_broadcastReceiver=新的ScanReceiver();
全局::Xamarin.Forms.Forms.Init(这个,bundle);
var my_application=new App();
_broadcastReceiver.ScandaReceived+=(s,Scanda)=>
{
};
//注册广播接收器
IntentFilter过滤器=新的IntentFilter(ScanReceiver.IntentAction);
filter.AddCategory(ScanReceiver.IntentCategory);
这个.RegisterReceiver(_broadcastReceiver,filter);
加载应用程序(我的应用程序);
}
[广播接收器(已启用=真)]
公共类ScanReceiver:BroadcastReceiver
{
//此意图字符串包含作为字符串的数据源
私有静态字符串SOURCE_TAG=“com.motorolasolutions.emdk.datawedge.SOURCE”;
//此意图字符串包含作为字符串的条形码符号
私有静态字符串LABEL\u TYPE\u TAG=“com.motorolasolutions.emdk.datawedge.LABEL\u TYPE”;
//此意图字符串包含捕获的数据作为字符串
//(对于MSR,此数据字符串包含轨迹数据的串联)
私有静态字符串数据\u string\u TAG=“com.motorolasolutions.emdk.datawedge.DATA\u string”;
//我们行动的意图
公共静态字符串IntentAction=“barcodescanner.RECVR”;
公共静态字符串IntentCategory=“android.intent.category.DEFAULT”;
已接收公共事件事件处理程序;
公共覆盖void OnReceive(上下文,意图i)
{
//检查意向行动是否针对我们
如果(i.作用等于(意图))
{
//定义一个将保存输出的字符串
串出“”;
//获取数据源
String source=i.GetStringExtra(source_标记);
//保存它以便以后使用
if(source==null)
source=“扫描器”;
//从意图中获取数据
字符串数据=i.GetStringExtra(数据字符串标记);
//让我们为数据长度定义一个变量
int data_len=0;
//并将其设置为数据的长度
如果(数据!=null)
数据长度=数据长度;
//检查数据是否来自条形码扫描仪
if(source.Equals(“扫描器”))
{
//检查数据中是否有任何内容
if(data!=null&&data.Length>0)
{
//我们有一些数据,所以让我们看看它的符号
String sLabelType=i.GetStringExtra(标签类型标签);
//检查字符串是否为空
if(sLabelType!=null&&sLabelType.Length>0)
{
//标签类型字符串的格式为label-type-SYMBOLOGY
//因此,让我们跳过标签类型部分,只获取符号
sLabelType=sLabelType.Substring(11);
}
否则{
//字符串为空,因此我们将其设置为“未知”
sLabelType=“未知”;
}
//让我们构造输出字符串的开头
Out=“Scanner”+”符号:“+sLabelType+”,长度:“+data\u len.ToString()+”,数据:“+data.ToString()+”\r\n”;
}
}
//检查数据是否来自MSR
if(来源等于(“msr”))
{
//构造输出字符串的开头
Out=“Source:MSR,长度:“+data\u len.ToString()+”,数据:“+data.ToString()+”\r\n”;
}
如果(ScandaReceived!=null)
{
斯堪的纳维亚收到(本,外);
}
}
}
}
这就导致了当我扫描某个东西时什么都没发生的问题

有人能给我指出正确的方向吗?因为我不知道该怎么做