【问题标题】:Material Theme in Xamarin.Form elementXamarin.Form 元素中的材质主题
【发布时间】:2016-04-28 18:04:25
【问题描述】:

在我的应用程序中,使用 steps here 创建的 AppCompat 主题运行良好。我想在 Xamarin.Forms 按钮中应用 RaisedButton 颜色。为此,我尝试了以下方法:

  • 在MyApplication.Droid > Resources > Values> style.xml refering 中添加样式为:

    <style name="MyRaisedButton" parent="Widget.AppCompat.Button.Colored"></style>

  • 在视图 MyView.xaml 中的 MyApplication.Shared Portable 表单库中添加了一个按钮:

    <Button Style="{StaticResource MyRaisedButton}" Text="Raised Button" />

但它不起作用。由于这两个文件(MyView.xaml 和 style.xml)位于不同的项目中。如何在 Xamarin.Forms 中添加 Widget.AppCompat.Button.Colored 样式的按钮?

【问题讨论】:

    标签: android xamarin xamarin.android android-appcompat


    【解决方案1】:

    您可以通过将自定义 ButtonRenderer 添加到您的 Xamarin.Android 项目中来做到这一点:

    new Button {
        WidthRequest = 50,
        Text = "Button"
    },
    new RaisedButton {
        WidthRequest = 50,
        Text = "RaisedButton"
    }
    

    layout/raisedbutton.axml(根据自己的喜好定制)

    注意:如果需要,将其链接到 style id 并编辑您的 style.xml

    <?xml version="1.0" encoding="utf-8"?>
    <Button xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/raisedbutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="StackOverflow"
        android:textAllCaps="false"
        style="@style/Base.Widget.AppCompat.Button.Colored"
        android:gravity="center" />
    

    RaisedButton.cs(在 PCL 项目中):


    using Xamarin.Forms;
    
    namespace MaterialDesignButton
    {
        public class RaisedButton : Button
        {
        }
    }
    

    RaisedButtonRender.cs(在 Android 项目中)


    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    using MaterialDesignButton;
    using MaterialDesignButton.Droid;
    
    [assembly: ExportRenderer(typeof(RaisedButton), typeof(RaisedButtonRender))]
    namespace MaterialDesignButton.Droid
    {
        public class RaisedButtonRender : ButtonRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
            {
                if (Control == null)
                {
                    var raisedButton = (Android.Widget.Button)Inflate(Context, Resource.Layout.raisedbutton, null);
    
                    //Additional code: To make it generic               
                    if (e.NewElement != null)
                    {
                        raisedButton.Text = e.NewElement.Text;
                        raisedButton.Click += (s, ar) =>
                        {
                            if (e.NewElement.Command.CanExecute(null))
                            {
                                e.NewElement.Command.Execute(null);
                            }
                        };
                    }
    
                    SetNativeControl(raisedButton);
                }
                base.OnElementChanged(e);
            }
        }
    }
    

    参考:https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/

    【讨论】:

    • 这将用 RaisedButton 替换应用程序中的所有按钮,我需要使用这种样式特定一次。
    • @Vishnu 不,它不会。您仅为您的 Xamarin.Form Button 子类注册 CustomButtonRenderer
    • 我尝试了这个解决方案,创建class RaisedButton:Xamarin.Forms.Button,然后使用以下代码在 .Droid 项目中添加自定义渲染器。以下行有运行时错误,我可能缺少一些参考:button.SetBackgroundResource(Resource.Style.Widget_AppCompat_Button_Colored); 发生异常:Exception: android.content.res.Resources$NotFoundException
    • @Vishnu 我删除了我正在使用的版本并更新了答案
    • @Vishnu :-) 好的,对于android.support.v7.appcompat.R.style 的 ID 主列表,我使用 developer.android.com/reference/android/support/v7/appcompat/… 很容易从 google 的常量中删除“_”以获得 C# 样式名称。 . 这就是你要找的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多