- 1 year ago
Winui3 : How to fetch values from Settings for display resolution and display orientation and add it to a combobox
Comment
1 / 1
To fetch the display resolution and orientation settings in WinUI3 and add them to a combobox, you can use the following steps:
- Import the required namespaces:
csharpusing Windows.Graphics.Display;
using Windows.Storage;
using Windows.UI.Xaml.Controls;
- Retrieve the current display resolution and orientation from the
DisplayInformation
class:
csharpvar displayInfo = DisplayInformation.GetForCurrentView();
var resolution = $"{displayInfo.ScreenWidthInRawPixels} x {displayInfo.ScreenHeightInRawPixels}";
var orientation = displayInfo.CurrentOrientation.ToString();
- Store the values in the app's
ApplicationData
settings:
csharpvar localSettings = ApplicationData.Current.LocalSettings;
localSettings.Values["resolution"] = resolution;
localSettings.Values["orientation"] = orientation;
- Retrieve the values from the settings and add them to the combobox:
csharpvar comboBox = new ComboBox();
var resolutionSetting = (string)localSettings.Values["resolution"];
var orientationSetting = (string)localSettings.Values["orientation"];
comboBox.Items.Add(resolutionSetting);
comboBox.Items.Add(orientationSetting);
You can customize the combobox display by using the ComboBox.ItemTemplate
property and binding the ItemsSource
property to an observable collection of settings values.