Useful template methods for WinUI3 projects
The following is the general usage. For more details, see the XAML docs of the corresponding attributes.
Helpers | Usage |
---|---|
AppHelper | Helper for Application |
WindowHelper | Get and set properties of Window |
WindowSizeHelper | Limit the size of the window (works with WindowSizeHelperAttribute ) |
DragMoveAndResizeHelper | Apply drag-move and resize function to (UIElement )RootPanel |
DragZoneHelper | Reduce repetitive operations when using InputNonClientPointerSource |
TitleBarHelper | Set title bar |
TeachingTipHelper | Helper for TeachingTip |
ContentDialogHelper | Helper for ContentDialog |
Misc | Miscellaneous extension methods |
ThrowHelper | Throw helper |
Attributes | Usage |
---|---|
DependencyPropertyAttribute | Generate a dependency property |
LocalizedStringResourcesAttribute | Generate for all the .resw and .resjson files in PRIResource under the specified namespace |
|
Generate property according to the properties of the settings class T for the specified viewmodel class |
|
Generate constructor like record for the specified type, according to the properties of it |
|
Generate field _containerConfiguration and methods Initialize/Load/SaveConfiguration for the specified class |
WindowSizeHelperAttribute | Generate helper properties to limit the window size (works with WindowSizeHelper ) |
AttributeIgnoreAttribute | Ignores the effect of the specified attributes on this target |
DisableSourceGeneratorAttribute | Indicates that the source generator is disabled. This is usually used for debug purpose |
-
$\dagger$ : The attribute supportsAttributeIgnoreAttribute
Only for packaged applications.
AppContext.cs:
using Microsoft.Windows.Storage;
using WinUI3Utilities.Attributes;
namespace Sample;
[AppContext<AppConfig>]
public static partial class AppContext
{
public static string AppLocalFolder { get; private set; } = null!;
public static void Initialize()
{
AppLocalFolder = ApplicationData.GetDefault().LocalFolder.Path;
InitializeConfiguration();
AppConfig = LoadConfiguration() is not { } appConfigurations
? new() : appConfigurations;
}
public static void SetDefaultAppConfig() => AppConfig = new();
public static AppConfig AppConfig { get; private set; } = null!;
}
AppConfig.cs:
using WinUI3Utilities.Attributes;
namespace Sample;
[GenerateConstructor]
public partial record AppConfig
{
public int WindowWidth { get; set; } = 1280;
public int WindowHeight { get; set; } = 720;
...
}
When saving configuration, you can use the following code:
AppContext.SaveConfiguration(AppContext.AppConfig)
Localization (Reference)
Generate for all the .resw and .resjson files in PRIResource
under the specified namespace.
Sample.csproj
<Project Sdk="Microsoft.NET.Sdk">
...
<PropertyGroup>
<EnableDefaultPriItems>false</EnableDefaultPriItems>
</PropertyGroup>
<Target Name="InjectAdditionalFiles" BeforeTargets="GenerateMSBuildEditorConfigFileShouldRun">
<ItemGroup>
<AdditionalFiles Include="@(PRIResource)" SourceItemGroup="PRIResource" />
</ItemGroup>
</Target>
<ItemGroup>
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="SourceItemGroup" />
</ItemGroup>
...
<ItemGroup>
<PRIResource Include="**\*.resw" />
<PRIResource Include="**\*.resjson" />
</ItemGroup>
...
</Project>
AssemblyInfo.cs:
[assembly: LocalizedStringResources(nameof(Sample))]
XXX\APage.resw: ...
XXX\BWindow.resjson: ...