I have a UWP app and I want to upgrade it to fluent design system. I have created a new project using Windows Template Studio and my navigation is with Pivot.
Now I want to put acrylic background on the header of the pivot. as mentioned in the design guidelines of uwp it is recommended to use 70 percent acrylic in this scenario.
So I tried to use 70 percent acrylic with following code.
private void MainPivot_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.UI.Xaml.Media.AcrylicBrush"))
{
MainPivot.Background = Application.Current.Resources["SystemControlAltHighAcrylicWindowBrush"] as AcrylicBrush;
}
}
Where MainPivot is the pivot I am using and this loaded method is loaded event for that pivot.
The problem is that it works only for either Light or Dark Theme (depends which theme was set during last run of the app), but when app is running and I change theme and switch between light or dark themes, it doesn't work well for both themes, for example if I make the theme dark the acrylic color remains white and pivot header text is also white hence creating disturbed UI.
Also the FallBack Color doesn't make sense either, for light theme fallback color is black (which blends in with black text) and same problem occurs in dark theme.
The reason I am doing it from code behind because the min project target of my app is creators update which doesn't have acrylic brush.
The Conditional XAML provides a way to use the ApiInformation.IsTypePresent method in XAML markup. This lets you set properties and instantiate objects in markup based on the presence of an API without needing to use code behind.
To use a conditional method in XAML, you must first declare a conditional XAML namespace at the top of your page.
xmlns:IsAcrylicBrushPresent="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsTypePresent(Windows.UI.Xaml.Media.AcrylicBrush)"
xmlns:IsAcrylicBrushNotPresent="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsTypeNotPresent(Windows.UI.Xaml.Media.AcrylicBrush)"
After the namespace is defined, we can use the namespace prefix to the Background property of your Grid to qualify it as a property that should be set conditionally at runtime.
<Grid Name="MainPivot" IsAcrylicBrushPresent:Background="{ThemeResource SystemControlAltHighAcrylicWindowBrush}" IsAcrylicBrushNotPresent:Background="Red">
If the device supports AcrylicBrush, it will use the SystemControlAltHighAcrylicWindowBrush. If not, it will use the Red color.
Related
There are a few identical questions in SO asking about changing the background color of a button in UWP (Universal Windows Platform):
How to change the background color of button in Universal Windows Platform Apps?
how to change background color of button in UWP Apps in c# ?
However, it seems that Microsoft has been changing things. SolidColorBrush can no longer be found. I have tried using Windows.UI.Xaml.Media.Brush and Windows.UI.Colors. None of those work.
Mouse hovering over button.background, the hint shows that background is expecting type: Windows.UI.Xaml.Media.Brush.
My Question: How do I change the background color of a button using c# codes? If I use the suggested solutions in other identical posts, namespace SolidColorBrush will not be found.
Have you imported the following namespace?
Windows.UI.Xaml.Media
Because if have not, you won't be access the SolidColorBrush Class directly,
and will have to do so by:
Windows.UI.Xaml.Media.SolidColorBrush mycolor = new SolidColorBrush(Windows.UI.Colors.Blue);
In this example, I have created a SolidColorBrush with the Color of Blue, and I have setit directly, without any kind of conversion, like this:
myButton.Background = mycolor;
In case you want to create your own color, you can utilize the Windows.UI.Color.FromArgb method, where you can even specify the alpha of your Color.
Edit:
Looking back on your answer, I have realized that you were trying to create your Brush, which would set the Background of a button control, with the Brush class. The Brush class is the parent for several derived brushes classes, all with different purposes. One of them is the SolidColorBrush class.
So I have a few custom cells using ViewCell in my app. Inside my ViewCell are Labels. I want the font size of those labels to be dependent on the text size of the device just like how the TextCell's text changes when you adjust the text size of your device.
Below image is using a built-in TextCell.
Below image is using a custom cell ViewCell
Im testing this in an iOS device and the text size is set to the smallest available. Whenever I change the device text size the cells using TextCells will just automatically changed. The labels inside the ViewCell doesn't change at all. Any suggestion pointing me to the right direction is very much appreciated.
Short answer
The TextCell uses the native UITableViewCell on iOS. This native view takes the device accessibility settings into account. When you use the Xamarin Forms Label, it does not. Xamarin Forms does not support iOS Dynamic Type (yet).
Explanation
The Forms Label uses the FontSizeConverter to set the actual font size. This font size defaults to the value -1 if you don't set any value yourself. The value -1 results in the use of NamedSize.Default (see the Font class).
When you take a look at the FontSizeConverter, if it can't parse the value as an absolute value, it will try to convert to a NamedSize value. In that case, it will use Device.GetNamedSize to get the actual size. This is done through the IPlatformServices interface, which converts these to absolute values (see the IOSPlatformServices).
There is also a FontExtensions class, that implements the same conversion from Named Size to an absolute value. Not very DRY at first sight, but there could be a good reason for this, that I'm not aware of.
Unfortunately, both the FontExtensions and the IOSPlatformServices don't take the accessibility options of iOS into account, but just return absolute values.
Possible solution
On iOS, you could use UIKit.UIApplication.SharedApplication.PreferredContentSizeCategory to get the current accessibility category. Based on this value, you could determine a scale for your fonts (use DynamicResource for the font sizes if you use them in XAML, so they can change at runtime).
Or take a look at SushiHangover's answer on a similar question.
See the preferredContentSizeCategory on the Apple Developer API reference.
I am working on a Windows Store App using C#.
I am trying to change the cursor when the user hover over a rectangle but it is causing me problems.
Here is my code:
Rectangle item = sender as Rectangle;
item.Cursor = Cursors.AppStarting;
mouseVerticalPosition = e.GetCurrentPoint(null).Position.Y;
mouseHorizontalPosition = e.GetCurrentPoint(null).Position.X;
isMouseCaptured = true;
item.CapturePointer(e.Pointer);
It says 'Windows.UI.Xaml.Shapes.Rectangle' does not contain a definition for 'Cursor'.
You're confusing WPF with WinRT/XAML. These are both XAML-based UI technologies, but although superficially similar when looking at some basic controls and properties - these are completely separate implementations that have many differences once you start looking at the details. One of these is the Cursor property missing in the Windows Runtime.
You can use Window.Current.CoreWindow.PointerCursor property to get or set the cursor on the current window.
You can also use some attached behaviors I wrote in WinRT XAML Toolkit here to get an API similar to the WPF one where you set a cursor per element. There's a sample you can check here that shows how you can set a cursor on an element like this:
Extensions:FrameworkElementExtensions.SystemCursor="Arrow"
In Visual Studio, please suggest how to make custom color for a control and add it to property-> backColor Section.?
You can type a comma separated RGB value into the BackColor value in the property grid, eg:
150, 250, 70
You can use this method:
Color clr = Color.FromArgb(int alpha, int red, int green, int blue)
If you want this done using some User interface:
You could find out where the custom colors are stored for the current user, and then add one.
Or, create your own UITypeEditor, that contains the colors you want. Probably you have to inherit from Form, and override the BackColor property to add the correct attributes.
Much easier is to just set it in code, using the Argb code from the other answers.
You can not.
What you see is a default editor for Color type (create own control, add there public property of Color type and it will also uses it). Web and System tabs working fine. First tab is a sort of custom color pickup part from standard color pickup dialog.
I think MS fails to make popup editor to show modal dialog (because popup will get closed). =D
Perhaps colors there are taken from Windows color dialog, so you have to arrange it there (perhaps you can use winapi to do that). /shrug
This is a follow-up to my earlier question Draw Custom Buttons on Windows Vista/7 Aero Titlebar.
I revisited the topic quite recently and found this article which is essentially a hack to 'drawing' buttons on Aero-enabled title bar (Windows Vista & 7). What the code does is to create a transparent window over the current one and places the buttons on it, giving the impression of additional buttons on the title bar. The only problem is the buttons look like regular WinForms buttons!
My question is, how do I read the windows shell style (aka theme) in order to create buttons styled just like those in the Control Box (see image)?
I'd like answers to be in .NET (VB.NET or C#). I'm okay with unmanaged code.
So if I understand you correctly, you want to read what Windows 7 calls the "Window Color" aspect of the current theme.
Acording to MSDN http://msdn.microsoft.com/en-us/magazine/cc163435.aspx, you want DwmGetColorizationColor: "retrieves the current color that is being used for DWM glass composition. This value is based on the current color scheme. Changing the setting causes a WM_WMCOLORIZATIONCOLORCHANGED notification."
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern void DwmGetColorizationColor(out int pcrColorization, [MarshalAs(UnmanagedType.Bool)]out bool pfOpaqueBlend);
"You can check to see the composition color and opacity by calling the DwmGetColorizationColor function. If this function succeeds, it will set a GDI+ ARGB color value and a Boolean indicating whether the color is opaque. Just like changing the Aero scheme in the control panel, there's a message broadcast when the composition color has changed. WM_DWMCOLORIZATIONCOLORCHANGED is sent when this happens, but in this case the parameters tell you what the new color and opacity are."