Reputation: 1127
My problem is that I require the first pages of my app to be Native. I need a way to Navigate from a Xamarin Forms Content Page to the first native pages of the app when a user signs out. Is there anyway of from a Forms page starting a native ViewController (iOS) or starting an Activity (Android). I use custom renderers regularly.
It would be brilliant if I could somehow restart the app or call AppDelegate again or something.
Any help is appreciated.
Upvotes: 1
Views: 757
Reputation: 16652
If you use custom renderers regularly, then you can create a custom view renderer like this. For example:
In PCL:
public class NewView : View
{
}
In Android platform, create a layout under /Resources/layout first like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/tv"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="this is new view." />
<Button android:id="@+id/btn"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="change text" />
</LinearLayout>
Then create the renderer for NewView
like this:
public class NewViewRenderer : ViewRenderer
{
private TextView tv;
private Android.Widget.Button btn;
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.View> e)
{
base.OnElementChanged(e);
if (Control == null)
{
var context = Xamarin.Forms.Forms.Context;
LayoutInflater minflater = context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
var view = minflater.Inflate(Resource.Layout.newview, this, false);
tv = view.FindViewById<TextView>(Resource.Id.tv);
btn = view.FindViewById<Android.Widget.Button>(Resource.Id.btn);
SetNativeControl(view);
}
if (e.OldElement != null)
{
btn.Click -= Btn_Click;
}
if (e.NewElement != null)
{
btn.Click += Btn_Click;
}
}
private void Btn_Click(object sender, EventArgs e)
{
tv.Text = "Text changed!";
}
}
Finally use this view in ContentPage
to make it like a page:
<ContentPage.Content>
<local:NewView />
</ContentPage.Content>
For iOS platform, there should be a method to set the ViewController
as native control for a view renderer. But I'm not familiar with iOS, you may give a try.
Upvotes: 3