Android. Navigate from custom view renderer to another Page in xamarin forms project

Jakob Aagesen 1 Reputation point
2022-12-28T20:56:10.103+00:00

hi
i have made a custom view renderer for my Android project. It almost works. I have a problem navigating away from it.
I have a button in my custom view that should navigate to another page located in the xamarin forms project.

Using events, I can call methods in the xamarin form page, that hosts the custom view, from the custom vew renderer.
I have then tried, from there to call:

await Application.Current.MainPage.Navigation.PushAsync(new NewPage());   

to navigate to the new page, but i get the following error:

Java.Lang.IllegalArgumentException  
  Message=No view found for id 0x1 (unknown) for fragment FragmentContainer{e25dddc} (64107200-e440-448f-a9b5-9de8079f1cdf id=0x1)  

I don't know what it means and how to fix it. There really isn't much information to find anywhere, so i'm hoping that someone here can help me out.

in OnElementChanged i save the xamarin Form view:

videoRoom = e.NewElement as VideoRoomView;  

and from a button in the custom view:

var closeButton = this.Context.GetActivity().FindViewById<Android.Widget.Button>(Resource.Id.close_button);  
closeButton.Click += CloseButton;  

I call the xamarin form CloseRoom method:

private void CloseButton(object sender, EventArgs e)  
{  
    videoRoom.CloseRoom();  
}  

That will call a method in the xamarin Form view:

public void CloseRoom()  
{  
    CloseEvent?.Invoke(this, null);  
}  

The event is handled by the Xamarin Page that is hosting the custom view:

private void VideoRoomView_CloseEvent(object sender, EventArgs e)  
{  
    Close();  
}  
private async void Close()  
{  
    await Application.Current.MainPage.Navigation.PushAsync(new NewPage());  
}  

Please let me know, if any of this is unclear.

Developer technologies .NET Xamarin
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.