从Xamarin Forms导航到Xamarin Native



我的问题是我要求应用程序的第一页是Native。当用户注销时,我需要一种方法从Xamarin Forms内容页面导航到应用程序的第一个本地页面。是否存在从窗体页面启动本机ViewController(iOS(或启动活动(Android(的情况。我经常使用自定义渲染器。

如果我能以某种方式重新启动应用程序或再次调用AppDelegate之类的,那就太棒了。

感谢您的帮助。

如果您经常使用自定义渲染器,那么您可以创建这样的自定义视图渲染器。例如:

在PCL:

public class NewView : View
{
}

在Android平台中,首先在/Resources/layout下创建一个布局,如下所示:

<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>

然后创建NewView的渲染器,如下所示:

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!";
}
}

最后在ContentPage中使用此视图使其像一个页面:

<ContentPage.Content>
<local:NewView />
</ContentPage.Content>

对于iOS平台,应该有一种方法将ViewController设置为视图渲染器的本机控件。但我对iOS不熟悉,你可以试一试。

最新更新