Hello,
This is because the page in the Shell is navigated through the singleton mode, and when returning to the BarcodePage, the Shell will only reuse it without rebuilding it. This leads to the problem of no content in the CameraView.
Workaround: By executing the page's rebuild method in the navigation method.
In shell:
<ShellContent Route="camerapage" ContentTemplate="{DataTemplate local:MainPage}" />
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
}
protected override void OnNavigated(ShellNavigatedEventArgs args)
{
string local = args.Current.Location.ToString();
if (local.Contains("camerapage"))
{
MainPage.RecreateMainPage(); // recreate the page
}
base.OnNavigated(args);
}
}
In mainpage:
public partial class MainPage : ContentPage
{
int count = 0;
public static MainPage Instance{ get; private set; }
public MainPage()
{
InitializeComponent();
BindingContext = new BarcodeViewModel();
Instance = this;
}
public static void RecreateMainPage()
{
MainPage.Instance.Content = null;
VerticalStackLayout views = new VerticalStackLayout();
var CameraBarcodeReaderView = new CameraBarcodeReaderView();
CameraBarcodeReaderView.Options = new BarcodeReaderOptions()
{
Formats = ZXing.Net.Maui.BarcodeFormat.Code128,
AutoRotate = true,
Multiple = false,
TryHarder = true,
};
views.Add(CameraBarcodeReaderView);
MainPage.Instance.Content = views;
}
}
Best Regards,
Alec Liu.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.