I have a custom control (MyControl) in my Xamarin.Forms project.
I wonder if it is possible to style my control with CSS? Currently I do not know how to specify a selector for my custom control.
If I style the control in XAML, I need to import a namespace and use it:
xmlns:ctrls="clr-namespace:DemoApp.Controls"
...
<Style TargetType="ctrls:MyControl"...
CSS also supports namespaces:
#namespace ctlrs "clr-namespace:DemoApp.Controls";
However, if I try to write a CSS rule...
ctrls|MyControl {
margin: 10;
}
...I get a System.NotSupportedException: 'AT-rules not supported'.
So I wonder if there is a solution for custom control to be styled with CSS.
Select element by base class
CSS in file /Styles/styles.css in Xamarin.Forms project:
^MyClass {
font-style: italic;
}
XAML:
<ContentPage ...
xmlns:myNamespace="clr-namespace:MyNamespace;assembly=MyAssembly"
...
>
<ContentPage.Resources>
...
<StyleSheet Source="/Styles/styles.css" />
...
</ContentPage.Resources>
...
<myNamespace:MyClass Text="Text" />
...
</ContentPage>
Note:
The ^base selector is specific to Xamarin.Forms, and isn't part of the CSS specification.
Elements with a specific class attribute can be selected with the case sensitive .class selector:
For example , set the style of Label
style.css
.detailPageTitle {
font-style: bold;
font-size: medium;
text-align: center;
}
.detailPageSubtitle {
text-align: center;
font-style: italic;
}
in xaml
<ContentPage ...>
<ContentPage.Resources>
<StyleSheet Source="/Assets/styles.css" />
</ContentPage.Resources>
<ScrollView>
<StackLayout>
<Label ... StyleClass="detailPageTitle" />
<Label ... StyleClass="detailPageSubtitle"/>
</StackLayout>
</ScrollView>
</ContentPage>
In your case , you should make sure that your custom control has the property Margin.
For more details about using CSS in xamarin.forms you can check here .
Update
You can set the style as following
^MyControl {
background-color: lightgray;
}
Or you can directly set it in xaml
<ContentPage.Resources>
<StyleSheet>
<![CDATA[
^MyControl {
background-color: lightgray;
}
]]>
</StyleSheet>
</ContentPage.Resources>
<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<conv:MyControl Text="1111111"/>
<conv:MyControl Text="1111111"/>
<conv:MyControl Text="1111111"/>
<conv:MyControl Text="1111111"/>
</StackLayout>
Related
I have 3 buttons
<Button row="0" col="0" text="1" class="nums" style="height: 100;"/>
<Button row="0" col="1" text="2" class="nums"/>
<Button row="0" col="2" text="3" class="nums"/>
I can only get the height to change if I use inline style as button 1, button 2 and 3 rely on the app.css file and ignores the height although the other styles are applied.
.nums{
android-elevation: 4;
background-color: lightseagreen;
border-color: darkolivegreen;
border-radius: 10;
border-width: 5;
color: whitesmoke;
font-size: 24;
font-weight: bold;
height: 100;
width: 100;
}
Your height and width might get overridden by a higher specificity. Inline code basically overrides everything that is not !important, due to the increased specificity.
Also classes with the same specificity will get overridden when declared again after the first declaration. For example another class named .nums which gets processed after your code might interfer.
Simply appending your styles after the framework CSS or loading your custom CSS file after the framework CSS should do the trick.
Firstly, do not sentence case your element definitions: i.e: <button> not <Button>.
Your problem is easily solved:
<button row="0" col="0" text="1" class="nums" style="height: 100px;"/> <-- note the px added.
I'm trying to create a background image but I can't seem to being able to do so.. I looked at this question:
Nativescript background-image fullscreen
But without any luck. It doesn't show enough of the markup and also the OP there doesn't seem to use nativescript-angular because when you use <Page></Page> as suggested you get an error.
And this issue says that Page shouldn't be used with angular, and I'm positive I've read the same thing in the docs.
I use this css:
.login-page {
width: 100%;
height: 100%;
padding: 40px 60px;
background-image: ~/assets/img/login.jpg;
background-position: 50% 50%;
background-size: cover;
background-color: blue;
}
With this template:
<StackLayout class="login-page text-center" orientation="vertical" [formGroup]="loginForm">
<StackLayout class="input-field m-b-20">
<TextField class="input input-rounded" formControlName="email" hint="Email"></TextField>
</StackLayout>
<StackLayout class="input-field">
<TextField class="input input-rounded" formControlName="password" hint="Password" secure="true"></TextField>
</StackLayout>
</StackLayout>
The image is at app/assets/img/login.jpg. Any ideas why this isn't working?
At Component Login.ts:
import { Page } from "ui/page";
export class LoginComponent {
constructor(private page:Page) {
page.backgroundImage = '~/images/login.jpg';
}
}
Must create a folder at app root named images like the Nativescript docs says.
I have a List with grouped Object List Items in it. Like here in the Explored App, click on Samples. Now every of those items have a padding of 1rem, given by the css with the selector .sapMLIB.sapMObjLItem .
Now I wanted to reduce the top and the bottom padding to 0.25rem, so I added a
class to the Object and imported a custom css (via manifest.json), all as described in the Walkthrough. It did not work as the normal css overwrites my custom one.
An other try was to add the class sapUiNoContentPadding to the elements, but also the css rules behind that get overwritten by the rules described in the first paragraph.
What am I doing wrong? how to remove that padding without rewriting the renderer?
MyView:
<mvc:View
controllerName="sap.ui.xxxx.someapp.controller.MyList"
xmlns="sap.m"
xmlns:mvc="sap.ui.core.mvc">
<StandardListItem title="Titel"/>
<List class="sapUiResponsiveMargin sapUiNoContentPadding"
width="auto"
items="{path : '//elementsSet',
sorter : {
path : 'attribute1}',
group : true
}
}">
<items>
<ObjectListItem title="{= ${attribute1} === '' ? 'Enter Text Please' : ${attribute1}}"
icon="{= ${attribute1} === '' ? 'sap-icon://alert' : 'sap-icon://sys-enter'}"
number="{attribute4}"
numberUnit="$"
numberState="{= ${attribute4} > 10 ? 'Error' : 'Success' }"
type="Active" press="onItemPress"
markFlagged="true" markFavorite="true"
showMarkers="true"
class="sapUiNoContentPadding myownclassforpadding">
<firstStatus>
<ObjectStatus
text="some text" />
</firstStatus>
<attributes>
<ObjectAttribute text="{attribute1}" visible="false"/>
<ObjectAttribute text="{attribute2}"/>
<ObjectAttribute text="{attribute3}" visible="false"/>
<ObjectAttribute text="{attribute4}" visible="false"/>
</attributes>
</ObjectListItem>
</items>
</List>
</mvc:View>
my css
.myownclassforpadding{
padding: 0;
background-color: green;
}
Your CSS class myownclassforpadding will not be used, because CSS from the library is more specific as it uses two classes sapMLIB and sapMObjLItem.
You can make your CSS more specific this way:
.sapMLIB.sapMObjLItem.myownclassforpadding{
padding: 0;
background-color: green;
}
Have a look at the JSBin example.
Try the below selector to override default css.
.sapMLIB.sapMObjLItem.myownclassforpadding{
padding-top: 0.25rem;
padding-bottom: 0.25rem;
background-color: green;
}
If you write !important after the properties you want to change, it will overwrite the sapUI5 class properties.
For example:
.myownclassforpadding{
padding: 0 !important;
background-color: green !important;
}
This will make your padding and background-color properties take precedence over sapUI5.
I built a List of CustomListItems in a XML View:
<!-- List with CustomListItem (seperate Icon for event) -->
<List class="cTL" id="test-list2" type="Active" headerText="CustomListItems with Icon Control" items="{path : '/products'}">
<CustomListItem title="boom" counter="3" class="cTL-item" tabindex="1">
<content>
<core:Icon tabindex="2" decorative="false"
color="{
path: 'price',
formatter:'.setPrioColor'
}"
src="sap-icon://add"></core:Icon>
<layout:VerticalLayout class="cTL-text">
<layout:content>
<Label color="#333333" class="cTL-text-title" text="Orange"></Label>
<Text maxLines="1" wrapping="true" class="cTL-text-desc" text="Spain this is a long long long text lalalala onetwothree einzweidreivier Spain this is a long long long text lalalala onetwothree einzweidreivier"></Text>
</layout:content>
</layout:VerticalLayout>
</content>
</CustomListItem>
</List>
and I added some custom css, so that it looks just like a StandardListItem:
.cTL .cTL-item.sapMLIB {
padding: 0 1rem 0 1rem;
}
.cTL .sapUiIcon {
font-size: 1.375rem;
vertical-align: 80%;
}
.cTL .cTL-text {
margin: 1rem 0.5rem 0.5rem 1rem;
}
.cTL .cTL-text .cTL-text-title.sapMLabel {
font-size: 1rem;
color: #333333;
}
.cTL .cTL-text-desc {
color: #666666;
}
So it works fine if the browser window is fullscreen, BUT: On the Screenshot you can see the List with StandardlistItems above and the CustomListItems below. .. They are not responsive! Which Layout element would you recommend to get it behave like the Standardlistitem (shorten the Text and responsive alignment)?
I would wrap the content in a HBox, and both the icon and the additional layout (which I prefer to be a VBox instead of a VerticalLayout) in their own, centered VBox:
(removed all properties except for the important ones):
<List>
<CustomListItem>
<content>
<HBox justifyContent="Start" fitContainer="true">
<VBox justifyContent="Center">
<core:Icon />
</VBox>
<VBox justifyContent="Center">
<Label />
<Text />
</VBox>
</HBox>
</content>
</CustomListItem>
</List>
In our team, we replace the VBox and HBox with sap.ui.layout.Grid control. This will allow your layout to be responsive and work in IE9.
https://sapui5.hana.ondemand.com/sdk/explored.html#/sample/sap.ui.layout.sample.GridInfo/preview
is there a way to specify something like this in WPF:
CSS:
#someSpan input { color: #f1f1f1; }
or
span input { color: #f1f1f1; }
meaning, i'd like to have all TextBlock elements within container follow x style, w/out having to apply the style to each textblock.
just to clarify, i need to do something like this in WPF.
i know about the BasedOn attribute of a style.. but that's not quite what i'm looking for here
looking for something like this
<Style x:Key="FileItem" TargetType="{x:Type #SomeContainer TextBlock}">
or maybe within SomeContainer, add a TextBlock style that will apply to all of its textblocks
You can do that, you just need to nest styles, e.g.
<Style TargetType="{x:Type Border}">
<Style.Resources>
<Style TargetType="{x:Type TextBox}">
<!-- ... -->
</Style>
<Style.Resources>
</Style>
This allows you to style TextBoxes in Borders, elements however can only have one style applied to them, so having parallel "rules" is not going to work as well.
Regarding the last part of your question, if you want to apply a style to all TextBlocks within a particular element, just put the Style within that elements resources:
<TextBlock /> <!-- unaffected -->
<Grid>
<Grid.Resources>
<Style TargetType="TextBlock">
<!-- ... -->
</Style>
</Grid.Resources>
<TextBlock /> <!-- will be styled -->
</Grid>
If you have your styles stored in a separate ResourceDictionary then you can "import" them all for a particular element by merging resource dictionaries:
<Grid>
<Grid.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/MyOtherStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Grid.Resources>
<TextBlock /> <!-- will be styled -->
</Grid>