question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make XAML style writing more like CSS

See original GitHub issue

Make XAML style writing more like CSS

  1. Less code
  2. Be more logical
  3. Easier to read

I don’t know if future versions have that design in mind Let’s say I want a button with rounded corners

// version now

<Window.Resources>
      <Style x:Key="Btn" TargetType="Button">
          <Setter Property="Template">
              <Setter.Value>
                  <ControlTemplate TargetType="Button">
                      <Border BorderThickness="1" Width="60" Height="32" CornerRadius="3" Background="#4C5EE9">
                          <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" />
                      </Border>
                  </ControlTemplate>
              </Setter.Value>
          </Setter>
          <Setter Property="Foreground" Value="#fff"></Setter>
      </Style>
  </Window.Resources>

  <Button Style="{StaticResource Btn}" Content="Button"></Button>

// expect version 1

   <Style x:Key="Btn" TargetType="Button">
         Width=60,
         Height=32,
         Background="#4C5EE9",
         Foreground="#FFF",
         BorderRadius=3
   </Style>
   <Button Style="{StaticResource Btn}" Content="Button"></Button>

// expect version 2

   <Style>
       Btn {
         Width:60,
         Height:32,
         Background:"#4C5EE9",
         Color:"#FFF",
         BorderRadius: 3
       }
   </Style>
   <Button Style="{StaticResource Btn}" Content="Button"></Button>
    OR
   <Button Class="{Btn}" Content="Button"></Button>

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:23
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

12reactions
mandalorianbobcommented, Dec 15, 2020

You’re kind of comparing apples to oranges here. Based on your “Expect version 1” your first style should look like this:

<Style x:Key="Btn" TargetType="Button">
    <Setter Property="Width" Value="60" />
    <Setter Property="Height" Value="32" />
    <Setter Property="Background" Value="#4C5EE9" />
    <Setter Property="Foreground" Value="#FFF" />
    <Setter Property="BorderRadius" Value="3" />
</Style>
<Button Style="{StaticResource Btn}" Content="Button" />

Now that’s not really that bad.

(For what it’s worth, it would be great if <Setter Property="Width" Value="60" /> could instead be <Width>60</Width> but I understand that XAML doesn’t work that way at all at the moment. However, I think that’s an interesting notion - could an object (like Setter) be instead replaced by a tag that isn’t itself an object but is instead just parameters? That’d be neat.)

Of course, BorderRadius isn’t a property of Button. But what I suspect you’re doing is you’re trying to say “Look at all the stuff you have to do to change a template” but then what you show as “expected” does not look like it could result in a changed template.

So kind of not really comparing fairly. Again, apples to oranges.

But for sure, when you do have to create a new Template, it gets very verbose. If you wanna propose something that would make it less so, I think you would need to show something that looks like it could actually create a template.


Also, I don’t wanna exactly, you know, go all “language wars” or anything, but in my opinion: CSS is verbose. CSS is illogical. CSS is hard to read. But I’m not talking about “Color: Red” - I’m talking about when you have to write multiple nearly identical classes for lack of inheritance with -vendor-specific-tags whose values are like “split-inline-float” and you’ve got “clear: both” and !important randomly sprinkled throughout. I feel like CSS is an absolute nightmare once you move at all past the basics. So … you know, there’s that haha. I mean if you like CSS good on ya, but not everyone necessarily thinks it’s awesome.

1reaction
xzbaijjcommented, Jan 12, 2021

css and html are very Bad language!Because they are not rigorous

Read more comments on GitHub >

github_iconTop Results From Across the Web

Make XAML style writing more like CSS #316 - dotnet maui
Make XAML style writing more like CSS. Less code; Be more logical; Easier to read. I don't know if future versions have that...
Read more >
Styling Xamarin.Forms Apps using Cascading Style Sheets ...
Xamarin.Forms supports styling visual elements using Cascading Style Sheets (CSS).
Read more >
Equivalent of CSS in XAML
A resource dictionary maps pretty closely to a CSS file. Basically, you use these to store a mix of things: Styles.
Read more >
Xamarin.Forms Styling with CSS
This article explores the promise of styling Xamarin.Forms apps with CSS and what lies ahead. This is a new optional way to style...
Read more >
Introduction to WPF styles
WPF introduces styling, which is to XAML what CSS is to HTML. Using styles, you can group a set of properties and assign...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found