Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin 自定义渲染器不适用_Xamarin_Xamarin.forms_Xamarin.android_Custom Renderer - Fatal编程技术网

Xamarin 自定义渲染器不适用

Xamarin 自定义渲染器不适用,xamarin,xamarin.forms,xamarin.android,custom-renderer,Xamarin,Xamarin.forms,Xamarin.android,Custom Renderer,Xamarin.Forms.Buttons上的BackgroundColor属性开始为分配给按钮的整个区域着色,而不仅仅是按钮,因此我开始深入研究Android中的自定义渲染器,看看是否可以修复它。下面是我创建的类、使用自定义按钮的.xaml和渲染器(作为测试,它只是将按钮涂成绿色): 使用Xamarin.Forms; 命名空间wcsmobile { 公共类wcsButton:按钮 { } } [程序集:导出呈现器(typeof(wcsButton)、typeof(ButtonRenderer

Xamarin.Forms.Buttons上的BackgroundColor属性开始为分配给按钮的整个区域着色,而不仅仅是按钮,因此我开始深入研究Android中的自定义渲染器,看看是否可以修复它。下面是我创建的类、使用自定义按钮的.xaml和渲染器(作为测试,它只是将按钮涂成绿色):

使用Xamarin.Forms;
命名空间wcsmobile
{
公共类wcsButton:按钮
{
}
}


[程序集:导出呈现器(typeof(wcsButton)、typeof(ButtonRenderer))]
命名空间wcsmobile.Droid
{
类wcsButtonRenderer:ButtonRenderer
{
公共wcsButtonRenderer(上下文):基本(上下文)
{
}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(控件!=null)
{
Control.SetBackgroundColor(全局::Android.Graphics.Color.LightGreen);
}
}
}
}
没有错误,但按钮保持灰色,而不是绿色。在“base.OnElementChanged(e)”上放置断点没有任何作用;断点永远不会被命中。这两个项目都使用Xamarin.Forms版本3.1.0.583944

任何人只要知道为什么自定义渲染器似乎没有应用,都会非常有帮助。(甚至是关于为什么背景色一开始就不起作用的想法)

正如您提到的“两个项目”一样,答案似乎很明显:项目A中的渲染器不应用于项目B。您可以选择将其移动到适当的项目或将其移动到共享项目

<?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:local="clr-namespace:wcsmobile;assembly=wcsmobile"
    x:Class="wcsmobile.Transfer"
    Title="Transfer">
<ContentPage.Content>
    <StackLayout HorizontalOptions="FillAndExpand">
        <local:wcsButton Text="OK" HorizontalOptions="Center"/>
    </StackLayout>
</ContentPage.Content>
</ContentPage>

[assembly: ExportRenderer(typeof(wcsButton), typeof(ButtonRenderer))]
namespace wcsmobile.Droid
{
class wcsButtonRenderer : ButtonRenderer
{
    public wcsButtonRenderer(Context context) : base(context)
    {
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {
            Control.SetBackgroundColor(global::Android.Graphics.Color.LightGreen);
        }
    }
}
}