Get `LayoutBounds` height and width from static resource in .NET MAUI - maui

I have the following XAML:
<Path Style="{StaticResource squared}"
AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="-.2, .2, .3, .3" />
<Path Style="{StaticResource squared}"
AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="1.1, .7, .3, .3" />
<Path Style="{StaticResource squared}"
AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="-.1, .9, .3, .3" />
Since the height and width are the same, I was trying to set them via a static resource.
<x:Decimal x:Key="SquareSize">.3</x:Decimal>
However, I don't see a way to use it and pass only the height and width. I have tried something like this:
<AbsoluteLayout.LayoutBounds>
<Rect>
<Rect.X>-.2</Rect.X>
<Rect.Y>.2</Rect.Y>
<!--No way to assign a static resouce below-->
<Rect.Height />
</Rect>
</AbsoluteLayout.LayoutBounds>

AbsoluteLayout.LayoutBounds needs a Xamarin.Forms.Rectangle object.
You can refer to the following code:
<x:Double x:Key="myWidth">100</x:Double>
<x:Double x:Key="myHeight">100</x:Double>
<Rectangle x:Key="mRectangle"
WidthRequest="{StaticResource myWidth}"
HeightRequest="{StaticResource myHeight}"
>
</Rectangle>
And bind it as follows:
<Frame AbsoluteLayout.LayoutBounds="{Binding mRectangle}"
AbsoluteLayout.LayoutFlags="All" BackgroundColor="Yellow">
</Frame>

Related

Correct AbsoluteLayout positioning/overlay

The main goal of the task to create AutoSuggest control.
In my case AutoSuggest control is custom control inherited from Grid
<Grid xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
...
xmlns:handlers ="clr-namespace:CrowdExchangeV2.Controls.Handlers"
WidthRequest="150">
<Grid.RowDefinitions>
<RowDefinition Height="35"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Frame Grid.Row="0"
...>
<handlers:BorderlessEntry Placeholder="{Binding Source={x:Reference this}, Path=Placeholder}"
WidthRequest="150"
.../>
</Frame>
<AbsoluteLayout
WidthRequest="150"
MaximumHeightRequest="100"
Grid.Row="1"
x:Name="resAl">
<ScrollView
MaximumHeightRequest="100"
MaximumWidthRequest="160"
x:Name="resSV">
<CollectionView x:Name="list">
<CollectionView.ItemTemplate>
<DataTemplate>
<HorizontalStackLayout VerticalOptions="Center"
HorizontalOptions="Start">
<Label Text="{Binding Key}" Padding="0,0,4,0"/>
<Label Text="{Binding Value}"/>
</HorizontalStackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ScrollView>
</AbsoluteLayout>
</Grid>
So, control looking for suggestions in Dictionary while user typing text in Entry. The thing is - when CollectionView updates suggestions it pushes all the other elements on ContentPage down... but suppose to overlay them. All right lets wrap Suggester in AbsoluteLayout on contentPage and setBounds to a desirable position. (PS: Suggester name is suggester 0_o)
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
...
Title=" ">
<AbsoluteLayout x:Name="mainAbs">
<AbsoluteLayout Margin="0,0,0,0"
x:Name="suggestAl">
<suggester:Suggester Placeholder="USD"
SearchDictionary="{Binding SearchCurrency}"
MaximumHeightRequest="150"
MaximumWidthRequest="150"/>
</AbsoluteLayout>
<Grid Padding="10" AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="All">
<ScrollView>
<VerticalStackLayout>
...
<Frame HorizontalOptions="Fill">
<VerticalStackLayout>
...
<VerticalStackLayout HeightRequest="80"
x:Name="currVSL">
<Label Text="Currency out:"
VerticalOptions="Start"
HorizontalOptions="Start"
FontSize="Header"
x:Name="curLbl"/>
**SUggester should be here**
</VerticalStackLayout>
</VerticalStackLayout>
</Frame>
</VerticalStackLayout>
</ScrollView>
</Grid>
</AbsoluteLayout>
</ContentPage>
The first idea is - to grab x:Name="currVSL" bounds, adjust it for size of Label, and place x:Name="suggestAl" there, but i cant get absolute position of x:Name="currVSL" only relative and in absolute terms it's wrong
I don't want to hardcode position in static numbers, because the position highly depends on a particular device.
So the question is how to achieve a desirable result - Place x:Name="currVSL" under the x:Name="curLbl" label.
OR
Might be there is a more elegant solution to show suggestion results without pushing all the other elements down?(Read:overlay other elements on UI)
Image to visualise UI

.NET Maui Labels in VerticalStackLayout in a CollectionView not wrapping according the LineBreakMode

I have a HorizontalStackLayout with an image and a VerticalStackLayout containing a Label to hold my Subject. If I use a smaller device I would the Subject to be wrapped accoording to the width of the device and the Labels LineBreakMode. But this is not happend.
If I manually set the width of the VerticalStackLayout the Subject is wrapped but I want this to be automaticly done.
What have I missed or done wrong?
Using VS 2022 v17.3.0 Preview 4.0
My code look like this:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:model ="clr-namespace:fotbollz.Model"
x:Class="fotbollz.MainPage">
<CollectionView>
<CollectionView.ItemsSource>
<x:Array Type="{x:Type model:NewsItem}">
<model:NewsItem Created="2022-07-27" Updated="2022-07-27 12:15"
Subject="ÖFK klar för Allsvenskan igen och man är jätteglada!"
Author="Firstname Lastname"
ClubLogo="https://fotbollz.se/_upload/teams/logo/ofk_logo.gif" />
<model:NewsItem Created="2022-07-27 14:02" Updated="2022-07-27 12:15" Subject="Min rubrik2" Author="Firstname Lastname" ClubLogo="https://fotbollz.se/_upload/teams/logo/ofk_logo.gif" />
<model:NewsItem Created="2022-07-27 12:03" Updated="2022-07-27 12:15" Subject="Ytterhogdal vinner igen!" Author="Firstname Lastname" ClubLogo="https://fotbollz.se/_upload/teams/logo/ytterhogdal_logo.gif" />
<model:NewsItem Created="2022-07-27 12:04" Updated="2022-07-27 12:15" Subject="Min rubrik4" Author="Firstname Lastname" ClubLogo="https://fotbollz.se/_upload/teams/logo/ifk_logo.gif" />
<model:NewsItem Created="2022-07-27 12:05" Updated="2022-07-27 12:15" Subject="Häggenås spelade oavgjort!" Author="Myggan Handler" ClubLogo="https://fotbollz.se/_upload/teams/logo/haggenas.png" />
</x:Array>
</CollectionView.ItemsSource>
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="model:NewsItem">
<HorizontalStackLayout Padding="5">
<!-- This is the url to the club logo-->
<Image Source="{Binding ClubLogo}" HeightRequest="75" WidthRequest="75" Aspect="AspectFill" />
<!-- This is the Subject author and created date of the news
Problem: Why isn't the long text wrapped as described in LineBreakMode. If I set WidthRequest="275" then the text is wrapped.
-->
<VerticalStackLayout Padding="5" HorizontalOptions="Center">
<Label Text="{Binding Subject}" FontSize="14" LineBreakMode="MiddleTruncation" MaxLines="1" FontAttributes="Bold" />
<Label FontSize="12" >
<Label.Text>
<MultiBinding StringFormat="{}{0:yyyy-MM-dd HH:mm} Av: {1}">
<Binding Path="Created" />
<Binding Path="Author" />
</MultiBinding>
</Label.Text>
</Label>
<Label Text="{Binding Ingress}" FontSize="18" />
</VerticalStackLayout>
</HorizontalStackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ContentPage>
The attached image shows the result: on the left is what I want and the picture on the right is what I get: What I want and what I get

Flex Layout , Xamarin Forms one image with three labels

Trying to put one image and three label in a row with multiple columns just using flex layout and without nested flex layout, need some help.
<XF.FlexLayout direction="Column">
<XF.CollectionView itemsSource={Bind.oneWay(() => this.viewModel.list)}
itemsLayout="VerticalList">
<XF.CollectionView.itemTemplate>
<XF.FlexLayout {...XF.FlexLayout.grow(1)}>
<XF.Label margin="10, 10, 10, 10" text={Bind.twoWays((x) => x.data.name)}
fontAttributes="Bold" fontSize={18}/>
<XF.Label text={Bind.twoWays((x) => x.data.description)}
fontSize={16} />
<XF.Label text={Bind.twoWays((x) => x.data.price)}
fontSize={16} />
<XF.ImageButton margin="10, 10, 10, 10" widthRequest={80} heightRequest={120} {...XF.FlexLayout.basis(100)}
source={Bind.twoWays((x) => x.data.url)}
aspect="AspectFill"
{...XF.FlexLayout.order(-1)} />
<XF.Button {...XF.FlexLayout.basis(150)}
margin="10, 10, 10, 10"
heightRequest={50}
widthRequest={150}
text="Details"
borderRadius="6"
backgroundColor="#F26857"
borderColor="#F26857"
borderWidth={2}
textColor={Colors.white} />
</XF.FlexLayout>
</XF.CollectionView.itemTemplate>
</XF.CollectionView>
</XF.FlexLayout>
</XF.ContentPage>
You can use Grid to achieve it:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="50" />
<ColumnDefinition Width="50" />
<ColumnDefinition Width="50" />
</Grid.ColumnDefinitions>
<Image Grid.Row="0" Grid.Column="1" />
<Label text="lbl1" Grid.Row="0" Grid.Column="2" />
<Label text="lbl2" Grid.Row="0" Grid.Column="3" />
<Label text="lbl3" Grid.Row="0" Grid.Column="4" />
</Grid>

Dynamic layout for WKInterfacePicker - Swift - Watch App

I am creating a watch app and I have a problem with my pickers.
I want have some pickers side by side horizontal like this :
<!--Page1-->
<scene sceneID="db6-OT-aHK">
<objects>
<controller identifier="Page1" id="U48-q0-PUn" customClass="Page1" customModule="swiftTest_WatchKit_Extension">
<items>
<group width="1" alignment="center" verticalAlignment="center" id="tfC-gp-OjA">
<items>
<picker width="0.33000000000000002" height="100" alignment="center" verticalAlignment="center" id="5k4-Oj-Imh"/>
<picker width="0.33000000000000002" height="100" alignment="center" verticalAlignment="center" id="CWU-iM-QA5"/>
<picker width="0.33000000000000002" height="100" alignment="center" verticalAlignment="center" id="7EB-A5-pKO"/>
</items>
</group>
<group width="1" alignment="left" verticalAlignment="bottom" id="nmP-yl-udb">
<items>
<button width="1" alignment="left" title="Button" id="rSf-z8-JuU"/>
</items>
</group>
</items>
<connections>
<outlet property="goButton" destination="rSf-z8-JuU" id="FMh-MS-VME"/>
<outlet property="picker1" destination="5k4-Oj-Imh" id="Y6q-wM-MPf"/>
<outlet property="picker2" destination="CWU-iM-QA5" id="X2S-hS-XfV"/>
<outlet property="picker3" destination="7EB-A5-pKO" id="V3z-ek-cGw"/>
</connections>
</controller>
</objects>
<point key="canvasLocation" x="463" y="61"/>
</scene>
But, my WKPickerItem vary in size. With my method which fix the size of each picker, I have big white space for short WKPickerItem and some others WKPickerItem are cut because they are too long..
Is it possible to have variable size for these pickers ?
I can adapt size of picker dynamically with setWidth/Height but there is not possibilities to adapt automatically the size of pickers.
I think that pickers are not thought to do this in the watch

WPF Cell Button Hide on certain condition

I have data source which, getting bind to grid in WPF. Data source is
array of students, with following fields
Name, Grade
Grid have 3 columns
Name, Grade, Settings
Settings column contains simple button for settings as below
<DataGridTemplateColumn Header="Settings" Width="75" CanUserResize="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Name="cSettings" Click="cSettings_Click" Style="{DynamicResource EditSettingsButton}" Width="50" >
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Now, If grade is equal to one then only,Settings Button should get displayed.
Can I write condition in XAML itself ? i.e. Visibility of button should be on some condition ?
Tried below approach but not working
<DataTemplate>
<Button Name="cSettings" Click="cSettings_Click" Style="{DynamicResource EditSettingsButton}" Width="50" >
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Binding="{Binding Grade}" Value="1">
<Setter Property="Visibility" Value="Visible"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Grade}" Value="2">
<Setter Property="Visibility" Value="Hidden"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Button>
</DataTemplate>
Thanks
You could try using a converter instead. See this link