[在我的项目中,我使用Xamarin.forms和Xamarin.android。 Xamarin.forms中的所有页面都可以正常工作,但是当我单击应该指向Xamarin.forms中的活动的按钮时,它什么也没显示。我的代码如下所示,我想显示Xamarin.Forms NewViewRenderer
中的内容** Xamarin.forms:
MainPage.xaml(它具有下面的按钮,显示RegionPage.xaml)
...
<StackLayout Grid.Row="2" Grid.Column="1" Orientation="Vertical">
<Image Source="location.png"
Grid.Row="2"
Grid.Column="1"
BackgroundColor="White"
WidthRequest="60" HeightRequest="60"
>
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped_5"/>
</Image.GestureRecognizers>
</Image>
<Label Text="Regions" HorizontalTextAlignment="Center" />
</StackLayout>
....
NewView.cs
using ...
namespace miemss_xamarin
{public class NewView : View{}}
RegionPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:miemss_xamarin;assembly=miemss_xamarin"
mc:Ignorable="d"
Title="Regions"
x:Class="miemss_xamarin.RegionPage">
<ContentPage.Content>
<local:NewView />
</ContentPage.Content>
</ContentPage>
在android中,我的布局(NotificationLayout.xml)如下:
<?xml version="1.0" encoding="utf-8"?>
<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:text="Message Notifications for:"
android:id="@+id/msgText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMediumInverse"
android:padding="10dp"
android:textColor="@android:color/black"
/>
<Switch
android:id="@+id/SubscribeToRegion1"
android:text="Region 1"
android:textAppearance="?android:attr/textAppearanceMediumInverse"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> ... (and 4 other Switches)
NewViewRenderer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Firebase.Messaging;
using miemss_xamarin.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(miemss_xamarin.NewView), typeof(NewViewRenderer))]
namespace miemss_xamarin.Droid
{
public class NewViewRenderer : ViewRenderer <miemss_xamarin.NewView,miemss_xamarin.Droid.NewViewRenderer>
{
NewViewRenderer newViewRenderer;
Android.Widget.Switch btnRegion1; Android.Widget.Switch btnRegion2; Android.Widget.Switch btnRegion3; Android.Widget.Switch btnRegion4; Android.Widget.Switch btnRegion5;
public NewViewRenderer (Context context):base (context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<miemss_xamarin.NewView> e)
{
base.OnElementChanged(e);
if (Control == null)
{
// var context = new NewViewRenderer(Forms.Context);
//SetNativeControl(context);
var context = Android.App.Application.Context;
LayoutInflater minflater = context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
var view = minflater.Inflate(Resource.Layout.NotificationLayout, this, false);
var msgText = view.FindViewById<TextView>(Resource.Id.msgText);
btnRegion1 = view.FindViewById<Android.Widget.Switch>(Resource.Id.SubscribeToRegion1);
btnRegion2 = view.FindViewById<Android.Widget.Switch>(Resource.Id.SubscribeToRegion2);
btnRegion3 = view.FindViewById<Android.Widget.Switch>(Resource.Id.SubscribeToRegion3);
btnRegion4 = view.FindViewById<Android.Widget.Switch>(Resource.Id.SubscribeToRegion4);
btnRegion5 = view.FindViewById<Android.Widget.Switch>(Resource.Id.SubscribeToRegion5);
// base.SetNativeControl(view);
It always shows red underline under view, so I
}
if (e.NewElement != null)
{
btnRegion1.CheckedChange += (a, b) =>
{
bool isChecked = b.IsChecked;
if (isChecked)
FirebaseMessaging.Instance.SubscribeToTopic("R1");
else
FirebaseMessaging.Instance.UnsubscribeFromTopic("R1");
};
btnRegion2.CheckedChange += (c, d) =>
{
bool isChecked = d.IsChecked;
if (isChecked)
FirebaseMessaging.Instance.SubscribeToTopic("R2");
else
FirebaseMessaging.Instance.UnsubscribeFromTopic("R2");
};
btnRegion3.CheckedChange += (e, f) =>
{
bool isChecked = f.IsChecked;
if (isChecked)
FirebaseMessaging.Instance.SubscribeToTopic("R3");
else
FirebaseMessaging.Instance.UnsubscribeFromTopic("R3");
};
btnRegion4.CheckedChange += (g, h) =>
{
bool isChecked = h.IsChecked;
if (isChecked)
FirebaseMessaging.Instance.SubscribeToTopic("R4");
else
FirebaseMessaging.Instance.UnsubscribeFromTopic("R4");
};
btnRegion5.CheckedChange += (i, j) =>
{
bool isChecked = j.IsChecked;
if (isChecked)
FirebaseMessaging.Instance.SubscribeToTopic("R5");
else
FirebaseMessaging.Instance.UnsubscribeFromTopic("R5");
};
}
}
并且如下所示,MainActivity.cs(默认代码)也没有变化:
using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Util;
using Firebase.Iid;
using Android.Gms.Common;
using Firebase.Messaging;
namespace miemss_xamarin.Droid
{
[Activity(Label = "miemss_xamarin", Icon = "@mipmap/icon", Theme = "@style/MainTheme", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
TextView msgText;
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
我已经尝试了几种解决方案,这些解决方案涉及如何使用ViewRenderer在Xamarain.forms上显示android活动,但仍然无法完美运行。我想在Xamarin Forms中显示活动NewViewRenderer吗?现在,当我单击“区域图标”时,它显示了一个空白页面,并带有返回首页的按钮!
问题是您用于NewViewRenderer的基本类型:
public class NewViewRenderer : ViewRenderer<miemss_xamarin.NewView,miemss_xamarin.Droid.NewViewRenderer>
您希望本机控件是Android.Views.View,而不是NewViewrenderer。因此,请尝试以下方法:
public class NewViewRenderer : Xamarin.Forms.Platform.Android.ViewRenderer<NewView, Android.Views.View>
[设置NewViewRenderer
的类型时,您犯了一个错误。您应该设置ViewRenderer<NewView, Android.Views.View>
[assembly: ExportRenderer(typeof(NewView), typeof(NewViewRenderer))]
namespace CustomRenderDemo.Droid
{
public class NewViewRenderer : ViewRenderer<NewView, Android.Views.View>
{
NewViewRenderer newViewRenderer;
Android.Widget.Switch btnRegion1; Android.Widget.Switch btnRegion2; Android.Widget.Switch btnRegion3; Android.Widget.Switch btnRegion4; Android.Widget.Switch btnRegion5;
Context context;
public NewViewRenderer(Context context) : base(context)
{
this.context = context;
}
protected override void OnElementChanged(ElementChangedEventArgs<NewView> e)
{
base.OnElementChanged(e);
if (Control == null)
{
// var context = new NewViewRenderer(Forms.Context);
//SetNativeControl(context);
var context = Android.App.Application.Context;
var view = LayoutInflater.From(context).Inflate(Resource.Layout.NotificationLayout, null, false);
// LayoutInflater minflater = context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
// var view = minflater.Inflate(Resource.Layout.NotificationLayout, this, false);
var msgText = view.FindViewById<TextView>(Resource.Id.msgText);
btnRegion1 = view.FindViewById<Android.Widget.Switch>(Resource.Id.SubscribeToRegion1);
btnRegion2 = view.FindViewById<Android.Widget.Switch>(Resource.Id.SubscribeToRegion2);
btnRegion3 = view.FindViewById<Android.Widget.Switch>(Resource.Id.SubscribeToRegion3);
btnRegion4 = view.FindViewById<Android.Widget.Switch>(Resource.Id.SubscribeToRegion4);
btnRegion5 = view.FindViewById<Android.Widget.Switch>(Resource.Id.SubscribeToRegion5);
SetNativeControl(view);
// AddView(view);
// new NewView(context);
// SetNativeControl(new MyView(context));
// Control.AddView(view);
// base.SetNativeControl(view);
// It always shows red underline under view, so I
}
if (e.NewElement != null)
{
btnRegion1.CheckedChange += (a, b) =>
{
bool isChecked = b.IsChecked;
//if (isChecked)
// FirebaseMessaging.Instance.SubscribeToTopic("R1");
//else
// FirebaseMessaging.Instance.UnsubscribeFromTopic("R1");
};
btnRegion2.CheckedChange += (c, d) =>
{
bool isChecked = d.IsChecked;
//if (isChecked)
// FirebaseMessaging.Instance.SubscribeToTopic("R2");
//else
// FirebaseMessaging.Instance.UnsubscribeFromTopic("R2");
};
btnRegion3.CheckedChange += (e, f) =>
{
bool isChecked = f.IsChecked;
//if (isChecked)
// FirebaseMessaging.Instance.SubscribeToTopic("R3");
//else
// FirebaseMessaging.Instance.UnsubscribeFromTopic("R3");
};
btnRegion4.CheckedChange += (g, h) =>
{
bool isChecked = h.IsChecked;
//if (isChecked)
// FirebaseMessaging.Instance.SubscribeToTopic("R4");
//else
// FirebaseMessaging.Instance.UnsubscribeFromTopic("R4");
};
btnRegion5.CheckedChange += (i, j) =>
{
bool isChecked = j.IsChecked;
//if (isChecked)
// FirebaseMessaging.Instance.SubscribeToTopic("R5");
//else
// FirebaseMessaging.Instance.UnsubscribeFromTopic("R5");
};
}
}
}
这里正在运行sceenshot。