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 ();
}
}
}
}
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"
I'm trying to display a WebView2 within a Xamarin WPF app on .NET 5.
So at startup I set the MainPage to Navigator which looks like so:
Navigator.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:XamTestNET5.Views"
x:Class="XamTestNET5.Navigator" Title="Here is a title">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackLayout Grid.Row="0" Grid.Column="0" Orientation="Vertical" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Label FontSize="Medium">Hello</Label>
<!--<local:PlaceHolderView></local:PlaceHolderView>-->
<local:CustomWebBrowserView x:Name="browser" Source="https://www.google.com"></local:CustomWebBrowserView>
</StackLayout>
<StackLayout Grid.Row="1" Grid.Column="0" Orientation="Horizontal" HorizontalOptions="FillAndExpand" BackgroundColor="{StaticResource Primary}">
<Button FontSize="Large" Text="Tab 1" BorderWidth="0" BorderColor="Transparent"></Button>
<Button FontSize="Large" Text="Tab 2" BorderWidth="0" BorderColor="Transparent"></Button>
<Button FontSize="Large" Text="Tab 3" BorderWidth="0" BorderColor="Transparent"></Button>
</StackLayout>
</Grid>
</ContentPage>
CustomWebBrowserView.xaml:
<?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="XamTestNET5.Views.CustomWebBrowserView">
<ContentView.Content>
<Grid></Grid>
</ContentView.Content>
</ContentView>
CustomWebBrowserView.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 XamTestNET5.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CustomWebBrowserView : ContentView
{
public static readonly BindableProperty SourceProperty = BindableProperty.Create(
propertyName: "Source",
returnType: typeof(string),
declaringType: typeof(string),
defaultValue: "");
public string Source
{
get { return (string)GetValue(SourceProperty); }
set { SetValue(SourceProperty, value); }
}
public CustomWebBrowserView()
{
InitializeComponent();
}
}
}
WebBrowserRenderer.cs in the WPF project:
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms.Platform.WPF;
using XamTestNET5.Views;
using XamTestNET5WPF.Renderers;
[assembly: ExportRenderer(typeof(CustomWebBrowserView), typeof(WebBrowserRenderer))]
namespace XamTestNET5WPF.Renderers
{
public class WebBrowserRenderer : ViewRenderer<CustomWebBrowserView, WebView2>
{
WebView2 webView;
CoreWebView2EnvironmentOptions options = new CoreWebView2EnvironmentOptions();
CoreWebView2Environment env;
String userDataFolder = Path.GetTempPath();
public WebBrowserRenderer() : base()
{
}
private void WebView_CoreWebView2Ready(object sender, EventArgs e)
{
this.webView.CoreWebView2.Navigate(#"https://www.bing.com");
}
protected async override void OnElementChanged(ElementChangedEventArgs<CustomWebBrowserView> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
webView = new WebView2();
env = await CoreWebView2Environment.CreateAsync("", userDataFolder, options);
webView.CoreWebView2Ready += WebView_CoreWebView2Ready;
webView.Source = new Uri(#"https://www.bing.com");
webView.Visibility = System.Windows.Visibility.Visible;
this.SetNativeControl(webView);
await webView.EnsureCoreWebView2Async(env);
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == nameof(CustomWebBrowserView.SourceProperty))
{
this.webView.Source = new Uri(Element.Source);
this.webView.CoreWebView2.Navigate(Element.Source);
}
}
}
}
I can see by debugging that I'm getting the WebView2 but it obliterates the content page leaving only the title.
If I comment out the CustomWebBrowserView and put in a PlaceHolderView stub, which just looks like this:
<?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="XamTestNET5.Views.PlaceHolderView">
<ContentView.Content>
<StackLayout>
<Label Text="Placeholder" />
</StackLayout>
</ContentView.Content>
</ContentView>
The layout at least isn't obliterated, but of course no WebView2.
I can't get Shell to work right either but that's to be expected: https://github.com/xamarin/Xamarin.Forms/issues/7377
So I thought that the workaround in the meantime might be just to create my own custom navigation within the app, but I want to provide WebView2 controls in WPF. Am I doing something wrong in the WebBrowserRenderer or Xaml?
Change CustomWebBrowserView to the following, basing off of View rather than ContentView and everything is working now with no obliteration of the view:
CustomWebBrowserView.Xaml:
<?xml version="1.0" encoding="UTF-8"?>
<View xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XamTestNET5.Views.CustomWebBrowserView">
</View>
CustomWebBrowserView.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 XamTestNET5.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CustomWebBrowserView : View
{
public static readonly BindableProperty SourceProperty = BindableProperty.Create(
propertyName: "Source",
returnType: typeof(string),
declaringType: typeof(string),
defaultValue: "");
public string Source
{
get { return (string)GetValue(SourceProperty); }
set { SetValue(SourceProperty, value); }
}
public CustomWebBrowserView()
{
InitializeComponent();
}
}
}
Navigator.xaml:
<StackLayout Grid.Row="0" Grid.Column="0" Orientation="Vertical" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Label FontSize="Medium">Hello</Label>
<!--<local:PlaceHolderView></local:PlaceHolderView>-->
<local:CustomWebBrowserView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" x:Name="browser" Source="https://www.sneauxkones.com"></local:CustomWebBrowserView>
</StackLayout>
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 .
I am receiving an error - "Carousel has no children".
I utilized the carousel sample from Xamarin. Carousel works if the pages are the same. My requirement is that my pages are different but I want it to work in a carousel.
Page 1 - Image
Page 2 - Buttons
Page 3 - Check Boxes
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<CarouselPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CarouselApp.MainPage">
<CarouselPage.ItemTemplate>
<DataTemplate>
<ContentPage>
<ContentPage.Padding>
<OnPlatform x:TypeArguments="Thickness" iOS="0,40,0,0" Android="0,40,0,0">
</ContentPage.Padding>
<StackLayout>
<ContentView BindingContext="PageName"></ContentView>
</StackLayout>
</ContentPage>
</DataTemplate>
</CarouselPage.ItemTemplate>
</CarouselPage>
MainPage.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace CarouselApp
{
public partial class MainPage : CarouselPage
{
public MainPage()
{
InitializeComponent();
ItemsSource = CarouselAppDataModel.All;
}
}
}
DataModel
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace CarouselApp
{
public class CarouselDataModel : ContentView
{
public ContentView PageName { get; set; }
public static IList<CarouselDatModel> All { get; set; }
private Carousel03 fl3 as new Carousel03;
private Carousel04 fl4 as new Carousel04;
public CarouselDataDataModel()
{
All = new ObservableCollection<CarouselDataModel>
{
new CarouselDataModel
{
PageName = fl3
},
new CarouselDataModel
{
PageName = fl4
}
};
}
}
}
Since your pages are all different, and you are using XAML... rather than use a data template just create three consecutive content pages in your XAML as below
<CarouselPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CarouselPageNavigation.MainPage">
<ContentPage>
<ContentPage.Padding>
<OnPlatform x:TypeArguments="Thickness" iOS="0,40,0,0" Android="0,40,0,0" />
</ContentPage.Padding>
<StackLayout>
<Label Text="Red" FontSize="Medium" HorizontalOptions="Center" />
<BoxView Color="Red" WidthRequest="200" HeightRequest="200" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage>
<ContentPage>
...
</ContentPage>
<ContentPage>
...
</ContentPage>
</CarouselPage>