dotnet tool/workload installation fails, because of custom NuGet sources

If you are facing this issue when using dotnet’s workload install, or tool install:

Workload installation failed: Unable to load the service index for source https://pkgs.dev.azure.com/SourceNAME/_packaging/ProjectName/nuget/v3/index.json.

Then you probably should invoke the command with the following parameter:

--ignore-failed-sources

Hope this helps πŸ˜‰

This content has 2 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

Setup .NET MAUI project on macOS

You may also be interested to know how Xamarin.Forms has grown and who will be its successor. I was interested too, so I took the time between the two holidays to test the .NET MAUI developed by Microsoft, so you’ll get a couple of articles about it. First, I’ll show you the installation and how I managed it under macOS.

It’s possible that by the time you read this post, you’ll be able to create a MAUI project from UI, but it’s not currently an available feature for me. However, a lot of the tutorials were outdated because .NET 6 with VisualStudio2022 has been released, and xCode 13 is not in beta either, so you can skip some unnecessary installations.

But what is definitely needed

  • macOS capable device,
  • Visual Studio 2022,
  • xCode 13.

Get started on macOS

First thing first, fire up your macOS terminal, because we will start working in that.

If you havent installed Redth’s MAUI check tool yet, lets do this with the command

dotnet tool install -g redth.net.maui.check

Once it has been successfully installed, you will see the following output

You can invoke the tool using the following command: maui-check
Tool 'redth.net.maui.check' (version '0.10.0') was successfully installed.

So it says, that you can invoke the maui-check command from now on, but it’s a lie, since some things of ZSH terminal, we need to manually fresh up the terminals internal things, otherwise the maui-check command will fail with “Unknown command”.

export PATH=$HOME/.dotnet/tools:$PATH

From now on, you can freely invoke the

maui-check

Which results you the following output:

      _   _   _____   _____     __  __      _      _   _   ___                  
     | \ | | | ____| |_   _|   |  \/  |    / \    | | | | |_ _|                 
     |  \| | |  _|     | |     | |\/| |   / _ \   | | | |  | |                  
  _  | |\  | | |___    | |     | |  | |  / ___ \  | |_| |  | |                  
 (_) |_| \_| |_____|   |_|     |_|  |_| /_/   \_\  \___/  |___|                 
                                                                                
? .NET MAUI Check v0.10.0.0 ?
────────────────────────────────────────────────────────────────────────────────
This tool will attempt to evaluate your .NET MAUI development environment.
If problems are detected, this tool may offer the option to try and fix them for
you, or suggest a way to fix them yourself.

Thanks for choosing .NET MAUI!
────────────────────────────────────────────────────────────────────────────────
⏳ Synchronizing configuration... ok
⏳ Scheduling appointments... ok

It can fix a lot of things for you automatically, watch the output carefully, it prompts that you want to fix the issues or not.

To be honest, i don’t know this command will do the work any of the things above, but if you run this, after doing the things above, its also fine:

sudo dotnet workload install maui

Create a new MAUI project

Lets find a suitable place for our new project, then give out the following command:

dotnet new maui -n YourMauiProjectName

From now on, you can leave the terminal, and start editing the project in your favourite IDE, example the VS2022

Run your very first application

Since the projects are cannot be set as run projects in Visual Studio 2022 yet, we need to run our apps from the command line with this command

dotnet build YourMauiProjectName -t:Run -f net6.0-maccatalyst

This will run your app with mac Catalyst compilation, but you can create Android and iOS versions also with this command

This content has 2 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

Xamarin UWP: Use multilanguage resource files properly

IIf you are experiencing the oddity that the UWP version of your application can’t find the correct language version of your ‘resx’ files, then you have probably fallen into the same error I did.

The language management of UWP apps works differently to its Android and iOS counterparts.

Reade more about it at: https://docs.microsoft.com/en-us/windows/apps/design/globalizing/manage-language-and-region

How the UWP deals with multilingualism in brief

Only the union can be set as UI language

Two different lists are considered as the list of languages supported by the application. One is the list of languages supported by windows (language pack installed), and the other is the list of languages supported by the application (resx files created for them). The intersection of these can only be handled by the language switching code.

Where to define all of the supported languages by the app

Easily, without mainting it you can define them in only one line making a change in ‘Package.appxmanifest‘ file.

  <Resources>
    <Resource Language="x-generate" />
  </Resources>

The x-generate value will collect all of the available languages on compile time.

Otherwise, you will need to list all of them one by one:

  <Resources>
    <Resource Language="EN-US" />
    <Resource Language="JA-JP" />
    <Resource Language="FR-FR" />
  </Resources>

Best practice?

Perhaps, if the application is running on UWP platform, you should make an if statement for the runtime platform and filter out languages that are not supported by windows.

// Get all of the supported language by windows in BCP-47 language tag (i.e. "en-US")
IReadOnlyList<string> userLanguages = Windows.System.UserProfile.GlobalizationPreferences.Languages;
This content has 2 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

Code by rules: Make editorconfig to enforce code conventions and consistency

You can add an EditorConfig file to your project or codebase to enforce consistent coding styles for everyone that works in the codebase. EditorConfig settings take precedence over global Visual Studio text editor settings. This means that you can tailor each codebase to use text editor settings that are specific to that project. You can still set your own personal editor preferences in the Visual Studio Options dialog box. Those settings apply whenever you’re working in a codebase without an .editorconfig file, or when the .editorconfig file doesn’t override a particular setting. An example of such a preference is indent styleβ€”tabs or spaces.

EditorConfig settings are supported by numerous code editors and IDEs, including Visual Studio. It’s a portable component that travels with your code, and can enforce coding styles even outside of Visual Studio.

When you add an EditorConfig file to your project in Visual Studio, new lines of code are formatted according to the EditorConfig settings.

EditorConfig visual editor in Visual Studio for Windows

More detailed information, and syntax can be found here:

https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options

This content has 2 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

Xamarin.Forms: Use converters with binding objects which can have null as value.

You have a converter, which should return a specific value, even if the binded object is null.

Converters does not get executed, when the binded value is null. But here is a solution, how to handle this situations.

Define an example converter

In this case we will use an Int value to bool value converter.

    public class IsIntGreaterThanConverter : IValueConverter, IMarkupExtension
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is int bindedValue && parameter is int targetValue)
            {
                return bindedValue > targetValue;
            }
            else
                return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        public object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
    }

This code returns true, if the binded value is greater than the value provided as a converter parameter.

It can be used with the following code snippet in XAML:

            <Label Text="The value is greater than 0">
                <Label.IsVisible>
                    <Binding Path="Object.IntValue"
                             Converter="{app1:IsIntGreaterThanConverter}">
                        <Binding.ConverterParameter>
                            <x:Int32>0</x:Int32> 
                        </Binding.ConverterParameter>
                    </Binding>
                </Label.IsVisible>
            </Label>

But what happends, when the ‘Object’ is null? Well the converter does not get executed. And since View’s default value of the IsVisible bindable property is true, then the label will be visible, even if the Object is null.

How to handle null scenarios?

Bindings have a property called FallbackValue. Give a value to the fallbackvalue property in order to override the default value, like this:

           <Label Text="The value is greater than 0">
                <Label.IsVisible>
                    <Binding Path="Object.IntValue"

                             FallbackValue="False"

                             Converter="{app1:IsIntGreaterThanConverter}">
                        <Binding.ConverterParameter>
                            <x:Int32>0</x:Int32> 
                        </Binding.ConverterParameter>
                    </Binding>
                </Label.IsVisible>
            </Label>

This should return false even if the binded obejct is null. πŸ™‚

This content has 2 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.