I want to use StaticResource and benefits from auto complete of VS 2017 by using some syntax like this if it is exist:
<x:Double x:Key="NormalSpacing">
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double" iOS="7" Android="3" />
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double" iOS="15" Android="10" />
</OnIdiom.Tablet>
</OnIdiom>
</x:Double>
shortly I want to give the key to the main tag which contain OnIdiom and OnPlatform tags.
Use a style for auto complete with the compact OnIdiom and OnPlatformsyntax:
<Style x:Key="stackLayoutStyle" TargetType="StackLayout">
<Setter Property="Spacing"
Value="{OnIdiom
Phone = {OnPlatform iOS=7, Android=3 },
Tablet = {OnPlatform iOS=15, Android=10 }}" >
</Setter>
</Style>
...
<StackLayout Style="{StaticResource stackLayoutStyle}">
...
</StackLayout>
Related
In most desktop apps, if you're waiting for something, the mouse cursor will change to a wait cursor. Obviously, phones are a bit different. But, in UWP land, we'd like the mouse cursor to change to a wait cursor while async operations are occurring.
Is there a way to do this in standard Xamarin Forms? Or, with a 3rd party library?
The hope would be that on phones, there would be some other visual indicator that something is occurring. I.e. keyboard colour changes or something similar.
If not, I'm guessing I'll have to implement some dependency injected code for each platform. Any tips?
Not an answer to your UWP question, but this is how you can handle things on phone:
You could add a Grid over the whole page and make it grey and half transparent so the rest of your page gets "greyed out". Then add an ActivityIndicator to the middle of the Grid to indicate something is loading. Set IsVisible of the Grid to a bool value that indicates that something is loading/happening.
This is what I got on one of my pages:
<Grid BackgroundColor="##60000000"
IsVisible="{Binding IsLoading}">
<Grid VerticalOptions="CenterAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".5*" />
<ColumnDefinition Width="9*" />
<ColumnDefinition Width=".5*" />
</Grid.ColumnDefinitions>
<Frame Grid.Column="1"
HorizontalOptions="Center"
VerticalOptions="Center"
Padding="0"
HasShadow="True"
OutlineColor="#e6e6e6">
<Grid BackgroundColor="White"
RowSpacing="0"
ColumnSpacing="0">
<StackLayout>
<ActivityIndicator IsRunning="true"
Margin="10"
HorizontalOptions="Center"
VerticalOptions="Center" />
<Label Text="{i18n:Translate Text=loading_contacts}"
Margin="20,0,20,10"
HorizontalOptions="Center"
VerticalOptions="Center"/>
</StackLayout>
</Grid>
</Frame>
</Grid>
</Grid>
Mine is a similar answer to Dennis, with a bit more re-use.
I created a control template in App.xaml
<ControlTemplate x:Key="ActivityIndicatorTemplate">
<Grid>
<ContentPresenter />
<StackLayout Style="{StaticResource BlockingPanel}"
IsVisible="{TemplateBinding BindingContext.IsBusy}">
<ActivityIndicator Style="{StaticResource ActivityIndicatorStyle}"
IsVisible="{TemplateBinding BindingContext.IsBusy}"
IsRunning="{TemplateBinding BindingContext.IsBusy}" />
</StackLayout>
</Grid>
</ControlTemplate>
Here are the styles
<Style x:Key="BlockingPanel" TargetType="StackLayout">
<Setter Property="BackgroundColor" Value="{StaticResource BlockingColor}" />
<Setter Property="HorizontalOptions" Value="FillAndExpand" />
<Setter Property="VerticalOptions" Value="FillAndExpand" />
</Style>
<Style x:Key="ActivityIndicatorStyle" TargetType="ActivityIndicator">
<Setter Property="Color" Value="White" />
<Setter Property="HorizontalOptions" Value="CenterAndExpand" />
<Setter Property="VerticalOptions" Value="CenterAndExpand" />
</Style>
<Color x:Key="BlockingColor">
<x:Arguments>
<x:Double>0</x:Double>
<x:Double>0</x:Double>
<x:Double>0</x:Double>
<x:Double>0.75</x:Double>
</x:Arguments>
</Color>
Then you can use it on any page like this
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Incident.Pages.LoginPage"
ControlTemplate="{StaticResource ActivityIndicatorTemplate}"
Title="Login">
...
</ContentPage>
I saw the following code in stackoverflow:
<UserControl x:Class="WpfApplication3.UserControl1"
x:Name="Uc1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Resources>
<Style TargetType="Label">
<Setter Property="Foreground"
Value="{Binding Foreground, ElementName=Uc1, Mode=OneWay}"/>
</Style>
</UserControl.Resources>
<Grid>
<Label Content="Label 1"/>
<Label Content="Label 2"/>
</Grid>
</UserControl>
Question: I now wonder if I can target a specific label inside my usercontrol.resources for styling. Is it possible inside my userControl? If so then how?
A Style without a Key will be applied to all instances of the Target Type within the scope.
Give the style a Key, like,
<Style TargetType="Label" x:Key="MyLabel">
and then use the Key as following
<Label Content="Label 1" Style="{StaticResource MyLabel}" />
<!--Will not apply the style to Label 2-->
<Label Content="Label 2"/>
Edit:
I read your question again, seems you want to reference a Target from a Style, not reference a Style from a Target. Is that right? It sounds unnatural, it is like wanting to know the name of an instance of a derived class, from the base class.
In my Xamarin Forms project I would like to define the Form family onect per platform and the use that in the application.
So far I've hardcoded the FontFamily per controltype
<Style x:Key="TahomaBase_Label" TargetType="Label" BaseResourceKey="SubtitleStyle">
<Setter Property="FontFamily" Value="Tahoma" />
...
</Style>
Is it posible to set Fotnfamily globally in my XAML code preferable itn a OnPlatform tag?
Define a style in the App.xaml and then reference that style throughout the app. That way you can set the font once in the App.xaml using the OnPlatform tag and never have to worry about OnPlatform in all your other XAML files.
<Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="PlatformFontSample.App">
<Application.Resources>
<ResourceDictionary>
<OnPlatform x:Key="FontFamilyName" x:TypeArguments="x:String" iOS="MarkerFelt-Thin" Android="OpenSans" WinPhone="Segoe UI" />
<Style x:Key="FontLabel" TargetType="Label">
<Setter Property="FontFamily" Value="{DynamicResource FontFamilyName}" />
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
And then:
<Label Text="{Binding Name}" Style="{DynamicResource FontLabel}" FontSize="Medium" FontAttributes="Bold" LineBreakMode="NoWrap"/>
I want to make a label appear the same size proportionally regardless of the resolution of the target device
I have the following code
<StackLayout
Orientation="Vertical" Margin="0,0,0,0" Padding="0,0,0,0"
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Label Text="{Binding AccountName}"
Style="{StaticResource labelStylePrimaryBold}"
HorizontalOptions="StartAndExpand" />
</StackLayout>
I have the above code and this in my App.xaml
<Style x:Key="labelStylePrimaryBold" TargetType="Label">
<Setter Property="TextColor" Value="#414042" />
<Setter Property="FontAttributes" Value="Bold" />
<Setter Property="FontSize" Value="15" />
</Style>
I need to make this available across my app, I've seen the following code on
https://developer.xamarin.com/guides/xamarin-forms/user-interface/text/fonts/
label.FontSize = Device.OnPlatform (
24,
Device.GetNamedSize (NamedSize.Medium, label),
Device.GetNamedSize (NamedSize.Large, label)
);
But my label does not have an ID, how would I go about linking this up?
I'm very new to Xamarin, but this seems like a fairly obvious thing to want to do.
You can use Xamarin forms NamedSize Enumeration
You can choose from:
Default
Large
Medium
Micro
Small
Example:
<Label FontSize="small" Text="Joe"></Label>
<Label FontSize="Large" Text="Joe"></Label>
The following XAML:
<ProgressBar Height="10" BorderBrush="#898989" BorderThickness="2"
Foreground="#f1592a" Background="#363636" Margin="15,0"
VerticalAlignment="Center" HorizontalAlignment="Stretch" />
...generates the following styles:
How can I solve this difference in rendering?
I tried to make progress bar in 2 styles one with styling from Resource and one with inline style , BUt still getting the same behaviour . Here is the xaml i used and output
<Window.Resources>
<Style x:Key="ProgressStyleMy" TargetType="ProgressBar">
<Setter Property="BorderBrush" Value="#898989" ></Setter>
<Setter Property="BorderThickness" Value="2" ></Setter>
<Setter Property="Foreground" Value="#f1592a" ></Setter>
<Setter Property="Background" Value="#363636" ></Setter>
</Style>
</Window.Resources>
<Grid>
<StackPanel Background="Green">
<StackPanel Orientation="Horizontal">
<Button Margin="10" Click="Button_Click">Start</Button>
<Button Margin="10" Click="Button_Click_1">Start2</Button>
</StackPanel>
<ProgressBar Value="{Binding Progress}" Height="10" BorderBrush="#898989" BorderThickness="2" Foreground="#f1592a" Background="#363636" Name="pgsbar"
Margin="15,10" VerticalAlignment="Center" HorizontalAlignment="Stretch" >
</ProgressBar>
<ProgressBar Value="{Binding Progress}" Height="10" Style="{StaticResource ResourceKey=ProgressStyleMy}" Name="pgsbar2"
Margin="15,10" >
</ProgressBar>
</StackPanel>
</Grid>
What you're referring to is the Windows look and feel. The top image demonstrates the default styling (Chrome) that is provided by the Windows 7, while the bottom one (Metro) has to do with Windows 8. If you want to achieve Metro look in Windows 7, you'll have to edit the control template.
If you don't mind third-party resources, there is an excellent collection of Metro-styled controls over at http://mahapps.com/. Here's the GitHub link: https://github.com/MahApps/MahApps.Metro I figure it's better than trying to do it yourself, since someone else has done it already.
Here's the list of all of the controls available in the toolkit: http://mahapps.com/controls/
Click on the desired ones to see previews and how to get them working in your application.