I'm new in Xamarin. I want to add information to Book class then display information using displayalert() method and its worked. But Why got an error? Hope you guys can help me solve this problem.Thank you in advance.
NewBookPage.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="Form.NewBookPage">
<StackLayout Margin="10,10,10,0">
<Entry x:Name="nameEntry"
Placeholder="name of the book"/>
<Entry x:Name="authorEntry"
Placeholder="name of the author"/>
<Button Text="save"
Clicked="Button_Clicked" />
</StackLayout>
</ContentPage>
NewBookPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Form
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class NewBookPage : ContentPage
{
public NewBookPage ()
{
InitializeComponent ();
}
private void Button_Clicked(object sender, EventArgs e)
{
Book book = new Book()
{
Name = nameEntry.Text,
Author = authorEntry.Text
};
DisplayAlert("Success",book.Name+"/"+book.Author, "great!");
}
}
}
Error List
Image
1.Clean your project
2.Rebuild it, change your XAML file properties (Build Action) from Embedded Resource to Compile, Rebuild your project (will throw errors).
3.switch back your XAML file properties to Embedded Resource, Rebuild.
Refer to this link:
https://forums.xamarin.com/discussion/96447/the-name-initializecomponent-does-not-exist-in-the-current-context
Related
I know that this error has many answers, but they are all some system errors or smth like that. I just created this app and it worked fine. Then I added a view-model to handle my navigation between the register and login page and in login/register.xaml I updated the text to know on what page I'm. But now InitializeComponent is not recognized.
I put only Register page because login is the same but with login name:
using Xamarin.Forms;
namespace Appointments.Views
{
public partial class RegisterPage : ContentPage
{
public RegisterPage()
{
InitializeComponent();
}
}
}
And the view-model:
using Appointments.Views;
using MvvmHelpers;
using MvvmHelpers.Commands;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace Appointments.ViewModels
{
public class RegLogViewModel : BaseViewModel
{
public AsyncCommand GoToRegisterCommand;
public AsyncCommand GoToLoginCommand;
RegLogViewModel()
{
GoToLoginCommand = new AsyncCommand(GoToLogin);
GoToRegisterCommand = new AsyncCommand(GoToRegister);
}
async Task GoToRegister()
{
await Shell.Current.GoToAsync($"//{ nameof(RegisterPage)}");
}
async Task GoToLogin()
{
await Shell.Current.GoToAsync($"//{ nameof(LoginPage)}");
}
}
}
And Register.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="Appoitments.Views.RegisterPage">
<ContentPage.Content>
<StackLayout>
<Label
Text="Register Page!"
BackgroundColor="Blue"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
<Button
Text="Go To Login"
Command="{Binding GoToLoginCommand}"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
There is a typo mistake in your Register.xaml, that results in different namespace between the xaml and the cs partial definition of your Register class.
You have
x:Class="Appoitments.Views.RegisterPage"
instead of
x:Class="Appointments.Views.RegisterPage"
The user interface contain one go button only and are code in go.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
BackgroundColor="Indigo"
Padding="10,20,0,0"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ExerciseApp.Login">
<StackLayout VerticalOptions="Center" Spacing="20">
<Button Text="Go" Clicked="Entry_Completed"/>
</StackLayout>
</ContentPage>
After the Go button was clicked, facebook webpage will open and auto fill in the username and passwords and login automatically, below code are store in go.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Plugin.AutoLogin;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace ExerciseApp
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Login : ContentPage
{
void Entry_Completed(object sender, EventArgs e)
{
Device.OpenUri(new Uri("http://www.facebook.com"));
Device.FindElement(By.Id("email")).SendKeys("john#hotmail.com");
Device.FindElement(By.Id("pass")).SendKeys("passjohn");
Device.FindElement(By.Id("u_0_3")).SendKeys(Key.click);
}
public Login()
{
InitializeComponent();
}
}
}
The code are fails to run. Anyone can share me some idea?
I installed the plugin on the 3 projects (PCL/Android/IOS)
I am using Xamarin.forms and
The line that calls the popup page shows this error message after the app started:
Unhandled Exception:
System.InvalidOperationException: Platform is not created occurred
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MenuView : ContentPage
{
List<Loja> lstLojas;
IGeolocator locator;
Loja lojaPerto;
Localizacao localizacao;
MyPopupPage s;
public MenuView()
{
s = new MyPopupPage();
InitializeComponent();
CallingGeolocatorAsync();
login();
}
void login()
{
Navigation.PushPopupAsync(s, true);
}
this is my PopUp Page.cs
using System;
using System.Threading.Tasks;
using Rg.Plugins.Popup.Pages;
using Rg.Plugins.Popup.Services;
using Xamarin.Forms;
namespace neoFly_Montana.PopUp
{
public partial class MyPopupPage : PopupPage
{
public MyPopupPage()
{
InitializeComponent();
}
private void OnClose(object sender, EventArgs e)
{
PopupNavigation.PopAsync();
}
protected override Task OnAppearingAnimationEnd()
{
return Content.FadeTo(0.5);
}
protected override Task OnDisappearingAnimationBegin()
{
return Content.FadeTo(1);
}
}
}
this is my popup xml
<?xml version="1.0" encoding="utf-8" ?>
<pages:PopupPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup"
x:Class="neoFly_Montana.PopUp.MyPopupPage">
<!--Animations use example-->
<pages:PopupPage.Animation>
<animations:ScaleAnimation
PositionIn="Center"
PositionOut="Center"
ScaleIn="1.2"
ScaleOut="0.8"
DurationIn="400"
DurationOut="300"
EasingIn="SinOut"
EasingOut="SinIn"
HasBackgroundAnimation="True"/>
</pages:PopupPage.Animation>
<!-- Content -->
Can someone help?
You can't call that in a contructor...you need to use:
protected override void OnAppearing()
{
base.OnAppearing();
s = new MyPopupPage();
PopupNavigation.PushAsync(s, true);
}
Popuplogin.xaml:
<?xml version="1.0" encoding="UTF-8"?>
<pages:PopupPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SeralizationApp.Views.Popuplogin"
xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup"
xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
xmlns:myAnimations="clr-namespace:StalizationApp.Animations;assembly=MyProject">
<StackLayout Margin="12"
Padding="24"
Spacing="24"
BackgroundColor="White"
HorizontalOptions="Center"
VerticalOptions="Center">
<StackLayout>
<Label Text="Login" />
<Entry FontSize="30"
Text="Email" />
</StackLayout>
<StackLayout>
<Label Text="ContraseƱa"/>
<Entry FontSize="30"
IsPassword="True"
Text="ContraseƱa" />
</StackLayout>
//enter code here
<Button BackgroundColor="DodgerBlue"
FontSize="30"
Text="Login"
TextColor="White" />
</StackLayout>
</pages:PopupPage>
Popuplogin.cs
using Rg.Plugins.Popup.Pages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace SeralizationApp.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Popuplogin : PopupPage // ContentView
{
public Popuplogin ()
{
InitializeComponent ();
}
}
}
The code of my project:
MainPage.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace App2
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private async void Button_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new Page1());
}
}
}
MainPage.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"
xmlns:local="clr-namespace:App2"
x:Class="App2.MainPage">
<Button Text="Welcome to Xamarin Forms!"
VerticalOptions="Center"
HorizontalOptions="Center" Clicked="Button_Clicked" />
</ContentPage>
In every project i create in xamarin when i click a button when in the android emulator i receive this message :
enter image description here
An unhandled exception occured.
And when i click the button in others projects it's give me the same message, please help me !
So i found that i have to change this line in app.xaml.cs from MainPage = new exercice2_stack_layout.MainPage(); to Mainpage = new NavigationPage (new MainPage()); to make the navigation.pushasync work perfectly .
work with a web view however1 when I try to work with the web view
the page is crashed and get this error:
"An unhandled exception occured."
attaching my code:
also I want to know if I navagate with the web view to a Image
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="MashamApp.MImageEditor">
<ContentPage.Content>
<StackLayout>
<StackLayout Orientation="Horizontal" Padding="10,10">
<Button Text="Back" HorizontalOptions="StartAndExpand" Clicked="backClicked" />
<Button Text="Forward" HorizontalOptions="End" Clicked="forwardClicked" />
</StackLayout>
<WebView x:Name="Browser" WidthRequest="1000" HeightRequest="1000" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
C#:
using Syncfusion.SfImageEditor.XForms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace MashamApp
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MImageEditor : ContentPage
{
public MImageEditor()
{
InitializeComponent();
Browser.Source = "http://www.walla.co.il";
}
private void backClicked(object sender, EventArgs e)
{
// Check to see if there is anywhere to go back to
if (Browser.CanGoBack) {
Browser.GoBack ();
} else { // If not, leave the view
Navigation.PopAsync ();
}
}
private void forwardClicked(object sender, EventArgs e)
{
if (Browser.CanGoForward) {
Browser.GoForward ();
}
}
}
}