我知道如果我使用了一个视图控制器,我可以使用它:
var scanner = new MobileBarcodeScanner(this.NavigationController);
我如何知道我在需要使用的按钮内使用的导航?
public class BarReaderButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
// I tried this but don't worked
var scanner = new MobileBarcodeScanner(this.NavigationController);
// I tried this but do
var scanner = new MobileBarcodeScanner(this);
Element.Clicked += async(s_, e_) => {
// Setup our button
// Tell our scanner to use the default overlay
scanner.UseCustomOverlay = false;
//We can customize the top and bottom text of the default overlay
scanner.TopText = "Hold camera up to barcode to scan";
scanner.BottomText = "Barcode will automatically scan";
//Start scanning
var result = await scanner.Scan ();
HandleScanResult(result);
};
}
}
我无法在按钮渲染中使用此代码。或者之前做过这个人?
该项目是iOS和Android的共享应用程序。
答案 0 :(得分:0)
创建扫描程序的代码应保留在视图控制器/活动/页面内。您仍然可以使用自定义按钮并将所需的代码添加到Clicked事件处理程序。
var myCustomButton = new BarReaderButton();
myCustomButton.Clicked += async(s, e) => {
var scanner = new MobileBarcodeScanner();
scanner.UseCustomOverlay = false;
//Start scanning
var result = await scanner.Scan ();
//Do something with the result
};
如果这是Xamarin.Forms,您还必须在页面代码中使用特定于平台的代码,因为条形码阅读器需要Android上的上下文:
#if __IOS__
var scanner = new MobileBarcodeScanner();
#elif __ANDROID__
var scanner = new MobileBarcodeScanner(Forms.Context);
#endif