为什么我想扫描时不能使用导航?
Why I can't use navigation when I want scan?
我有一个问题要问任何在 Xamarin
.
中使用过 ZXING
条码扫描器的人
我有一个应用程序,其中一页包含以下代码:
newitempage.xaml
表单扫描仪
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ERP.Views.NewItemPage"
Shell.PresentationMode="ModalAnimated"
Title="New Item"
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
ios:Page.UseSafeArea="true">
<ContentPage.Content>
<StackLayout Spacing="3" Padding="15">
<Label Text="Text" FontSize="Medium" />
<Entry Text="{Binding Text, Mode=TwoWay}" FontSize="Medium" />
<Button Text="scan" Command="{Binding ScanCommand}" HorizontalOptions="FillAndExpand"></Button>
<Label Text="Description" FontSize="Medium" />
<Editor Text="{Binding Description, Mode=TwoWay}" x:Name="mycode" AutoSize="TextChanges" FontSize="Medium" Margin="0" />
<StackLayout Orientation="Horizontal">
<Button Text="Cancel" Command="{Binding CancelCommand}" HorizontalOptions="FillAndExpand"></Button>
<Button Text="Save" Command="{Binding SaveCommand}" HorizontalOptions="FillAndExpand"></Button>
</StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>
newitempage.xaml.cs
using ERP.Models;
using ERP.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using ZXing.Net.Mobile.Forms;
namespace ERP.Views
{
public partial class NewItemPage : ContentPage
{
public Item Item { get; set; }
public NewItemPage()
{
InitializeComponent();
BindingContext = new NewItemViewModel();
}
newitemviewmodel.cs
private async void OnScan()
{
var scan = new ZXingScannerPage();
await Navigation.PushAsync(scan);
scan.OnScanResult += (result) =>
{
Device.BeginInvokeOnMainThread(async () =>
{
await Navigation.PopAsync();
mycode.Text = result.Text;
});
};
}
请帮忙解决我的问题。谢谢
`await Navigation.PushAsync(scan);`
Navigation
是 Page
的 属性。您无法从 VM 访问它。在这种情况下使用的一种常见模式是访问 App
的 MainPage
属性 以使用它的 Navigation
属性
App.Current.MainPage.Navigation.PushAsync(scan);
我有一个问题要问任何在 Xamarin
.
ZXING
条码扫描器的人
我有一个应用程序,其中一页包含以下代码:
newitempage.xaml 表单扫描仪
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ERP.Views.NewItemPage"
Shell.PresentationMode="ModalAnimated"
Title="New Item"
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
ios:Page.UseSafeArea="true">
<ContentPage.Content>
<StackLayout Spacing="3" Padding="15">
<Label Text="Text" FontSize="Medium" />
<Entry Text="{Binding Text, Mode=TwoWay}" FontSize="Medium" />
<Button Text="scan" Command="{Binding ScanCommand}" HorizontalOptions="FillAndExpand"></Button>
<Label Text="Description" FontSize="Medium" />
<Editor Text="{Binding Description, Mode=TwoWay}" x:Name="mycode" AutoSize="TextChanges" FontSize="Medium" Margin="0" />
<StackLayout Orientation="Horizontal">
<Button Text="Cancel" Command="{Binding CancelCommand}" HorizontalOptions="FillAndExpand"></Button>
<Button Text="Save" Command="{Binding SaveCommand}" HorizontalOptions="FillAndExpand"></Button>
</StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>
newitempage.xaml.cs
using ERP.Models;
using ERP.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using ZXing.Net.Mobile.Forms;
namespace ERP.Views
{
public partial class NewItemPage : ContentPage
{
public Item Item { get; set; }
public NewItemPage()
{
InitializeComponent();
BindingContext = new NewItemViewModel();
}
newitemviewmodel.cs
private async void OnScan()
{
var scan = new ZXingScannerPage();
await Navigation.PushAsync(scan);
scan.OnScanResult += (result) =>
{
Device.BeginInvokeOnMainThread(async () =>
{
await Navigation.PopAsync();
mycode.Text = result.Text;
});
};
}
请帮忙解决我的问题。谢谢
`await Navigation.PushAsync(scan);`
Navigation
是 Page
的 属性。您无法从 VM 访问它。在这种情况下使用的一种常见模式是访问 App
的 MainPage
属性 以使用它的 Navigation
属性
App.Current.MainPage.Navigation.PushAsync(scan);