I’m writing a cross-platform application using the Avalonia UI Community Toolkit. and it turned out that one of my TextBlocks
does not fit on the screen when launched on the phone. how to set FontSize for this TextBlock was specifically for the android version. for example for the Desktop version this is 24, and for Android it is 20
I tried to do this using ChatGPT but nothing worked
here is an example
axaml markup
<Border Grid.Row="0" Background="#2576f7" Height="28" MaxHeight="28">
<Grid>
<Button Background="Transparent" HorizontalAlignment="Left" Content="Back" FontFamily="Arial" FontSize="16" FontWeight="Bold" Command="{Binding NavigateToSelectItemMorphologyTestPageCommand}">
<Button.Styles>
<Style Selector="Button:pointerover /template/ ContentPresenter">
<Setter Property="Background" Value="Transparent"/>
</Style>
</Button.Styles>
</Button>
<TextBlock x:Name="HeaderText" Text="long text that doesn't fit on the page " FontFamily="Arial" FontSize="24" FontWeight="Bold" TextAlignment="Center"/>
</Grid>
</Border>
and just the text size of 24 does not fit on the page on the Telophane screen, but everything is fine on the computer screen.
This is what ChatGPT advised me.
...ViewModel
[ObservableProperty]
private bool isAndroid;
public ...ViewModel()
{
IsAndroid = RuntimeInformation.IsOSPlatform(OSPlatform.Create("ANDROID"));
}
and change the markup
<UserControl.Resources>
<Style x:Key="HeaderTextStyle" TargetType="TextBlock">
<Setter Property="FontFamily" Value="Arial" />
<Setter Property="FontSize" Value="24" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsAndroid}" Value="True">
<Setter Property="FontSize" Value="20" />
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid Background="#ffdadada">
<ScrollViewer VerticalScrollBarVisibility="Visible">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border Grid.Row="0" Background="#2576f7" Height="28" MaxHeight="28">
<Grid>
<Button Background="Transparent" HorizontalAlignment="Left" Content="Back" FontFamily="Arial" FontSize="16" FontWeight="Bold" Command="{Binding NavigateToSelectItemTestRussianPageCommand}">
<Button.Styles>
<Style Selector="Button:pointerover /template/ ContentPresenter">
<Setter Property="Background" Value="Transparent"/>
</Style>
</Button.Styles>
</Button>
<TextBlock Text="long text that doesn't fit on the page" FontWeight="Bold" TextAlignment="Center" Style="{StaticResource HeaderTextStyle}"/>
</Grid>
</Border>
but there were a lot of errors
1.
Error (active) AXN0002 XamlX.XamlParseException: Unable to resolve type DataTrigger from namespace https://github.com/avaloniaui Line 14, position 6.
2. Error (active) CS0103 The name 'InitializeComponent' does not exist in the current context
but I couldn’t fix them and I also couldn’t find a way to do it differently
sorry for my english as it is not my native language