My code has:
<Button x:Name="correctButton" HeightRequest="60" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand">
<Button.FontSize>
<OnPlatform x:TypeArguments="x:Double" iOS="25" Android="20" />
</Button.FontSize>
</Button>
Can someone explain what x:Double means
I have broken it down for you with comments in the XAML.
<!-- Here is the button declared, note how you also see the x:Name here -->
<Button x:Name="correctButton" HeightRequest="60" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand">
<!-- Besides giving properties to a control, in this case a button, as attributes, you can also set them by adding them as child nodes. That is what happens with the FontSize here -->
<Button.FontSize>
<!-- We are not just setting the FontSize here, we are also letting the value be dependent on which platform we are running on. For iOS the value will be 25, for Android 20. -->
<OnPlatform x:TypeArguments="x:Double" iOS="25" Android="20" />
</Button.FontSize>
</Button>
Now, the x:Double is needed to tell the OnPlatform tag which type we should provide to it, this can also be x:Int32 or any other type if needed. Because we will always provide string values in the OnPlatform tag, it needs to know to what type it has to cast the value.
Remember that I pointed out to you also the x:Name property. the x is a shorthand for the namespace where to find the type. If you look at your page declaration, it will probably have an attribute like: xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml", see how the x is declared here? That is why the x is needed in front, to tell the XAML that the Double type can be found in the http://schemas.microsoft.com/winfx/2009/xaml namespace, abbreviated to x by default.
Note that the OnPlatform tag in this way is deprecated as of Xamarin.Forms 2.3.4. You should now use it as such:
<Button x:Name="correctButton" HeightRequest="60" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand">
<Button.FontSize>
<OnPlatform x:TypeArguments="x:Double" x:Key="WindowBackgroundTable">
<On Platform="Android" Value="20" />
<On Platform="iOS" Value="25" />
</OnPlatform>
</Button.FontSize>
</Button>
Sorry for the poor english.
When used in XAML this tag needs the type of the value you're sending to the property, once this is a generic method. I belive that XF uses reflection and castings to set the attribute to the specific platform.
See more in this article.
I hope it helps you.
Related
Hello I am trying to display data on labels and I want to provide extra details about the data, when the user clicks on a button e.g. .
I know there is the tag in html, where you can define a summary and the details below.
Is there a way, or even better a ContentView, which can do that in .NET MAUI?
I've thought about adding a temporary label under the summary label, with the details, but that looks weird and is not intuitive.
If by details you mean a collapse / expander (which seem to be what details is in html) you can use the expander from the .net maui community toolkit :
<Expander>
<Expander.Header>
<Label Text="Baboon"
FontAttributes="Bold"
FontSize="Medium" />
</Expander.Header>
<HorizontalStackLayout Padding="10">
<Image Source="http://upload.wikimedia.org/wikipedia/commons/thumb/f/fc/Papio_anubis_%28Serengeti%2C_2009%29.jpg/200px-Papio_anubis_%28Serengeti%2C_2009%29.jpg"
Aspect="AspectFill"
HeightRequest="120"
WidthRequest="120" />
<Label Text="Baboons are African and Arabian Old World monkeys belonging to the genus Papio, part of the subfamily Cercopithecinae."
FontAttributes="Italic" />
</HorizontalStackLayout>
</Expander>
Otherwise if you just want to provide some additional texte about an element I would go with tooltips.
<Button Text="Save"
ToolTipProperties.Text="Click to Save your data" />
I need to insert an image inside a label in Xamarin forms.
Sample: "If you find this image <image.png> you win"
I tryied to convert my png to Font and use it as Span inside the label but I failed (I don't know how to use the ttf file in the span). Any other suggestion? I prefer to avoid html view
I would say the easiest solution is a GestureRecognizer
<StackLayout>
<Label Text="Click this image" />
<Image Source="tapped.jpg" />
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding TapCommand}" CommandParameter="Image1" />
</StackLayout.GestureRecognizers>
</StackLayout>
more info on https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/gestures/tap
I have some code that looks like this:
<Frame CornerRadius="20" >
<Frame.Content>
<Grid x:Name="detailRowArea" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
<xaml:CardWordsFourRowsPlus IsVisible="{Binding FourRowsPlus}" />
<xaml:CardWordsFourRows IsVisible="{Binding FourRows}" />
<xaml:CardWordsThreeRows IsVisible="{Binding ThreeRows}" />
<xaml:CardWordsTwoRows IsVisible="{Binding TwoRows}" />
</Grid>
</Frame.Content>
</Frame>
I just now noticed that the developer had used Frame.Content
Can anyone tell me why this is used? The code seems to work without it so I am wondering what advantage it offers.
There really isn't any advantage other than I guess readability, but the Content tags really aren't needed. The Forms control in xaml will assume that any content that appears between the start and end tags is assumed to be assigned to the Content property.
If you want a more detailed explanation you can check out the Content Properties section in the docs.
If you want to not use the <Frame.Content> tag that is fine, bear in mind that Frame descends from ContentView thus you would ideally define the content of this view in the Content tag. This is more of a semantic decision as opposed to performative decision.
This being said, you would need to use the content tag if you wanted to implement any kind of OnPlatform code in your frame. For example (hand coded, could be wrong)
<Frame>
<Frame.CornerRadius>
<OnPlatform x:TypeArguments="x:Single">
<On Platform="iOS">50</On>
<On Platform="Android">75</On>
</OnPlatform>
</Frame.CornerRadius>
<Frame.Content>
<Label Text="Hello World"/>
</Frame.Content>
</Frame>
I am attempting to use a simple DataTrigger element within the XAML of a Xamarin Forms page:
<Frame BackgroundColor="Red" HorizontalOptions="Fill" VerticalOptions="FillAndExpand">
<Frame.Triggers>
<DataTrigger TargetType="Frame" Binding="{Binding IsValid}" Value="True">
<Setter Property="BackgroundColor" Value="{x:Static Color.Lime}" />
</DataTrigger>
</Frame.Triggers>
</Frame>
This configuration crashes the application with what is effectively a NullReferenceException:
Java.Lang.NullPointerExceptionAttempt to invoke virtual method 'boolean android.graphics.Bitmap.isMutable()' on a null object reference
If I comment out the Setter in the above sample, the application runs normally but, of course, the trigger doesn't work.
Can anyone please suggest what I am doing wrong?
Found it!
I had a look at the decompiled version. This is a bug that occurs when the frame has a height of 0.
And this causes the error in FrameOnPropertyChanged
Adding Padding="1" or HeightRequest="1" WidthRequest="1" to your Frame should fix it, except there is something forcing it to 0.
I am currently following the steps http://www.windowsphonegeek.com/articles/Creating-a-WP7-Custom-Control-in-7-Steps? in doing a custom control for WP7. I have created my control in a normal windows phone Portrait Page xaml file (combining some control), I am not sure how I can convert it to work in generic.xaml file (as ResourceDictionary). So far it didn't work.
I tried to use Expression Blend to do the converting but I am not sure how to do it.
Edit: I am posting my code, it is a box that displays dynamic time. I want also to add properties for providing the Date and another for the color of the box.
This is the code so far.
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
xmlns:local="clr-namespace:CereTime">
<!-- Check xmlns:local im case of error -->
<Style TargetType="local:CereT1">
<!-- After specifing the custom properties in .cs file, implement them here -->
<Setter Property="Date" Value="{TemplateBinding Date}" /> <!-- Under check -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:CereT1">
<Canvas Background="YellowGreen" Width="100" Height="100" Name="DateBox" HorizontalAlignment="Left" VerticalAlignment="Top">
<StackPanel Orientation="Vertical" Height="100" Width="100">
<TextBlock Name="Month" Text="Month" Foreground="White" TextAlignment="Center" HorizontalAlignment="Center" FontSize="24" FontWeight="Bold" Margin="0,12,0,0" />
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,6,0,0">
<TextBlock Name="Date" Text="0" VerticalAlignment="Bottom" Margin="0,0,5,0" FontSize="26.667"/>
<TextBlock Name="No_Name" Text="|" FontSize="26.667" />
<TextBlock Name="Year" Text="Year" Margin="5,0,0,0" FontSize="26.667" />
</StackPanel>
</StackPanel>
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Kindly Advise me.
Thanks,
While you can move the style of the user control to a ResourceDictionary, why bother when you can have that and the template in the corresponding XAML for your UserControl?
Simple set the inside MyUserControl.xaml , along with the other properties you wish to change.
But the whole part about separating a style of a custom control to a ResourceDictionary , haven't a awful lot to do with UserControls. Perhaps you should tell us what's really wrong, instead of asking meta-questions.
I had the same problem when trying to create control following to link you give.
Solution is to add
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="themes/generic.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
into Application.Resources tag in App.xaml