unhandled exception when using Navigation.PushAsync - c#

Some of the pages in my application -like the FillForm page- requires registration at first. to ensure that the user can't open those pages without being registered I did the following
Made a form for registration in a page with class name Registration
Created a new class called ContentPageRequiresRegistration
public class ContentPageRequiresRegistration : ContentPage
{
private static enumUserType userType;
public ContentPageRequiresRegistration():base()
{
if(userType==enumUserType.unRegistered)
Task.Run(() => this.LoadRegitrationPage()).Wait();
}
private async void LoadRegitrationPage()
{
await Navigation.PushAsync(new Registration(false));
}
}
Extended pages that requires registration from ContentPageRequiresRegistration
public partial class FillForm:ContentPageRequiresRegistration { ... }
And in App.cs file
public App(){
MainPage = new NavigationPage(new FillForm(1));
}
FillForm xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPageRequiresRegistration xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="HuraApp.Pages.FillForm"
Title="Fill forms">
</ContentPageRequiresRegistration>
But when I run the application it gives me an unhandled exception, and after some debugging it seems to be caused by the navigation statement
await Navigation.PushAsync(new Registration(false));
Why is this problem happening, and how can I solve it?

Related

Why ZXing.Net.Maui scanner doesn't work if the app doesn't use Shell?

I'm trying to use ZXing.Net.Maui scanner in an empty .Net Maui app.
I seems like it doesn't work if the app doesn't use shell.
Here is my app class code:
The scanner works if the app is initialized using this code:
namespace MauiApp1;
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new AppShell();
}
}
However if the app is initialized using this code, it doesn't work. The camera view is just black and doesn't shod the camera feed.
namespace MauiApp1;
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new MainPage());
}
}
In both cases I'm creating the scanner by following the different steps in the documentation:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:zxing="clr-namespace:ZXing.Net.Maui.Controls;assembly=ZXing.Net.MAUI"
x:Class="MauiApp1.MainPage">
<ScrollView>
<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
<zxing:CameraBarcodeReaderView x:Name="cameraBarcodeReaderView" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>
C#
using ZXing.Net.Maui;
namespace MauiApp1;
public partial class MainPage : ContentPage
{
int count = 0;
public MainPage()
{
InitializeComponent();
cameraBarcodeReaderView.Options = new BarcodeReaderOptions
{
Formats = BarcodeFormats.OneDimensional,
AutoRotate = true,
Multiple = true
};
}
}
Does anyone know what am I doing wrong please ? My app doesn't use Shell. So I'm unable to use the scanner.
I can replicate the issue you descripted above and I notice that there is an open issue you raised about this problem: https://github.com/Redth/ZXing.Net.Maui/issues/109.
As an alternative workaround, if you don't want the app use shell when initializing the app, you can use the code below:
namespace MauiApp1;
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new MainPage();
}
}

Xamarin.Forms: App does not show elements on the page

I am currently learning to use Xamarin.Forms and C# (first week into it) and am trying to create a basic login app.
I started with a blank template. Now, when I run my app, only the basic screen pops up, but not the elements in the page. What is wrong?
App.xaml.cs
using Xamarin.Forms;
namespace XamarinApp
{
public partial class App : Application
{
public App()
{
InitializeComponent();
new LoginPage();
}
protected override void OnStart()
{
}
protected override void OnSleep()
{
}
protected override void OnResume()
{
}
}
}
LoginPage.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="XamarinApp.LoginPage">
<ContentPage.Content>
<StackLayout>
<Label
Text="Welcome"
/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
LoginPage.xaml.cs
using Xamarin.Forms;
namespace XamarinApp
{
public partial class LoginPage : ContentPage
{
public LoginPage()
{
InitializeComponent();
}
}
}
TL-DR
I believe that to solve your problem you will need to change your line:
new LoginPage();
to:
MainPage = new LoginPage();
My reasoning
You need to specify what the MainPage is. This part can be seen as confusing when you first start out because the example page that is supplied is also called MainPage but one is a class/Page implementation and the other key part is a property provided by the Application class that your App class inherits from. So that the app when running knows it's starting Page.
Typically when you create a new Blank Xamarin.Forms app you will see the following code in App.xaml.cs
public App()
{
InitializeComponent();
MainPage = new MainPage();
}
I suspect that your changes to add in your LoginPage somehow ended up removing the key part: MainPage = from that line.

How to deal with System.TypeLoadException in the app.xaml.cs in xamarin.forms?

Sorry to bother you, but I've come across a error in my code I don't how to deal with.
Whenever I try to run my app, I get faced with this:
System.TypeLoadException: 'Could not resolve type with token 01000019
from typeref (expected class
'Xamarin.Forms.Xaml.Diagnostics.VisualDiagnostics' in assembly
'Xamarin.Forms.Core, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=null')'
It states it occurs when the InitializeComponent(); is called in the constructor of App.xaml.cs.
Constructor in question:
public App()
{
//Line throwing the error
InitializeComponent();
MainPage = new NavigationPage(new Login.LogonFinal()); //Defines what page the app opens on when starting
}
App.xaml.cs
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace NEA_I_MDL
{
public partial class App : Application
{
static Databases.AccountDatabaseController AccountDatabaseVar;
public App()
{
//Line throwing the error
InitializeComponent();
MainPage = new NavigationPage(new Login.LogonFinal()); //Defines what page the app opens on when starting
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
public static Databases.AccountDatabaseController AccountDatabaseFunc
{
get
{
if(AccountDatabaseVar == null)
{
AccountDatabaseVar = new Databases.AccountDatabaseController();
}
return AccountDatabaseVar;
}
}
}
}
Thank you for reading, any tips/assistance will be a huge help for ineptly written code.
Can I ask that you please
Make sure you dont have different versions of the same Nugets in your solution.
Clean & Rebuild your Project
If That doesn't work for you try delete all your obj and bin folders and rebuild.
This usually happens because of Updates or version conflicts In my case atleast.
And do you mind showing us the LoginFinal Method?
I think you can just call Login
MainPage = new NavigationPage(new Login);

Different Navigation Types Between Pages

I am trying to make an app with a Main Page that is a plain Content Page (i.e. no toolbar) which has a button that leads into a Navigation Page (with toolbar). I'm struggling to find any resources to help me out in this specific case.
I have tried the following. In my App.xaml:
public partial class App : Application
{
public App ()
{
InitializeComponent();
MainPage = new App5.MainPage();
Pallets = new NavigationPage(new Pallets()); //Error: 'Pallets' is a type but is used like a variable
}
...
}
Above is the error I get on Pallets. I could change Pallets for MainPage and write the whole app with Navigation pages, which I will do for now, but I would prefer not to.
In my MainPage.xaml.cs, I'm trying to switch the view when the button is clicked:
public partial class MainPage : ContentPage
{
...
async void Button_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new Pallets());
}
}
I have seen examples such as this that include Navigation Page to Navigation Page buttons, but not plain Content Page to Navigation Page buttons.

Xamarin Cannot Implicityconvert type to xamarin.Forms.Page

I've just start new Xamarin.Forms project and I don't know what I'm doing wrong.
In my App.cs file I'm trying to set my TestPage(I've created TestPage by Add->New Item-> BlankPage) as Main Page but i'm geting this error "Severity Code Description Project File Line Suppression State
Error CS0029 Cannot implicitly convert type 'Paternity_Test.TestPage' to 'Xamarin.Forms.Page'"
My TesPage.xaml.cs
namespace Paternity_Test
{
public partial class TestPage : Xamarin.Forms.Page
{
public TestPage()
{
this.InitializeComponent();
}
}
}
And My App.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
namespace Paternity_Test
{
public class App : Application
{
public App ()
{
MainPage = new TestPage(); //here Im geting this error
}
protected override void OnStart ()
{
// Handle when your app starts
}
protected override void OnSleep ()
{
// Handle when your app sleeps
}
protected override void OnResume ()
{
// Handle when your app resumes
}
}
}
What I'm doing wrong?
<Page x:Class="Paternity_Test.TestPage" xmlns="schemas.microsoft.com/winfx/2006/xaml/presentation"; xmlns:x="schemas.microsoft.com/winfx/2006/xaml"; xmlns:local="using:Paternity_Test" xmlns:d="schemas.microsoft.com/expression/blend/2008"; xmlns:mc="schemas.openxmlformats.org/markup-compatibility/2006"; mc:Ignorable="d">
You can not use those Microsoft XAML namespaces, they need to be Xamarin/ WinFx based:
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
FYI: You can not currently use Blend to create these XAML files
ContentView Example:
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="TableHeaderBug.MyView">
<ContentView.Content>
</ContentView.Content>
</ContentView>
ContentPage Example:
<?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="TableHeaderBug.MyPage99">
<ContentPage.Content>
</ContentPage.Content>
</ContentPage>
Xamarin.Forms XAML Basics
Part 1. Getting Started with XAML
The first XML namespace declaration means that tags defined within the XAML file with no prefix refer to classes in Xamarin.Forms, for example ContentPage. The second namespace declaration defines a prefix of x. This is used for several elements and attributes that are intrinsic to XAML itself and which (in theory) are supported by all implementations of XAML. However, these elements and attributes are slightly different depending on the year embedded in the URI. Xamarin.Forms supports the 2009 XAML specification, but not all of it.
Device.OnPlatform has been deprecated and the suggestion is to use switch(Device.RuntimePlatform)
Below is an example using the switch:
switch(Device.RuntimePlatform)
{
case Device.iOS:
Padding = new Thickness(0, 20, 0, 0);
break;
case Device.Android:
Padding = new Thickness(10, 20, 0, 0);
break;
case Device.WinPhone:
Padding = new Thickness(30, 20, 0, 0);
break;
}

Categories

Resources