Linear Gradient in UWP XAML works fine but I need to convert it to Radial Gradient brush.
Here is my current UWP XAML code
<Page
x:Class="button_radious.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:button_radious"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Grid.Background>
<LinearGradientBrush StartPoint="0,1" EndPoint="1,0">
<GradientStop Color="#000000" Offset="0.30"/>
<GradientStop Color="green" Offset="0.65"/>
<GradientStop Color="White" Offset="0.90"/>
</LinearGradientBrush>
</Grid.Background>
</Grid>
</Page>
RadialGradientBrush is not included by default as UWP API. You will need to add reference to the Windows Community Toolkit UI - Microsoft.Toolkit.Uwp.UI which contains RadialGradientBrush which you can use as expected, the same way as in WPF.
<Grid>
<Grid.Background>
<media:RadialGradientBrush
AlphaMode="Premultiplied"
RadiusX="0.2" RadiusY="0.2"
SpreadMethod="Reflect">
<GradientStop Color="Red" Offset="0" />
<GradientStop Color="Transparent" Offset="0.25" />
<GradientStop Color="Yellow" Offset="0.50" />
<GradientStop Color="Transparent" Offset="0.75" />
<GradientStop Color="Green" Offset="1.0" />
</media:RadialGradientBrush>
</Grid.Fill>
</Grid>
Because the brush resides in the library, you will have to add the following namespace declaration to your Page element:
xmlns:media="Microsoft.Toolkit.Uwp.UI.Media"
Related
You can set a gradient background inside the xaml for a page like this
<Button.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="White" Offset="0.6" />
<GradientStop Color="Blue" Offset="1.0" />
</LinearGradientBrush>
</Button.Background>
but when I search for the format to use when doing this in a global style like
<ResourceDictionary>
<Style TargetType="Button">
<Setter Property="BorderColor" Value="Lime" />
<Setter Property="BorderRadius" Value="10" />
......
I find nothing usable. It seems that is might not even be doable? Most links even seem to think there is no direct support for gradients without plugins, so perhaps it is a new addition to xamarin forms?
I specifically want to do this for buttons and contentpages, but I suspect the format will be the same for both - if it is supported at all.
You can try with the following:
<ResourceDictionary>
<Style TargetType="Button">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="White" Offset="0.6"/>
<GradientStop Color="Blue" Offset="1.0"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Keep in mind that currently Xamarin.forms is having an issue with global implicit style
Documentation: styles-and-templates
I want to animate a GradientBrush (LinearGradientBrush in my case) used by a control's fill property. I tried to change the gradient stops values (offset or color) in my storyboard but it doesn't seem to work. I target a grid's background for the example:
<Grid x:Name="LogoGrid" Height="512" Width="512">
<Grid.Background>
<LinearGradientBrush x:Name="LogoBackgroundBrush" StartPoint="0 0" EndPoint="1 1">
<GradientStop x:Name="Stop0" Color="Transparent" Offset="0" />
<GradientStop x:Name="Stop1" Color="#80FFFFFF" Offset="0.5" />
<GradientStop x:Name="Stop2" Color="Transparent" Offset="1" />
</LinearGradientBrush>
</Grid.Background>
</Grid>
And the storyboard:
<Storyboard x:Key="LoadingStoryBoard">
<ColorAnimationUsingKeyFrames Storyboard.TargetName="LogoGrid"
Storyboard.TargetProperty="(UIElement.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)"
RepeatBehavior="Forever" EnableDependentAnimation="True">
<LinearColorKeyFrame Value="#40000000" KeyTime="0:0:1" />
<LinearColorKeyFrame Value="#A0FFFFFF" KeyTime="0:0:2" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
Did you make sure to set EnableDependentAnimation to true?
You can look at my answer to another similar question for a complete example.
You didn't mention how you start the storyboard. Anyway, I made it work by replacing x:Key with x:Name (otherwise I can't reference the storyboard from code).
XAML
<Grid x:Name="LogoGrid">
<Grid.Resources>
<Storyboard x:Name="LoadingStoryBoard">
<ColorAnimationUsingKeyFrames
Storyboard.TargetName="LogoGrid"
Storyboard.TargetProperty="(UIElement.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)"
RepeatBehavior="Forever"
EnableDependentAnimation="True">
<LinearColorKeyFrame Value="#40000000" KeyTime="0:0:1" />
<LinearColorKeyFrame Value="#A0FFFFFF" KeyTime="0:0:2" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</Grid.Resources>
<Grid.Background>
<LinearGradientBrush x:Name="LogoBackgroundBrush" StartPoint="0,0" EndPoint="1,1">
<GradientStop x:Name="Stop0" Color="Transparent" Offset="0" />
<GradientStop x:Name="Stop1" Color="#80FFFFFF" Offset="0.5" />
<GradientStop x:Name="Stop2" Color="Transparent" Offset="1" />
</LinearGradientBrush>
</Grid.Background>
</Grid>
Code-behind
public sealed partial class MainPage
{
public MainPage()
{
InitializeComponent();
Loaded += (sender, args) => LoadingStoryBoard.Begin();
}
}
Here is a complete demo project on GitHub.
Edit
Tangetial: This shows how to access the storyboard through x:Key instead of my x:Name. The trick is to access the storyboard through Resources, e.g.:
((Storyboard)Resources["LoadingStoryboard"]).Begin();
I am trying to create a rectangle with a gradient that reflects outside. I want something similar to this (but for a rectangle/grid). I was not able to achieve it using a LinearGradientBrush. Below is what I tried. But that is not at all what I want.
<Border Grid.Row="1" Grid.ColumnSpan="2">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="White" Offset="1" />
</LinearGradientBrush>
</Border.Background>
Thank you.
It's doable, but tricky. You'll need to do it in Blend because there's a gradient tool that lets you easily rotate a gradient and adjust the gradient stops. You'll need a parent grid with two child grids of equal size. One child will need the gradient to be horizontal, and the other vertical. You will have to adjust the opacity on both grids so the gradient appears to be one. I threw this XAML together in a few minutes, so it's not perfect, but it might help you out....
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid Height="200">
<Grid.Background>
<LinearGradientBrush EndPoint="0.51,1.024"
StartPoint="0.507,0.052">
<GradientStop Color="White"
Offset="1" />
<GradientStop Color="#FFF7F7F7"
Offset="0.098" />
<GradientStop Color="Black"
Offset="0.5" />
<GradientStop Color="#FFC3C3C3"
Offset="0.211" />
<GradientStop Color="White"
Offset="0.829" />
</LinearGradientBrush>
</Grid.Background>
</Grid>
<Grid Height="200"
Margin="0,220">
<Grid.Background>
<LinearGradientBrush EndPoint="1.001,0.588"
StartPoint="-0.001,0.596">
<GradientStop Color="White"
Offset="1" />
<GradientStop Color="#FFF7F7F7" />
<GradientStop Color="#7F000000"
Offset="0.498" />
</LinearGradientBrush>
</Grid.Background>
</Grid>
</Grid>
I have problems with RadPieChart. Always the radPieChart shows all series in the same color (Blue) and doesn't apply the DefaultPalette. The xaml code is the following:
<telerik:RadPieChart x:Name="chartTipoGasto" Palette="{Binding DefaultPalette}" Grid.Row="3">
<telerik:PieSeries ItemsSource="{Binding}" RadiusFactor="0.9">
<telerik:PieSeries.ValueBinding>
<telerik:PropertyNameDataPointBinding PropertyName="Importe"></telerik:PropertyNameDataPointBinding>
</telerik:PieSeries.ValueBinding>
<telerik:PieSeries.LabelDefinitions>
<telerik:ChartSeriesLabelDefinition Margin="-7">
<telerik:ChartSeriesLabelDefinition.Binding>
<telerik:PropertyNameDataPointBinding PropertyName="Concepto"></telerik:PropertyNameDataPointBinding>
</telerik:ChartSeriesLabelDefinition.Binding>
</telerik:ChartSeriesLabelDefinition>
</telerik:PieSeries.LabelDefinitions>
</telerik:PieSeries>
</telerik:RadPieChart>
I don't know why all series is displayed in blue and not in diferents colors like its show in demo projects that I download from Telerik website.
Thanks in advance!
Is there actually something you're binding to with the Palette="{Binding DefaultPalette}" you have declared?
You can apply your own Palette like this (You change the Gradients to Solid brushes or I think even Images if you wanted) and add as many as you want.
<chart:RadChart.PaletteBrushes>
<RadialGradientBrush>
<GradientStop Color="#FF010DBE"
Offset="0" />
<GradientStop Color="#FF0659FD"
Offset="0.5" />
<GradientStop Color="#FF0117CA"
Offset="1" />
</RadialGradientBrush>
<LinearGradientBrush EndPoint="0.5,1"
StartPoint="0.5,0">
<GradientStop Color="#FF029912"
Offset="0" />
<GradientStop Color="#FF14FD22"
Offset="0.492" />
<GradientStop Color="#FF03930C"
Offset="1" />
</LinearGradientBrush>
<SolidColorBrush Color="#FFFCBA2A"/>
<RadialGradientBrush>
<GradientStop Color="#FFDE9A05"
Offset="0" />
<GradientStop Color="#FFF7AB05"
Offset="0.5" />
<GradientStop Color="#FFDC8E03"
Offset="1" />
</RadialGradientBrush>
</chart:RadChart.PaletteBrushes>
UPDATE: I moved the TextBox's style to the phone:PhoneApplicationPage.Resources tag and it behaves exactly the same way so it turns out that is not the fact the I am using a ResourceDictionary what is causing the problem but that there is something wrong with the way I am defining the style.
I just started playing around with ResourceDictionaries and I really like them but when I tried to use them on my application everything stopped working.
First the following TextBox:
<TextBox Grid.Column="1"
Grid.Row="0"
x:Name="Value"
InputScope="Number"
TextAlignment="Right"
TextChanged="OnValueTextChanged">
<TextBox.Style>
<StaticResource ResourceKey="InputTextBox" />
</TextBox.Style>
</TextBox>
Update: I have updated the ResourceDictionary per XAMeLi's answer and now I see the borders but it would seem as the TextBox does not have any background but, when I click on it nothing happens as if the TextBox is not even there. Then by pure luck I noticed that if I click on the bottom border the numeric keyboard would pop up as if the TextBox is too small or hiding below the border element. I tried modifying the TextBox height to no avail. This is driving me crazy.
Then the ListPickers are even worse:
<toolkit:ListPicker
Grid.Column="0"
Grid.ColumnSpan="2"
Grid.Row="1"
x:Name="CategoriesPicker"
HeaderTemplate="{StaticResource ListPickerHeaderTemplate}"
FullModeItemTemplate="{StaticResource CategoriesPickerTemplate}"
ExpansionMode="FullScreenOnly"
BorderThickness="0"
Padding="0"
Margin="0"
SelectionChanged="OnCategoriesPickerSelectionChanged">
<toolkit:ListPicker.Style>
<StaticResource ResourceKey="ListPickersStyle"/>
</toolkit:ListPicker.Style>
</toolkit:ListPicker>
When the Style is in it won't even bind the data I'm giving to it.
The file with the ResourceDictionary looks like this:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit">
<Style x:Name="InputTextBox" TargetType="TextBox">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Margin" Value="-12"/>
<Setter Property="Height" Value="50"/>
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0 0" EndPoint="0 1">
<GradientStop Color="DarkGray" Offset="0"/>
<GradientStop Color="DarkGray" Offset=".3"/>
<GradientStop Color="LightSlateGray" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Border
BorderThickness="2"
Margin="15"
CornerRadius="3">
<Border.BorderBrush>
<LinearGradientBrush StartPoint="0 0" EndPoint="0 1">
<GradientStop Offset="0" Color="DarkGray"></GradientStop>
<GradientStop Offset="0.3" Color="DarkGray"></GradientStop>
<GradientStop Offset="1" Color="LightSlateGray"></GradientStop>
</LinearGradientBrush>
</Border.BorderBrush>
<Border
BorderThickness="2"
CornerRadius="3">
<Border.BorderBrush>
<LinearGradientBrush StartPoint="1 1" EndPoint="1 0">
<GradientStop Offset="1" Color="Gray"></GradientStop>
<GradientStop Offset="0.3" Color="DarkGray"></GradientStop>
<GradientStop Offset="0" Color="DarkGray"></GradientStop>
</LinearGradientBrush>
</Border.BorderBrush>
</Border>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Name="ListPickersStyle" TargetType="toolkit:ListPicker">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="toolkit:ListPicker">
<Border
BorderThickness="2"
Padding="0"
Margin="10"
CornerRadius="3"
Background="DarkGray">
<Border.BorderBrush>
<LinearGradientBrush StartPoint="0 0" EndPoint="0 1">
<GradientStop Offset="0" Color="DarkGray"></GradientStop>
<GradientStop Offset="0.3" Color="DarkGray"></GradientStop>
<GradientStop Offset="1" Color="LightSlateGray"></GradientStop>
</LinearGradientBrush>
</Border.BorderBrush>
<Border BorderThickness="2"
CornerRadius="3">
<Border.BorderBrush>
<LinearGradientBrush StartPoint="1 1" EndPoint="1 0">
<GradientStop Offset="1" Color="Gray"></GradientStop>
<GradientStop Offset="0.3" Color="DarkGray"></GradientStop>
<GradientStop Offset="0" Color="DarkGray"></GradientStop>
</LinearGradientBrush>
</Border.BorderBrush>
<toolkit:ListPicker
BorderThickness="0"
Padding="0"
Margin="0">
<toolkit:ListPicker.Background>
<LinearGradientBrush StartPoint="0 0" EndPoint="0 1">
<GradientStop Offset="0" Color="DarkGray"></GradientStop>
<GradientStop Offset="0.5" Color="DarkGray"></GradientStop>
<GradientStop Offset="1" Color="LightSlateGray"></GradientStop>
</LinearGradientBrush>
</toolkit:ListPicker.Background>
</toolkit:ListPicker>
</Border>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Please, somebody explain to me what am I doing wrong.
Inside your ControlTemplates there are the controls them selves, i.e. control template for text box is holding a TextBox. This is not how control templates should be used. Use Blend or VS11 to extract the default style for each control (I'd recommend doing it in a new clean solution) and then change the visual appearance.
You should be able to reference the style just like any other property, e.g.:
<TextBox Style="{StaticResource InputTextBox}"/>
And try setting your Style first, then any settings you want to override, e.g.:
<TextBox Style="{StaticResource InputTextBox}" TextAlignment="Right" />
You should use x:Key instead of x:Name in ResourceDictionary:
x:Key and x:Name are not identical concepts. x:Key is used exclusively
in resource dictionaries. x:Name is used for all areas of XAML. A
FindName call using a key value will not retrieve a keyed resource.
However, Silverlight 5 can use an x:Name (or Name) attribute as the
substitute resource key for a resource item if no x:Key exists on the
item.
and as soon as Windows Phone is far from Silverlight 5, you can not use x:Name in the dictionary.