我尝试使用NuGet FAB.Forms
包中的xamarin.forms中的浮动操作按钮(github)。我尝试将我的代码设置为Sample
文件夹
Xamarin.Droid MainActivity.cs
档案
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
FAB.Droid.FloatingActionButtonRenderer.InitControl();
LoadApplication(new App());
}
Xamarin.ios AppDelegate.cs
档案
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
FAB.iOS.FloatingActionButtonRenderer.InitControl();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
Xamarin.portable App.cs
档案
public App(){MainPage = new LatestNews();}
LatestNews.xaml file
<?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:fab="clr-namespace:FAB.Forms;assembly=FAB.Forms"
x:Class="HuraApp.Pages.LatestNews">
...
<fab:FloatingActionButton
x:Name="fabBtn"
Source="plus.png"
Size="Normal"
Clicked="Handle_FabClicked"
NormalColor="Green"
RippleColor="Red" />
...
</ContentPage>
LatestNews.xaml.cs文件
void Handle_FabClicked(object sender, System.EventArgs e)
{
this.DisplayAlert("Floating Action Button", "You clicked the FAB!", "Awesome!");
}
portable packages.config文件
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="FAB.Forms" version="2.1.1" targetFramework="portable45-net45+win8+wpa81" />
<package id="Microsoft.Bcl" version="1.1.10" targetFramework="portable45-net45+win8+wpa81" />
<package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="portable45-net45+win8+wpa81" />
<package id="Microsoft.Net.Http" version="2.2.29" targetFramework="portable45-net45+win8+wpa81" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="portable45-net45+win8+wpa81" />
<package id="sameerIOTApps.Plugin.SecureStorage" version="1.2.1" targetFramework="portable45-net45+win8+wpa81" />
<package id="Xam.Plugin.Media" version="2.3.0" targetFramework="portable45-net45+win8+wpa81" />
<package id="Xamarin.Forms" version="2.3.2.127" targetFramework="portable45-net45+win8+wpa81" />
<package id="Xamarin.Forms.Maps" version="2.3.2.127" targetFramework="portable45-net45+win8+wpa81" />
</packages>
droid package.config文件
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="FAB.Forms" version="2.1.1" targetFramework="monoandroid60" />
<package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="monoandroid60" />
<package id="Xamarin.Android.Support.Animated.Vector.Drawable" version="23.3.0" targetFramework="monoandroid60" />
<package id="Xamarin.Android.Support.Design" version="23.3.0" targetFramework="monoandroid60" />
<package id="Xamarin.Android.Support.v4" version="23.3.0" targetFramework="monoandroid60" />
<package id="Xamarin.Android.Support.v7.AppCompat" version="23.3.0" targetFramework="monoandroid60" />
<package id="Xamarin.Android.Support.v7.CardView" version="23.3.0" targetFramework="monoandroid60" />
<package id="Xamarin.Android.Support.v7.MediaRouter" version="23.3.0" targetFramework="monoandroid60" />
<package id="Xamarin.Android.Support.v7.RecyclerView" version="23.3.0" targetFramework="monoandroid60" />
<package id="Xamarin.Android.Support.Vector.Drawable" version="23.3.0" targetFramework="monoandroid60" />
<package id="Xamarin.Forms" version="2.3.2.127" targetFramework="monoandroid60" />
<package id="Xamarin.Forms.Maps" version="2.3.2.127" targetFramework="monoandroid60" />
<package id="Xamarin.GooglePlayServices.Base" version="29.0.0.1" targetFramework="monoandroid60" />
<package id="Xamarin.GooglePlayServices.Basement" version="29.0.0.1" targetFramework="monoandroid60" />
<package id="Xamarin.GooglePlayServices.Maps" version="29.0.0.1" targetFramework="monoandroid60" />
</packages>
iOS package.config文件
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="FAB.Forms" version="2.1.1" targetFramework="xamarinios10" />
<package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="xamarinios10" />
<package id="Xamarin.Forms" version="2.3.2.127" targetFramework="xamarinios10" />
<package id="Xamarin.Forms.Maps" version="2.3.2.127" targetFramework="xamarinios10" />
</packages>
但是当我在android模拟器上运行应用程序时,它会给我错误消息
`Android.Views.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class android.support.design.widget.FloatingActionButton`
为什么会这样?我错过了什么?我该如何解决这个问题?
答案 0 :(得分:1)
我有类似的问题,但我使用的是Android。只需将项目父主题更改为任何Theme.AppCompat~,这解决了我的问题,也许你也是。并将global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
添加到您的MainActivity.cs
。
[Activity(Label = "YourName", , Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
FAB.Droid.FloatingActionButtonRenderer.InitControl();
LoadApplication(new App());
}
}
您可以在style.xml
<style
name="MyTheme"
parent="Base.AppTheme">
</style>
<style name="Base.AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>