从Xamarin窗体导航到Xamarin原生
问题描述:
我的问题是,我需要我的应用程序的第一页是本机。当用户注销时,我需要一种从Xamarin Forms Content Page导航到应用程序的第一本地页面的方式。无论如何,从一个表单页面开始一个本地ViewController(iOS)或启动一个活动(Android)。我经常使用自定义渲染器。从Xamarin窗体导航到Xamarin原生
这将是辉煌的,如果我可以以某种方式重新启动应用程序或再次调用AppDelegate什么的。
任何帮助表示赞赏。
答
如果您定期使用自定义渲染器,则可以创建自定义视图渲染器,如this。例如:
在PCL:
public class NewView : View
{
}
在Android平台上,第一下创建/资源/布置的配置是这样的:
<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不熟悉,你可以试试看。