Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I put [assembly:Dependency()] above a class?

I'm trying to implement some platform-specific code in Xamarin, using this answer, but I ran into an issue with setting the platform-specific class to a Dependency. I get the following compiler error, underlined the assembly word of the tag:

Assembly and module attributes must precede all other elements defined in a file except using clauses and extern alias declarations.

I have the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Security.Permissions;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using ZXing.Net.Mobile.Forms;
using Xamarin.Forms;

namespace MyApp_Xamarin.Droid {
[assembly: Dependency(typeof(View.ActualPage.BarcodeScannerTest))]
public class BarcodeScannerTestClass : View.ActualPage.BarcodeScannerTest
{
    public async void Start(INavigation nav, Page page)
    {
        var scanPage = new ZXingScannerPage();

        scanPage.OnScanResult += (result) =>
        {
            // Stop scanning
            scanPage.IsScanning = false;

            // Pop the page and show the result
            Device.BeginInvokeOnMainThread(() =>
            {
                nav.PopAsync();
                page.DisplayAlert("Scanned Barcode", result.Text, "OK");
            });
        };

        // Navigate to our scanner page
        await nav.PushAsync(scanPage);
    }
}
}

What am I missing?

like image 486
Exec Avatar asked Jul 12 '16 07:07

Exec


Video Answer


1 Answers

As the error says the assembly attribute has to be declared before most other program elements in the file. The namespace declaration (namespace MyApp_Xamarin.Droid) is one of these elements. You have to move the attribute before that:

[assembly: Dependency(typeof(View.ActualPage.BarcodeScannerTest))]

namespace MyApp_Xamarin.Droid 
{
    public class BarcodeScannerTestClass : View.ActualPage.BarcodeScannerTest
like image 171
Mike Zboray Avatar answered Sep 18 '22 16:09

Mike Zboray