从 Xamarin Forms 导航到 Xamarin Native [英] Navigate from Xamarin Forms to Xamarin Native

查看:29
本文介绍了从 Xamarin Forms 导航到 Xamarin Native的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我要求我的应用程序的第一页是原生的.当用户退出时,我需要一种从 Xamarin 表单内容页面导航到应用程序的第一个本机页面的方法.是否有从表单页面启动本机 ViewController (iOS) 或启动 Activity (Android) 的方法.我经常使用自定义渲染器.

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.

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

It would be brilliant if I could somehow restart the app or call AppDelegate again or something.

感谢任何帮助.

推荐答案

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

If you use custom renderers regularly, then you can create a custom view renderer like this. For example:

在 PCL 中:

public class NewView : View
{
}

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

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>

然后像这样为 NewView 创建渲染器:

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

最后在 ContentPage 中使用这个视图使它像一个页面:

Finally use this view in ContentPage to make it like a page:

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

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

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.

这篇关于从 Xamarin Forms 导航到 Xamarin Native的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆