How to reproduce something similar to CSS in Windows Form?

0

I would like to make a program with the same appearance / style as the website, however in Windows Form does not work CSS because it is a Web technology. So how could I do something like this?

    
asked by anonymous 11.12.2017 / 18:34

2 answers

5

No, CSS is not a question of Web technology, it is if the renderer supports, and in case it is not something implemented, there are desktop software that give some similar support, such as Qt for using QSS, Qt CSS, but that's another story.

The important thing is to understand that each rendering engine uses its own "css engine" in browsers and email clients, but Windows Forms does not have this, going back to what matters, however you may be interested by WPF (Windows Presentation Foundation)

Then something like in CSS:

div {
   font-familty: "Comic Sans MS";
   font-size: 14px;
   text-align: center;
}

The template in XAML would affect all TextBlock s:

<Style TargetType="TextBlock">
  <Setter Property="HorizontalAlignment" Value="Center" />
  <Setter Property="FontFamily" Value="Comic Sans MS"/>
  <Setter Property="FontSize" Value="14"/>
</Style>

Reading source:

However XamlCSS

However I found this project link

An example to apply would be, in App.xaml.cs (for example) would add this:

public App()
{
    XamlCSS.XamarinForms.Css.Initialize(this);
    ...
}

And in the App it would look like this App.xaml :

<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:css="clr-namespace:XamlCSS;assembly=XamlCSS"
             x:Class="App.App">
  <Application.Resources>
    <ResourceDictionary>
    <css:StyleSheet x:Key="inlineStyle">
        <css:StyleSheet.Content>
          .important
          {
              BackgroundColor: Red;
              FontSize: 25;
              WidthRequest: 200;
          }
          Label
          {
              TextColor: Red;
              FontSize: 25;
          }
          Button
          {
              TextColor: Blue;
              BackgroundColor: Black;
          }
        </css:StyleSheet.Content>
      </css:StyleSheet>
    </ResourceDictionary>
  </Application.Resources>
</Application>

Supported Platforms

Some functionalities

  • CSS Selectors
  • Multiple StyleSheets
  • Nested selectors (similar to Sass)
  • Variables in Css
  • Import style from other CSS files
  • Mixins
11.12.2017 / 18:55
0

CSS is specifically for HTML and can not be applied on Windows Form. It is possible to do something similar using XML. Check this link:

link

You can download the project and check the example to apply to your code.

    
11.12.2017 / 18:56