我正在尝试使用this answer在Xamarin中实现一些特定于平台的代码,但是我遇到了将特定于平台的类设置为依赖关系的问题。我收到以下编译器错误,标记了标记的assembly
字:
除了使用子句和外部别名声明之外,程序集和模块属性必须位于文件中定义的所有其他元素之前。
我有以下代码:
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);
}
}
}
我错过了什么?
答案 0 :(得分:10)
如错误所示,必须在文件中的大多数其他程序元素之前声明程序集属性。名称空间声明(namespace MyApp_Xamarin.Droid
)是这些元素之一。您必须在此之前移动该属性:
[assembly: Dependency(typeof(View.ActualPage.BarcodeScannerTest))]
namespace MyApp_Xamarin.Droid
{
public class BarcodeScannerTestClass : View.ActualPage.BarcodeScannerTest
答案 1 :(得分:0)
将特定于平台的类设置为Entry或Any Controller的依赖关系。
I get the following compiler error, underlined the assembly word of the tag:
除了使用子句和外部别名声明之外,程序集和模块属性必须位于文件中定义的所有其他元素之前。
using CustomrendersAll.customRenders;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
namespace CustomrendersAll.Droid.customRenders
{
[assembly: ExportRenderer (typeof(MyEntry), typeof(customRenderAndriod))]
public class customRenderAndriod : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.SetBackgroundColor(global::Android.Graphics.Color.LightGreen);
}
}
}
}
上面有些错误,下面的代码是正确的
using CustomrendersAll.customRenders;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
using CustomrendersAll.Droid.customRenders;
[assembly: ExportRenderer(typeof(MyEntry), typeof(customRenderAndriod))]
namespace CustomrendersAll.Droid.customRenders
{
public class customRenderAndriod : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.SetBackgroundColor(global::Android.Graphics.Color.LightGreen);
}
}
}
}
在Class之前不正确的方式第一个代码是我写的类方式汇编这个代码是
[assembly: ExportRenderer(typeof(Pclclass), typeof(customRenderAndriod))]
在名称空间第二代码添加程序集之前,此代码为
[assembly: ExportRenderer(typeof(Pclclass), typeof(customRenderAndriod))]
答案 2 :(得分:-3)
尝试使用
使用System;使用System.Reflection;使用System.Security.Permissions;