How to use Uno.Material

This guide will walk you through the necessary steps to setup and to use the Uno.Material package in an Uno Platform application.

Tip

The complete source code that goes along with this guide is available in the unoplatform/Uno.Samples GitHub repository - UnoMaterialSample

Prerequisites


Tip

For a step-by-step guide to installing the prerequisites for your preferred IDE and environment, consult the Get Started guide.

Step-by-steps

Section 1: Setup Uno.Material

  1. Create a new Uno Platform application, following the instructions here.

  2. Add NuGet package Uno.Material to each of project heads by:

    Note

    You may have to check the [x] Include Prerelease to find this package, as there are currently no stable release.

    Note

    The project heads refer to the projects targeted to a specific platforms:

    • UnoMaterialSample.Droid
    • UnoMaterialSample.iOS
    • UnoMaterialSample.macOS
    • UnoMaterialSample.Skia.Gtk
    • UnoMaterialSample.Skia.Tizen
    • UnoMaterialSample.Skia.WPF
    • UnoMaterialSample.UWP
    • UnoMaterialSample.Wasm

    The shared project is not part of this, and the .Skia.WPF.Host project is another exception.

    Here are some issues that you are likely to run into after complete the above step:

    • NU1605: Detected package downgrade: Uno.UI from 3.6.0-dev.275 to 3.5.1. Reference the package directly from the project to select a different version.

      The app may not compile, crash at runtime, or behave strangely as a result of this. solution: You need to update the version of Uno.UI packages for all project heads that you are using to the higher version.

      note: By Uno.UI packages, it includes:

      • Uno.UI
      • Uno.UI.RemoteControl
      • Uno.UI.WebAssembly
      • Uno.UI.Skia.Gtk
      • Uno.UI.Skia.Tizen
      • Uno.UI.Skia.Wpf
    • When building the .Droid project, the project failed to build with:

      error : Could not find 1 Android X assemblies, make sure to install the following NuGet packages:
          - Xamarin.AndroidX.Lifecycle.LiveData
      You can also copy-and-paste the following snippet into your .csproj file:
          <PackageReference Include="Xamarin.AndroidX.Lifecycle.LiveData" Version="2.1.0" />
      

      solution: Simply add the specific version of Xamarin.AndroidX.Lifecycle.LiveData to the .Droid project

  3. Add the following code inside App.xaml:

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!-- Load WinUI resources -->
                <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
    
                <!-- Load Material Color Palette -->
                <MaterialColors xmlns="using:Uno.Material" />
    
                <!-- Load the Material control resources -->
     			<MaterialResources xmlns="using:Uno.Material" />
    
                <!-- Application's custom styles -->
                <!-- other ResourceDictionaries -->
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    

Section 2: Using Uno.Material library

  1. Let's add a few controls with the Material style to MainPage.xaml:
    <Page x:Class="UnoMaterialSample.MainPage"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:toolkit="using:Uno.UI.Toolkit">
    
        <Grid toolkit:VisibleBoundsPadding.PaddingMask="Top" >
            <ScrollViewer>
                <StackPanel Margin="16,0" Spacing="8">
                    <!-- controls with material styles -->
                    <TextBlock Text="Hello, Material!" Style="{StaticResource MaterialHeadline2}" />
                    <TextBlock Text="text" Style="{StaticResource MaterialBody1}" />
                    <Button Content="button" Style="{StaticResource MaterialContainedButtonStyle}" />
                    <ComboBox ItemsSource="asd" Style="{StaticResource MaterialComboBoxStyle}" />
                    <DatePicker Style="{StaticResource MaterialDatePickerStyle}" />
                    <TextBox Text="input" Style="{StaticResource MaterialFilledTextBoxStyle}" />
    
                </StackPanel>
            </ScrollViewer>
        </Grid>
    <Page>
    
  2. Now we'll add a few new controls that are defined in the Material package - Card, ChipGroup, Chip, and Divider:
    <Page ...
          xmlns:material="using:Uno.Material.Controls">
    
        <Grid toolkit:VisibleBoundsPadding.PaddingMask="Top" >
            <ScrollViewer>
                <StackPanel Margin="16,0" Spacing="8">
                    <!-- controls with material styles -->
                    <!-- ... -->
    
                    <!-- material controls -->
                    <material:Divider SubHeader="Uno.Material Controls:" Style="{StaticResource MaterialDividerStyle}" />
                    <material:Card HeaderContent="Material Design"
                            SupportingContent="Material is a design system created by Google to help teams build high-quality digital experiences for Android, iOS, Flutter, and the web."
                            Style="{StaticResource MaterialOutlinedCardStyle}">
                        <material:Card.HeaderContentTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding}" Margin="16,14,16,0" Style="{ThemeResource MaterialHeadline6}" />
                            </DataTemplate>
                        </material:Card.HeaderContentTemplate>
                        <material:Card.SupportingContentTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding}" Margin="16,0,16,14" Style="{ThemeResource MaterialBody2}" />
                            </DataTemplate>
                        </material:Card.SupportingContentTemplate>
                    </material:Card>
                    <material:ChipGroup SelectionMode="Multiple" Style="{StaticResource MaterialChipGroupStyle}">
                        <material:Chip Content="Uno" Style="{StaticResource MaterialChipStyle}" />
                        <material:Chip Content="Material" Style="{StaticResource MaterialChipStyle}" />
                        <material:Chip Content="Controls" Style="{StaticResource MaterialChipStyle}" />
                    </material:ChipGroup>
                </StackPanel>
            </ScrollViewer>
        </Grid>
    <Page>
    
Tip

You can find the style names using these methods:

Section 3: Overriding Color Palette

  1. Create the nested folders Styles\ and then Styles\Application\ under the .Shared project
  2. Add a new Resource Dictionary ColorPaletteOverride.xaml under Styles\Application\
  3. Replace the content of that res-dict with the source from: https://github.com/unoplatform/Uno.Themes/blob/master/src/library/Uno.Material/Styles/Application/ColorPalette.xaml
  4. Make a few changes to the color:

    Here we are replacing the last 2 characters with 00, essentially dropping the blue-channel

    <!-- Light Theme -->
    <ResourceDictionary x:Key="Light">
        <Color x:Key="MaterialPrimaryColor">#5B4C00</Color>
        <Color x:Key="MaterialPrimaryVariantDarkColor">#353F00</Color>
        <Color x:Key="MaterialPrimaryVariantLightColor">#B6A800</Color>
        <Color x:Key="MaterialSecondaryColor">#67E500</Color>
        <Color x:Key="MaterialSecondaryVariantDarkColor">#2BB200</Color>
        <Color x:Key="MaterialSecondaryVariantLightColor">#9CFF00</Color>
        <Color x:Key="MaterialBackgroundColor">#FFFFFF</Color>
        <Color x:Key="MaterialSurfaceColor">#FFFFFF</Color>
        <Color x:Key="MaterialErrorColor">#F85900</Color>
        <Color x:Key="MaterialOnPrimaryColor">#FFFF00</Color>
        <Color x:Key="MaterialOnSecondaryColor">#000000</Color>
        <Color x:Key="MaterialOnBackgroundColor">#000000</Color>
        <Color x:Key="MaterialOnSurfaceColor">#000000</Color>
        <Color x:Key="MaterialOnErrorColor">#000000</Color>
        <Color x:Key="MaterialOverlayColor">#51000000</Color>
    
        <!-- ... -->
    </ResourceDictionary>
    
    <!-- Dark Theme -->
    <ResourceDictionary x:Key="Dark">
        <Color x:Key="MaterialPrimaryColor">#B6A800</Color>
        <Color x:Key="MaterialPrimaryVariantDarkColor">#353F00</Color>
        <Color x:Key="MaterialPrimaryVariantLightColor">#D4CB00</Color>
        <Color x:Key="MaterialSecondaryColor">#67E500</Color>
        <Color x:Key="MaterialSecondaryVariantDarkColor">#2BB200</Color>
        <Color x:Key="MaterialSecondaryVariantLightColor">#9CFF00</Color>
        <Color x:Key="MaterialBackgroundColor">#121212</Color>
        <Color x:Key="MaterialSurfaceColor">#121212</Color>
        <Color x:Key="MaterialErrorColor">#CF6600</Color>
        <Color x:Key="MaterialOnPrimaryColor">#0000FF</Color>
        <Color x:Key="MaterialOnSecondaryColor">#000000</Color>
        <Color x:Key="MaterialOnBackgroundColor">#FFFFFF</Color>
        <Color x:Key="MaterialOnSurfaceColor">#DEFFFFFF</Color>
        <Color x:Key="MaterialOnErrorColor">#000000</Color>
        <Color x:Key="MaterialOverlayColor">#51FFFFFF</Color>
    
        <!-- ... -->
    </ResourceDictionary>
    
    <!-- ... -->
    

    You may also use this for picking colors: https://material.io/design/color/the-color-system.html#tools-for-picking-colors

  5. In App.xaml, update the line that initializes the MaterialColors to include the new palette override:
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!-- ... -->
    
                <!-- Load Material Color Palette with ColorPaletteOverrideSource -->
     			<MaterialColors xmlns="using:Uno.Material"
     							ColorPaletteOverrideSource="ms-appx:///ColorPaletteOverride.xaml" />
    
                <!-- Load the Material control resources -->
     			<MaterialResources xmlns="using:Uno.Material" />
    
                <!-- ... -->
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    
  6. Run the app, you should now see the controls using your new color scheme.

Note

Get the complete code

See the completed sample on GitHub: UnoMaterialSample

Additional Resources



Help! I'm having trouble

Tip

If you ran into difficulties with any part of this guide, you can: