2017-08-29 75 views
0

我的問題是,我需要我的應用程序的第一頁是本機。當用戶註銷時,我需要一種從Xamarin Forms Content Page導航到應用程序的第一本地頁面的方式。無論如何,從一個表單頁面開始一個本地ViewController(iOS)或啓動一個活動(Android)。我經常使用自定義渲染器。從Xamarin窗體導航到Xamarin原生

這將是輝煌的,如果我可以以某種方式重新啓動應用程序或再次調用AppDelegate什麼的。

任何幫助表示讚賞。

回答

1

如果您定期使用自定義渲染器,則可以創建自定義視圖渲染器,如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不熟悉,你可以試試看。