Universal Windows Platform – Acrylic Material

Acrylic Material demonstrates how to use Acrylic which is a feature of the Fluent Design System in Windows 10.

Step 1

If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.

vs2017

Step 2

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…

vs2017-file-new-project

Step 3

From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project
vs2017-new-project-window

Step 4

Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10, version 1803 (10.0; Build 17134) which is the April 2018 Update and the Minimum Version to be the same.

vs2017-target-platform

The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates

Step 5

In the Solution Explorer select MainPage.xaml

vs2017-mainpage

Step 6

From the Menu choose View and then Designer

vs2017-view-designer

Step 7

The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:

<Grid Margin="50">
	<Grid.RowDefinitions>
		<RowDefinition Height="Auto"/>
		<RowDefinition Height="*"/>
	</Grid.RowDefinitions>
	<ComboBox Grid.Row="0" Name="Options" HorizontalAlignment="Stretch" SelectionChanged="Options_SelectionChanged">
		<ComboBoxItem IsSelected="True">None</ComboBoxItem>
		<ComboBoxItem>SystemControlAcrylicElementBrush</ComboBoxItem>
		<ComboBoxItem>SystemControlAcrylicElementMediumHighBrush</ComboBoxItem>
		<ComboBoxItem>SystemControlBaseHighAcrylicElementMediumBrush</ComboBoxItem>
	</ComboBox>
	<Viewbox Grid.Row="1">
		<Grid>
			<StackPanel Spacing="5" Orientation="Horizontal" HorizontalAlignment="Center">
				<Rectangle Width="50" Height="50" Fill="Black"/>
				<Rectangle Width="50" Height="50" Fill="Gray"/>
				<Rectangle Width="50" Height="50" Fill="Red"/>
				<Rectangle Width="50" Height="50" Fill="Orange"/>
				<Rectangle Width="50" Height="50" Fill="Yellow"/>
				<Rectangle Width="50" Height="50" Fill="Green"/>
				<Rectangle Width="50" Height="50" Fill="Cyan"/>
				<Rectangle Width="50" Height="50" Fill="Blue"/>
				<Rectangle Width="50" Height="50" Fill="Magenta"/>
				<Rectangle Width="50" Height="50" Fill="Purple"/>
			</StackPanel>
			<Rectangle x:Name="Overlay"/>
		</Grid>
	</Viewbox>
</Grid>

The main block of XAML is a Grid Control with two Rows, the first Row contains a ComboBox with a selection of Acrylic options and the second Row contains a Grid with Rectangle Controls in a StackPanel and overlayed on them is another Rectangle.

Step 8

From the Menu choose View and then Code

vs2017-view-code

Step 9

Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:

private void Options_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
	if (Overlay != null && Windows.Foundation.Metadata.ApiInformation.IsTypePresent(
		"Windows.UI.Xaml.Media.XamlCompositionBrushBase"))
	{
		string value = (string)((ComboBoxItem)Options.SelectedItem).Content;
		Overlay.Fill = (value != "None" ? (AcrylicBrush)Application.Current.Resources[value] : null);
	}
}

The Options_SelectionChanged is an Event that will be triggered when something is Selected from the ComboBox. It uses the Windows.Foundation.Metadata.ApiInformation.IsTypePresent Method to check that Windows.UI.Xaml.Media.XamlCompositionBrushBase is present which is used for Acrylic and one of the Selected options will be used to set the Fill of the Overlay Rectangle to an AcrylicBrush.

Step 10

That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application

vs2017-local-machine

Step 11

Once the Application has started running you can use the ComboBox to select an AcrylicBrush to use that will be overlayed on the coloured Rectangle Controls

uwp-ran-acrylic-material

Step 12

To Exit the Application select the Close button in the top right of the Application

vs2017-close

This example shows how it’s possible to use the AcrylicBrush in an application which is part of the Fluent Design System to produce a glass-like effect in applications.

Creative Commons License

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s