Open Closed

Problem in UWP application #6273


0
dfielec created

Hello,

I'm trying to implement an UWP application on the Xamarin Shared project.

After doing:

ApplicationBootstrapper.InitializeIfNeeds<MY_PROJECTXamarinUwpModule>();
...
Xamarin.Forms.Forms.Init(e);
...
ConfigureFlurlHttp();

in the App.xaml.cs

And:

LoadApplication(new MY_PROJECT_NAMESPACE.App());

in the MainPage.xaml.cs

the module:

namespace MY_PROJECT
{
    [DependsOn(typeof(MY_PROJECTXamarinSharedModule))]
    public class MY_PROJECTXamarinUwpModule : AbpModule
    {
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(typeof(MY_PROJECTXamarinUwpModule).GetAssembly());
        }
    }
}

and the Locale.cs:

public class Locale : ILocale, ISingletonDependency
{
	public void SetLocale(CultureInfo ci)
	{
		Thread.CurrentThread.CurrentCulture = ci;
		Thread.CurrentThread.CurrentUICulture = ci;
	}

	public CultureInfo GetCurrentCultureInfo()
	{
		var netLanguage = "en";

		if (Windows.System.UserProfile.GlobalizationPreferences.Languages.Count > 0)
		{
			var pref = Windows.System.UserProfile.GlobalizationPreferences.Languages[0];

			netLanguage = UwpToDotnetLanguage(pref);
		}

		// this gets called a lot - try/catch can be expensive so consider caching or something
		System.Globalization.CultureInfo ci = null;
		try
		{
			ci = new System.Globalization.CultureInfo(netLanguage);
		}
		catch (CultureNotFoundException e1)
		{
			// iOS locale not valid .NET culture (eg. "en-ES" : English in Spain)
			// fallback to first characters, in this case "en"
			try
			{
				var fallback = ToDotnetFallbackLanguage(new PlatformCulture(netLanguage));
				Console.WriteLine(netLanguage + " failed, trying " + fallback + " (" + e1.Message + ")");
				ci = new System.Globalization.CultureInfo(fallback);
			}
			catch (CultureNotFoundException e2)
			{
				// iOS language not valid .NET culture, falling back to English
				Console.WriteLine(netLanguage + " couldn't be set, using 'en' (" + e2.Message + ")");
				ci = new System.Globalization.CultureInfo("en");
			}
		}

		return ci;
		//return new CultureInfo(Windows.System.UserProfile.GlobalizationPreferences.Languages[0]);
	}

	string UwpToDotnetLanguage(string androidLanguage)
	{
		Console.WriteLine("Android Language:" + androidLanguage);
		var netLanguage = androidLanguage;

		//certain languages need to be converted to CultureInfo equivalent
		switch (androidLanguage)
		{
			case "ms-BN":   // "Malaysian (Brunei)" not supported .NET culture
			case "ms-MY":   // "Malaysian (Malaysia)" not supported .NET culture
			case "ms-SG":   // "Malaysian (Singapore)" not supported .NET culture
				netLanguage = "ms"; // closest supported
				break;
			case "in-ID":  // "Indonesian (Indonesia)" has different code in  .NET 
				netLanguage = "id-ID"; // correct code for .NET
				break;
			case "gsw-CH":  // "Schwiizert├╝├╝tsch (Swiss German)" not supported .NET culture
				netLanguage = "de-CH"; // closest supported
				break;
				// add more application-specific cases here (if required)
				// ONLY use cultures that have been tested and known to work
		}

		Console.WriteLine(".NET Language/Locale:" + netLanguage);
		return netLanguage;
	}

	string ToDotnetFallbackLanguage(PlatformCulture platCulture)
	{
		Console.WriteLine(".NET Fallback Language:" + platCulture.LanguageCode);
		var netLanguage = platCulture.LanguageCode; // use the first part of the identifier (two chars, usually);

		switch (platCulture.LanguageCode)
		{
			case "gsw":
				netLanguage = "de-CH"; // equivalent to German (Switzerland) for this app
				break;
				// add more application-specific cases here (if required)
				// ONLY use cultures that have been tested and known to work
		}

		Console.WriteLine(".NET Fallback Language/Locale:" + netLanguage + " (application-specific)");
		return netLanguage;
	}
}

It compiles perfectly but when I try to exectute the program, I get "System.ArgumentNullException : 'Value cannot be null.'" in the:

return new AsyncCommand(new Command(async () => await func()));

line 31 of the MY_PROJECT.Mobile.Shared\Core\Threading\AsyncCommand.cs file

and here is the stacktrace: [Code externe]

MY_PROJECT.Mobile.Shared.dll!MY_PROJECT.Core.Threading.AsyncCommand.Create.AnonymousMethod__0() Ligne 31 C# [Code externe] MY_PROJECT.Mobile.Shared.dll!MY_PROJECT.Core.Threading.AsyncCommand.Execute(object parameter) Ligne 26 C# MY_PROJECT.Mobile.Shared.dll!MY_PROJECT.Commands.HttpRequestCommand.Execute(object parameter) Ligne 27 C# MY_PROJECT.Mobile.Shared.dll!MY_PROJECT.Behaviors.InvokeCommandAction.Execute(object sender, object parameter) Ligne 58 C# MY_PROJECT.Mobile.Shared.dll!MY_PROJECT.Behaviors.EventHandlerBehavior.OnEvent(object sender, object eventArgs) Ligne 104 C# [Code externe]

please Help

Thanks in advance


1 Answer(s)
  • 0
    alper created

    hi,

    I'm not experienced in UWP development. but if you get exception in AsyncCommand you can directly use to see if the problem is AsyncCommand or not. Looks like an expected parameter is not there (maybe not coming from XAML)

    
     public class AsyncCommand : ICommand
        { 
            public static AsyncCommand Create(Func<System.Threading.Tasks.Task> func)
            {
                var funcName = nameof(func); /*put debugger to see the parameters*/
                var command = new Command(async () => await func());
                var asyncCommand =  new AsyncCommand(command);
                return asyncCommand;
            }
        }
    

    Important suggestion:

    • Close Visual Studio
    • Delete all bin and obj folders in the solution directory
    • Open solution and try again.

    And these can give clue;

    https://forums.xamarin.com/discussion/128306/value-cannot-be-null-parameter-name-type

    https://forums.xamarin.com/discussion/106106/how-can-i-resolve-value-cannot-be-null-parameter-name-source-exception-xamarin-forms-latest-ver

    https://stackoverflow.com/questions/22438816/cannot-create-custom-command-in-wpf-application