【问题标题】:Navigate from Xamarin Forms to Xamarin Native从 Xamarin 表单导航到 Xamarin Native
【发布时间】:2018-02-07 04:17:13
【问题描述】:

我的问题是我要求我的应用程序的第一页是本机的。当用户注销时,我需要一种从 Xamarin 表单内容页面导航到应用程序的第一个本机页面的方法。无论如何从表单页面启动本机 ViewController (iOS) 或启动 Activity (Android)。我经常使用自定义渲染器。

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

感谢任何帮助。

【问题讨论】:

    标签: ios xamarin xamarin.ios xamarin.android


    【解决方案1】:

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

    在 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不熟悉,你可以试试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-30
      • 2016-09-07
      • 1970-01-01
      • 2021-01-09
      相关资源
      最近更新 更多