1
我想將樣式應用於控件。這是風格將樣式應用於自定義渲染器
<Application.Resources>
<ResourceDictionary>
<SolidColorBrush x:Key="BackgroundColor" Color="Yellow" />
<Style TargetType="Button" x:Name="myNewButtonStyle">
<Setter Property="Background" Value="{StaticResource BackgroundColor}" />
</Style>
</ResourceDictionary>
</Application.Resources>
,它可以在App.xaml中(UWP項目)中找到。這裏是自定義呈現:
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (this.Element != null)
{
var style = Windows.UI.Xaml.Application.Current.Resources["myNewButtonStyle"] as Windows.UI.Xaml.Style;
this.Control.Style = style;
}
}
的想法是基於this answer。但風格不適用。在代碼中設置背景顏色不工作:
this.Control.BackgroundColor = new SolidColorBrush(Windows.UI.Colors.Yellow);
如何申請一個樣式的自定義渲染控制?
Setter屬性可能是「BackgroundColor」而非背景。試試吧 –
*元素'Button'*上的未知成員'BackgroundColor'。 *成員「BackgroundColor」無法識別或無法訪問。* – testing