Device class is marked obsolete in .net MAUI

Viewed 166

Recently I updated to the latest version of .net MAUI and I was a bit surprised to see that the MAUI Device class has now been marked as Obsolete

I have multiple use cases where I am using the device class for something or the other.

For instance now the mainthread method is obsolete:

Device.BeginInvokeOnMainThread(() =>{});

Is there a new helper class that is going to be performing these activities now or am I missing something?

enter image description here

2 Answers

As Jason pointed out in the comments apparently they have decided to dissolve the whole Device class and now all functionality present there is either being moved out or being replaced with other options, none under the same tree though more information here

Device

This is the containing class, and everything it had is now obsolete. Ready for removal in .NET 7! GetNamedSize, NamedSize and IFontNamedSizeService This used to be the only way to get scaled font sizes, but now it is built into Font. If we want to actually expose those values to the user, we can maybe use a "stringly-typed enum" to allow for supporting the other values on the system. We only had Title1, but there is also a Title2 and Title3. Device.Styles and ISystemResourcesProvider ("Device Styles" eg: {DynamicResource TitleStyle}) This is the way to get the OS font styles. This may be more popular than I think, but I am just pre-emptively removing it for now. In both cases (named sizes and device styles), I may be missing the real uses here, so this PR is to show what is going and can start a conversation.

If anything, some things need to be tweaked and not exist only in the Device class.

Named Sizes NOTE: these values appear to be magical and do things, but at XAML parse time, they are translated into a double with no idea that they were special values. Thus, they never did respond to OS changes. Once set, they are just a number and nothing special

For named sizes, we could have Font be smarter and actually have the stringly-typed value as a field. For example:

class Font {
    double Size;
    string NamedSize;
    string Family;
}

Then the font manager can do a check like this:

double realSize;
if (font.NamedSize != null) {
    realSize = GetSizeFromName(font.NamedSize);
} else {
    realSize = font.Size * scalingFactor;
}

Hopefully this helps someone out :)

Related