Xamarin.forms 带有Xamarin表单的UWP自定义渲染器

Xamarin.forms 带有Xamarin表单的UWP自定义渲染器,xamarin.forms,xamarin.uwp,Xamarin.forms,Xamarin.uwp,我的自定义渲染器有以下代码。使用的元素是一个标签,我试图设置一个圆形边缘的背景色 [程序集:ExportRenderer(typeof(RoundedLabel)、typeof(RoundedLabelCustomRenderer))] 命名空间MyNamespace.UWP.CustomRenders { 公共类RoundedLabelCustomRenderer:LabelRenderer { 受保护的覆盖无效OnElementChanged(ElementChangedEventArgs

我的自定义渲染器有以下代码。使用的元素是一个标签,我试图设置一个圆形边缘的背景色

[程序集:ExportRenderer(typeof(RoundedLabel)、typeof(RoundedLabelCustomRenderer))]
命名空间MyNamespace.UWP.CustomRenders
{
公共类RoundedLabelCustomRenderer:LabelRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(例如NewElement!=null)
{
var view=(RoundedLabel)e.NewElement;
儿童。清除();
var border=新边框
{
拐角半径=新拐角半径(视图:圆角拐角半径),
背景=新的SolidColorBrush(view.RoundedBackgroundColor.ToWindows()),
孩子=控制
};
Control.Padding=new Windows.UI.Xaml.Thickness(
view.InsidePadding.Left,
view.InsidePadding.Top,
view.InsidePadding.Right,
视图。内部添加。底部);
Control.Foreground=newsolidColorBrush(view.TextColor.ToWindows());
添加(边框);
}
}
}
}
对于按钮(UWP中的一个复合对象)之类的东西,这很好,如果它是“纯”XAML,那么


我会做的

我只是在玩游戏,试图把这两个片段协调起来


如果有人能指出我做错了什么,我们将不胜感激。

使用定制的
LabelRenderer
很难实现您的要求。因为没有这样的界面来修改背景颜色和半径。但是,您可以通过自定义
视图
执行此操作。然后在UWP客户端项目中,您可以使用
UserControl
来呈现您想要的控件

CustomNewLabelControl.cs

公共类CustomNewLabelControl:视图
{
公共静态只读BindableProperty LabelTextProperty=BindableProperty.Create(
propertyName:“LabelText”,
eturnType:typeof(字符串),
declaringType:typeof(CustomNewLabelControl),
defaultValue:默认值(字符串));
公共字符串标签文本
{
获取{return(string)GetValue(LabelTextProperty);}
set{SetValue(LabelTextProperty,value);}
}
公共静态只读BindableProperty LabelRadiusProperty=BindableProperty.Create(
propertyName:“LabelRadius”,
eturnType:typeof(双),
declaringType:typeof(CustomNewLabelControl),
defaultValue:默认值(双精度);
公共双标签半径
{
获取{return(double)GetValue(LabelRadiusProperty);}
set{SetValue(LabelRadiusProperty,value);}
}
公共静态只读BindableProperty LabelBackgroundProperty=BindableProperty.Create(
propertyName:“LabelBackground”,
eturnType:typeof(颜色),
declaringType:typeof(CustomNewLabelControl),
defaultValue:默认值(颜色));
公共彩色标签背景
{
获取{return(Color)GetValue(LabelBackgroundProperty);}
set{SetValue(LabelBackgroundProperty,value);}
}
}
NewLabelControl.xaml.cs

公共密封部分类NewLabelControl:UserControl
{
公共NewLabelControl()
{
this.InitializeComponent();
this.DataContext=this;
}
公共字符串文本
{
获取{return(string)GetValue(TextProperty);}
set{SetValue(TextProperty,value);}
}
公共静态只读DependencyProperty TextProperty=
Register(“Text”、typeof(string)、typeof(NewLabelControl)、newpropertyMetadata(0));
公共SolidColorBrush LabelBackground
{
获取{return(SolidColorBrush)GetValue(LabelBackgroundProperty);}
set{SetValue(LabelBackgroundProperty,value);}
}
公共静态只读从属属性LabelBackgroundProperty=
DependencyProperty.Register(“LabelBackground”、typeof(SolidColorBrush)、typeof(NewLabelControl)、NewPropertyMetadata(0));
公共角半径LabelRadius
{
获取{return(CornerRadius)GetValue(LabelRadiusProperty);}
set{SetValue(LabelRadiusProperty,value);}
}
公共静态只读从属属性LabelRadiusProperty=
DependencyProperty.Register(“LabelRadius”、typeof(CornerRadius)、typeof(NewLabelControl)、NewPropertyMetadata(0));
公共SolidColorBrush实验室地面
{
获取{return(SolidColorBrush)GetValue(LabelForegroundProperty);}
set{SetValue(LabelForegroundProperty,value);}
}
公共静态只读从属属性LabelForegroundProperty=
DependencyProperty.Register(“LabelForeground”、typeof(SolidColorBrush)、typeof(NewLabelControl)、NewPropertyMetadata(0));
}
NewLabelControl.xaml


CustomNewLabelRanderer.cs

内部类CustomNewLabelRanderer:ViewRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(Control==null)
{
SetNativeControl(新的NewLabelControl());
}
if(e.OldElement!=null)
{
}
if(例如NewElement!=null)
{
Control.Text=Element.LabelText;
Control.LabelRadius=new Windows.UI.Xaml.CornerRadius(Element.LabelRadius);
颜色=Element.LabelBackground;
Control.LabelBackground=new Windows.UI.Xaml.Media.SolidColorBrush(
Windows.UI.Color.FromArgb(
(字节)(颜色A*255),
(字节)(color.R*255),
(字节)(颜色G*255),
(字节)(color.B*255));
}
}
}