c# - 为什么我不能把 [assembly :Dependency()] above a class?

标签 c# xamarin.forms

我正在尝试使用 this answer 在 Xamarin 中实现一些特定于平台的代码,但我遇到了将特定于平台的类设置为依赖项的问题。我收到以下编译器错误,在标记的 assembly 字下划线:

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

我有以下代码:

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);
    }
}
}

我错过了什么?

最佳答案

正如错误所说,程序集属性必须在文件中的大多数其他程序元素之前声明。命名空间声明 (namespace MyApp_Xamarin.Droid) 是这些元素之一。您必须在此之前移动属性:

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

namespace MyApp_Xamarin.Droid 
{
    public class BarcodeScannerTestClass : View.ActualPage.BarcodeScannerTest

关于c# - 为什么我不能把 [assembly :Dependency()] above a class?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38322800/

相关文章:

android - Xamarin.Forms:从 Iconize 创建一个圆形/圆形按钮

c# - 在 SSIS 脚本任务中使用 c# 批量插入到 .CSV 目标文件

c# - 如何在 Xamarin.Forms 中单击按钮时从 map 中删除多段线

xaml - SVG 作为 Xamarin.Forms 按钮图像

c# - 如何在 Xamarin 表格 pcl 的网格中插入背景图像

c - 如何将 C 项目合并到 Xamarin Forms 中?

c# - 如何在 C# 中从低级类属性获取同一类中某些顶级属性的引用?

c# - SemaphoreSlim 不会对并发请求进行排队,并且仍然允许双重预订

c# - 可以使用 webkitdotnet 在 C# 中通过 WebKit 浏览器下载文件吗?

c# - 有没有简单的方法在 C# 中创建序数?