【发布时间】:2019-11-30 09:55:23
【问题描述】:
将 Xamarin Forms 3.6 与 Prism、Fody 和 ZXing 结合使用并运行 UWP 应用程序。
MainPage/Navigation Page/ViewA/ViewB/ViewC?selectedTab=Tab3. 的导航堆栈
Tab3 上有一个 ZXingScannerView 控件。 如果我尝试将按钮返回到 ViewB,则应用程序崩溃并显示错误“在意外时间调用了一个方法。” 异常表明它正在尝试等待 ZXing StopScanningAsync() 方法。
页面顺序是这样的:
MainPage,它是最初显示视图 A 的主详细信息。
视图 A -> 视图 B -> 视图 C,这是一个选项卡式页面
选项卡上有 4 个定义为内容页面的视图,所有这些视图都可以通过最初显示的 Tab1 进行导航。
Tab1是一个简单的数据输入表单
Tab2 有一个输入字段、一个按钮和一个 CustomControl 实例
Tab3 有一个按钮、一个 ZXingScannerView 控件和一个 CustomControl 实例
Tab4 有一个按钮、一个 Image 和一个 CustomControl 实例
所以当用户到达 ViewC 时,导航 uri 应该是 MainPage/Navigation Page/ViewA/ViewB/ViewC?selectedTab=Tab1
问题在于后退按钮操作。
使用 Prism Navigation 我可以捕获 OnNavigatedFrom、OnNavigatingTo、OnNavigatedTo。
在选项卡之间移动时不会发生这些事件。
Tab3 上的 ZXingScannerView 存在问题。
我可以单击每个选项卡并前进和后退以显示每个页面。
只要我不使用 ZXing 视图显示 Tab3,后退按钮(返回 ViewB)就可以工作。必须通过单击按钮来启动扫描,以便在加载页面时控件具有 IsScanning=false。
如果我切换到 Tab3(以及之前或之后的任何其他选项卡),然后尝试将按钮返回到 ViewB,则应用程序崩溃并出现错误“在意外时间调用了方法”。 异常表明它正在尝试等待 ZXing StopScanningAsync() 方法。 标记是这样的:
<Grid
HorizontalOptions="FillAndExpand"
IsVisible="{Binding IsScanning}"
VerticalOptions="FillAndExpand">
<zxing:ZXingScannerView
x:Name="zxing"
IsAnalyzing="{Binding IsAnalyzing}"
IsScanning="{Binding IsScanning}"
ScanResultCommand="{Binding OnScanResult}" />
<zxing:ZXingDefaultOverlay
BottomText="Place the red line over the barcode you'd like to scan."
HeightRequest="20"
TopText="Accepts EAN-8 or EAN-13 barcodes" />
</Grid>
而视图模型代码是这样的:
public Tab3PageViewModel(INavigationService navigationService,
IEventAggregator eventAgregatorService,
IPageDialogService dialogService
)
: base(navigationService, eventAgregatorService)
{
_DialogService = dialogService;
this.IsScanning = false;
this.IsAnalyzing = false;
}
public void Handle_BeginScan()
{
IsScanning = true;
IsAnalyzing = true;
}
public void Handle_OnScanResult(Result result)
{
MainThread.BeginInvokeOnMainThread(async () =>
{
if (result != null)
{
var payload = new PublishedEventPayload()
{
Code = result.Text,
ViewName = this.ViewName
};
this.AgregatorService.GetEvent<FindByBarcodeEvent>().Publish(payload);
}
else
{
await _DialogService.DisplayAlertAsync("Not found", "Unfortunately the ISBN code could not be resolved. Please try again.", "OK");
}
});
}
在 Tab3 上,我放置了一个 OnNavigatedFrom 事件以确保 IsScanning=false,但这并没有什么区别。 该应用程序会在它到达之前崩溃。
Prism Navigation Service 表示它会在调用 NavigateAsync() 时将新视图添加到堆栈中。 从 ViewB 到 ViewC 时,我尝试显式设置完整的导航路径,但对问题没有影响。查看时,导航堆栈似乎是正确的。 这似乎是一个导航问题,但我无法弄清楚为什么 ZXing 控件会干扰。
大家有什么建议吗?
【问题讨论】:
标签: xamarin xamarin.forms prism zxing