I have designed a whole app in xamarinforms, I want to add left and right padding to 10 points on all buttons in the app, is there a way to do it using App.xaml or any other way where I change the padding of the button for iOS platform only ?
Check these two ms docs:
global style (you can still override the margin/padding on specific pages)
XAML markup for platform
And this is a sample for your App.xaml:
<Application.Resources>
<ResourceDictionary>
<Style ApplyToDerivedTypes="True" TargetType="Button">
<Setter Property="Margin" Value="{x:OnPlatform iOS='10,0', Android='0'}" />
</Style>
</ResourceDictionary>
</Application.Resources>
Related
Since i have not seen something helpful after searching for a while, i decided to ask here:
I have a small testing Project, just to import a custom Icon Font from the interwebs.
Current Proedure is the following:
my MainApp.xaml consists of a single Textblock, which is referencing a Style-element in Styles.xaml, which in turn references a FontFamily where i load the ttf file per platform.
In WASM it works great (base64 URI), but in UWP i just cant get the font-icon to display at all.
The Style.xaml gets imported perfectly by the App.xaml, the Size gets applied, but it seems there is something wrong with the FontFamily-Tags.
I tried:
Installing my font by hand => Worked like a charm. So its probably not the font file?
https://platform.uno/docs/articles/features/custom-fonts.html , but it didn't help.
https://blog.mzikmund.com/2020/01/custom-fonts-in-uno-platform/ which in turn, didn't change much.
https://github.com/MartinZikmund/blog-2020/tree/master/Uno.CustomFonts even after cross-referencing here, i couldn't get it to work.
https://github.com/unoplatform/calculator/blob/uno/src/Calculator.Shared/Styles.xaml i also cross-checked with the official calculator-port, but even after setting things up like they did, nothing changed.
Reinstall VS (i use the Community Version).
Here's my Code:
MainApp.xaml:
<Page
x:Class="Testing.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Testing"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Text="L" Style="{ThemeResource IconTextStyle}" />
</Grid>
</Page>
Styles.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:xamarin="http://uno.ui/xamarin"
xmlns:macos="http://uno.ui/macos"
xmlns:wasm="http://uno.ui/wasm"
xmlns:skia="http://uno.ui/skia"
xmlns:win="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="xamarin wasm macos skia"
>
<win:FontFamily x:Key="IconScratchedFontFamily">ms-appx:///Assets/PWSmallIcons.ttf#Small-Icons-Scratched</win:FontFamily>
<macos:FontFamily x:Key="IconScratchedFontFamily">ms-appx:///Assets/PWSmallIcons.ttf#Small-Icons-Scratched</macos:FontFamily>
<wasm:FontFamily x:Key="IconScratchedFontFamily">Small-Icons-Scratched</wasm:FontFamily>
<skia:FontFamily x:Key="IconScratchedFontFamily">ms-appx:///Assets/PWSmallIcons.ttf#Small-Icons-Scratched</skia:FontFamily>
<win:FontFamily x:Key="IconClearFontFamily">ms-appx:///Assets/PWSmallIconsFree.ttf#Small-Icons-Free</win:FontFamily>
<macos:FontFamily x:Key="IconClearFontFamily">ms-appx:///Assets/PWSmallIconsFree.ttf#Small-Icons-Free</macos:FontFamily>
<wasm:FontFamily x:Key="IconClearFontFamily">Small-Icons-Free</wasm:FontFamily>
<skia:FontFamily x:Key="IconClearFontFamily">ms-appx:///Assets/PWSmallIconsFree.ttf#Small-Icons-Free</skia:FontFamily>
<Style TargetType="TextBlock" x:Key="IconTextStyle">
<Setter Property="FontFamily"
Value="{StaticResource IconScratchedFontFamily}" />
<Setter Property="FontWeight"
Value="Normal" />
<Setter Property="FontSize"
Value="116" />
</Style>
</ResourceDictionary>
App.xaml:
<Application
x:Class="Testing.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Testing">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Found the solution:
<win:FontFamily x:Key="IconScratchedFontFamily">ms-appx:///Assets/PWSmallIcons.ttf#PWSmallIcons</win:FontFamily>
<macos:FontFamily x:Key="IconScratchedFontFamily">ms-appx:///Assets/PWSmallIcons.ttf#PWSmallIcons</macos:FontFamily>
Background: The part after the # indicates the name of the font, as it is written in the .ttf File, not name of the Font as I want to reference it, which is pretty obvious if you think about it.
It was nearly impossible to figure out with my provided code, so I decided to update my question for anyone else running into this problem.
For Win and macOS the way to define the FontFamily in the Resources is by using just the FontName that in your case it would be something like:
<win:FontFamily x:Key="IconScratchedFontFamily">Small-Icons-Scratched</win:FontFamily>
<macos:FontFamily x:Key="IconScratchedFontFamily">Small-Icons-Scratched</macos:FontFamily>
and
<win:FontFamily x:Key="IconClearFontFamily">Small-Icons-Free</win:FontFamily>
<macos:FontFamily x:Key="IconClearFontFamily">Small-Icons-Free</macos:FontFamily>
With this, you should be able to use your FontFamily definitions in any of your Styles.
Hope this helps.-
Is there an easy way to set the background color of all windows in app.xaml?
I hope to set the default background color of windows in app.xaml.
Please let me know how to set the background color of all windows in xaml.
Thanks in advance.
You can set default styles in App.xaml file like that:
<Application.Resources>
<Style TargetType="{x:Type Window}">
<Setter Property="Background" Value="Black" />
</Style>
</Application.Resources>
TargetType is looking for specific type of element, For example Gird, Button, DockPanel
Property is specifying exact value you want to set
Value is the value itself
I have a class library where I'm defining (basically extending) some controls such as TextBox, Button etc. I'm also using MaterialDesignInXamlToolkit which is used to stylize controls. So my class library will essentially have controls with my own extended functionality and they will look like styles defined in MaterialDesignInXamlToolkit.
Now my question is, since I don't have App.xaml in class library project, where should I write the XAML code to import the styles of MaterialDesignInXamlToolkit, so that they will be applied to my extended controls? What is the place in class library where you can specify styles which are globally accessible and are applied to all the controls?
I searched about this but didn't find what I want. Please help.
Update: Here is my code (not working).
MaterialTextBox.cs
using System.Windows.Controls;
namespace MaterialControls
{
public class MaterialTextBox : TextBox
{
... some extra features here (no XAML file for this class, just this .cs)...
}
}
Themes.xaml (this will contain all the global styles)
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MaterialControls">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary>
<Style TargetType="local:MaterialTextBox">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Height" Value="100"/>
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Now I want these styles to apply to MaterialTextBox so that wherever I use it, it should come with this look and featues out of the box.
What is the place in class library where you can specify styles which are globally accessible and are applied to all the controls?
There is none really. In a single resource dictionary, you could use <ResourceDictionary.MergedDictionaries> to import resources that the resources that you define in the resource Dictionary itself are based on, e.g.:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication8">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="...">
<!-- style based on MaterialDesignTheme -->
</Style>
</ResourceDictionary>
But there is no concept of an App.xaml or some kind of "global resource cache" in a class library.
Found the solution.
I was using Class Library project where I actually should have used WPF Custom Control Library project. Here project type is important otherwise you will have to play with .csproj file to make it work.
So now created a new WPF Custom Control Library project (New Project > Windows > Classic Desktop > WPF Custom Control Library template). This project has Themes\Generic.xaml file which will be used as a default location for styles.
There is no concept for a dictionary in the assembly which is automaticaly merged into app.xaml. But for a default control style there is one.
To assign a default style set the DefaultStyleKeyProperty for the control.
static MaterialTextBox() {
DefaultStyleKeyProperty.OverrideMetadata(typeof(MaterialTextBox), new FrameworkPropertyMetadata(typeof(MaterialTextBox)));
}
and in Themes\Generic.xaml add the style:
<Style TargetType="{x:Type local:MaterialTextBox}">
...
</Style>
Do not merge Themes\Generic.xaml in your App.xaml
do only add default styles for controls created in this assembly.
The resources in Themes\Generic.xaml are not globaly available, but through the DefaultStyleKeyProperty the resource is found and assigned to the control.
I'm trying to add a resource dictionary to a page but I keep getting a designer error in my xaml. The app runs fine with no issue but the designer error bothers me.
Here's how I did it. Both works fin at run time. But it's saying Failed to set "Source". Any clues?
That also gives an error to all the static resource I used from the resource dictionary.
Update:
This is another approach. Instead of directly adding it to the Page's resources, I added it to the Application.Resources still cant resolve the styles. I'm using VS2017 v15.4.4
Steps to reproduce:
List item
Create a new UWP Application Project (App1)
Create a new UWP Class Library (ClassLibrary1)
In ClassLibrary1, add a new ResourceDictionary (Dictionary1.xaml).
In Dictionary1.xaml, add a Style, let's say a button style.
<Style TargetType="Button" x:Name="ButtonStyle1" x:Key="ButtonStyle1">
<Setter Propeprty="Background" Value="Red" />
</Style>
In the ClassLibrary1, add a new BlankPage (BlankPage1)
In the BlankPage1, add a button and use ButtonStyle1
`
In App1, merge the resource dictionary to App.Resources (inside App.xaml).
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionary>
<ResourceDictionary Source="ms-appx:///ClassLibrary1/Dictionary1.xaml" />
</ResourceDictionary.MergedDictionary>
</ResourceDictionary>
</Application.Resources>
Make the BlankPage1 the start page, go to App.xaml.cs and and change MainPage to BlankPage1:
if (e.PrelaunchActivated == false)
{
if (rootFrame.Content == null)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
rootFrame.Navigate(typeof(BlankPage1), e.Arguments);
}
// Ensure the current window is active
Window.Current.Activate();
}
Run the application. It will run just fine. But the problem is in the designer where it cannot recognize the style from the resource dictionary.
I followed your steps and found a typo and an issue.
The typo: In Style step (Perhaps only in the sample), There is typo in the word 'Proeprty':
<Style TargetType="Button" x:Name="ButtonStyle1" x:Key="ButtonStyle1">
<Style.Setters>
<Setter Property="Background" Value="Red" />
</Style.Setters>
</Style>
The issue in the way you are merging dictionary, it should be using the following syntax:
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>
After fixing both issues, Visual studio designer didn't complain about anything.
Did it fix the issue for you? If not then your steps to reproduce aren't complete.
Problem
I am working on an application, and recently I reorganized the file structure as I was adding more parts. Around this time I noticed that at runtime, styles from my App.xaml weren't getting applied. I made a lot of code changes at once before building so I am unsure of what may have changed.
Information
App.xaml remained unchanged before it stopped getting applied.
My program is laid out to have a main <ContentControl> that I bind some views to for some MVVM action. Originally I had all my views for different subfunctions of my program in one Views folder, but individual parts were getting too large so I split them up from this:
<Project>/Models
<Project>/ViewModels
<Project>/Views
to
<Project>/EventEditor/Models
<Project>/EventEditor/ViewModels
<Project>/EventEditor/Views
<Project>/ConfigurationEditor/Models
<Project>/ConfigurationEditor/ViewModels
<Project>/ConfigurationEditor/Views
My UserControl doesn't have any resources or styles of its own and should only be using the ones from App.xaml for now.
I only have 1 App.xaml file and no other files containing styles/dictionaries/etc outside of the individual view XAMLs.
Here is my App.xaml:
<Application x:Class="EventSuite.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:EventSuite"
StartupUri="MainWindow.xaml">
<Application.Resources>
<!-- Global Styles -->
<Style TargetType="ListBoxItem">
<Setter Property="FontSize" Value="14" />
</Style>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="14" />
</Style>
<Style TargetType="Label">
<Setter Property="FontSize" Value="14" />
</Style>
<Style TargetType="Button">
<Setter Property="Margin" Value="2,0,2,0"/>
<Setter Property="Padding" Value="2"/>
<Setter Property="FontSize" Value="14"/>
</Style>
</Application.Resources>
Steps Taken
Clean build/rebuild
Breaking and using Tree Visualizer to investigate (no luck there)
Diffing files/projects. Nothing that I found could be causing this (I don't know exactly what to be looking for)
Questions
Is there a way to debug styles which are being used? I tried the WPF Tree Visualizer and it showed null for Style on my buttons, which is what I would expect since I didn't explicitly set any style.
Could file structure have a role to play? Perhaps App.xaml isn't being read correctly, or it's not being applied correctly. I haven't seen any warnings or indications of failure.
Namespace related? I would hope not...
In summary I am a little lost as to why App.xaml applies in design time and not at runtime. Why could this be happening after some changes to my project. I am lost! Let me know if you need more information or examples.
Turns out it was an issue with App.xaml StartupURI.
My StartupURI was MainWindow.xaml and my App.xaml.cs was as follows:
public partial class App : Application
{
public App()
{
var dialog = new SageCenter.View.MainWindow();
dialog.ShowDialog();
}
}
It was never calling InitializeComponent() on my application, but my window was still getting created. I kept the StartupURI in my App.xaml and changed App.xaml.cs to this to fix it:
public partial class App : Application
{
public App()
{
InitializeComponent();
}
}
The StartupURI="MainWindow.xaml" causes the InitializeComponent() call to instantiate my window.