Window extension class for Win UI 3 Applications.
This simple class extends the Microsoft.UI.Xaml.Window class with the following properties you may know from WPF, but are currently missing in Win UI 3:
Title
- the window titleIcon
- the window icon (from a resource, using the application icon or from an external file path)Width
- the window widthHeight
- the window heightWindowStartupLocation
- the window startup location (Manual
orCenterScreen
)WindowState
- the window state (Normal
,Minimized
orMaximized
)ResizeMode
- the resize modes (NoResize
,CanMinimize
,CanResize
orCanResizeWithGrip
)WindowStyle
- the window sytle (None
,SingleBorderWindow
,ThreeDBorderWindow
orToolWindow
)
All properties are made available in the XAML layout files. It will also take DPI scaling into account.
To use it in your code, simple inherit from the Pyther.WindowX.WindowX
class.
public sealed partial class MainWindow : Pyther.WindowX.WindowX
{
public MainWindow() {
this.InitializeComponent();
}
}
and modify your XAML root tag
<winX:WindowX
...
xmlns:winX="using:Pyther.WindowX"
...
Title="A Pyther.WindowX Example"
Icon="Demo.Assets.window.ico"
Width="800"
Height="500"
WindowStartupLocation="CenterScreen"
WindowStyle="ToolWindow"
>
...
</winX:WindowX>
that's it!
In addition to XAML, you can of course set all properties by code:
public sealed partial class MainWindow : Pyther.WindowX.WindowX
{
public MainWindow() {
this.InitializeComponent();
this.Title = "A Pyther.WindowX Example";
this.Icon = "Demo.Assets.window.ico";
this.Width = 800;
this.Height = 500;
this.WindowStartupLocation = Pyther.WindowX.WindowStartupLocation.CenterScreen;
this.WindowStyle = Pyther.WindowX.WindowStyle.ToolWindow;
}
}
This code requires PInvoke and System.Drawing.Common (if you want to use the Icon
property) to run.
You will find all the required source in Pyther.WindowX/WindowX.cs.
The are 4 ways of using an icon:
- embedded resource - This is the default way to include an icon into your application. You add them as an embedded(!) resource and access it using the resource resolve syntax
"namespace.path.file"
likeIcon="Demo.Assets.window.ico"
. - using the application icon - If you has set an application icon, you can simply (re)use it with
Icon="application"
orIcon="app"
. - using relative file path - To load an icon from an external file relative to your application directory, you can use the
rel:
prefix likeIcon="rel:media\window.ico"
. - using absolute file path - And finally you can also load from an external absolute file path using the
abs:
prefix likeIcon="abs:E:\Projects\Pyther\WindowX\window.ico"
.
It's also worth to mention, that no Exception will be thrown, if setting an icon failed.