Null exception namespace App in Xamarin for UWP - xamarin

I am developing UWP app in xamarin. The application works on IOS, Mac , Android, Windows. I have created UWP project in it according to the tutorial given in developer.xamarin.com. But it giving error saying Accessibility.App namespace not found.
Here is my code:
namespace Accessibility.UWP
{
public sealed partial class MainPage
{
public MainPage()
{
this.InitializeComponent();
this.LoadApplication(new Accessibility.App());
}
}
}

According to your description, the app can't see Accessibility.App. As you claim that Android and iOS projects work there are two things that can cause the problem:
you don't have the reference to your Shared/PCL project in your UWP project (most likely).
you have possibly changed the namespace / class name in the Shared/PCL project to something else than Accessibility.App

This is usually caused by missing reference to the shared project or class library where Xamarin.Forms App class resides. From the description this project should be called Accessibility.
Right-click the UWP project select Add, Reference... then in Solution tab select the Accessibility project.
Also it might happen that the UWP project didn't pick up on the reference, so restarting Visual Studio might help as well.
If all fails, you can try to use class name binding with using. On top of the source code file add:
using FormsApp = Accessibility.App;
And then in code use:
this.LoadApplication(new FormsApp());

Related

Can't use ObservableProperty from the CommunityToolkit.MVVM

I'm trying to use the new ObservableProperty attribute from the CommunityToolkit.MVVM. Any time I add it, I get 17 errors such as "The type MainViewModel already contains a definition for FileToPlay", or "Type MainViewModel already defines a member called 'OnFileToPlayChanging' with the same parameter types". These are all in the MainViewModel.g.cs file.
I'm using VS 2022 Community, and the project has a WPF Application project template targeting .NET6.
Sample code that generates the error is:
namespace CorePlayer.ViewModel
{
public partial class MainViewModel : ObservableObject
{
[ObservableProperty]
private string? fileToPlay;
}
}
Anyone have any idea what I could be doing wrong?
Thanks
I had this exact same problem. Searching for an answer led me to this question.
My application is a WPF app on .NET Core 3.1.
I fixed it by following the guidance at the end of this YouTube video, .NET Community Toolkit.
I chose my second project as a .NET Standard 2.1 project.
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>10.0</LangVersion>
</PropertyGroup>
After updating Visual Studio to version 17.4.0 preview 6 (and making sure the class was marked "partial"), it now works.

Mapbox Navigation Xamarin Bindings - AAR errors "does not implement interface IComparator.Compare"

I have created all my .gradle files (.jar and .aar) through Android Studio.
The .jar files compile to .dll files through the android bindings in Xamarin without any issues - and I can expand them in object explorer Visual Studio when including them as references.
My problem is with the .aar files.
I have the mapbox-android-core-1.4.0.aar file from the gradle in my Jars folder in Visual Studio.
The build action is set to "LibraryProjectZip" and under references I have included Xamarin.Android.Support.v7.AppCompat which then included all other android support dependencies.
The Android Compile version is Android 9.0
Android class parser: class-parse
Android codegen target: XAJavaInterop1
When I build i get the following error:
Error CS0535 'FileUtils.LastModifiedComparator' does not implement interface member 'IComparator.Compare(Object, Object)'
The official Xamarin troublehsooting says I must add the managed return to metadata which I did as follows:
<attr path="/api/package[#name='com.mapbox.android.core']/class[#name='FileUtils']/method[#name='FileUtils.LastModifiedComparator']" name="managedReturn">java.lang.Object</attr>
With this added it still has exactly the same error, so I am not sure where I am going wrong.
Do I need to add a partial class to Additions or is the attr> above sufficient - just using the wrong information inside it, or am I missing what the error is in the first place?
Error CS0535 'FileUtils.LastModifiedComparator' does not implement interface member 'IComparator.Compare(Object, Object)'
To fix this issue, try to add a partial class declaration for 'LastModifiedComparator' class and explicitly implement IComparator.Compare(Object, Object):
public partial class DeviceService
{
public void Compare(object a, object b)
{
...
}
}
In your case, the method name in <attr ...> tag should be Compare instead of the 'LastModifiedComparator' method.
Check the tutorial:
https://learn.microsoft.com/en-us/xamarin/android/platform/binding-java-library/troubleshooting-bindings#possible-causes-6
Similar issue:
error CS0535 Class does not implement interface member

Xamarin Android Layout Resources in library project not included in Application

I have a Xamarin Android library project which is referenced by an Android application project.
The library project has layout files which need to be used by a library project component. The Resource.designer.cs file seems to be generated properly. The layouts are marked as Android Resources. Yet at runtime the resources are not there. Trying to access them just returns 0.
After reading all the SO questions on the topic, I am convinced this should work, but so far it doesn't. Ideas?
Create the Android Class Library.
Create a layout folder in this class library and create a layout in this folder.
Add the code in your xamarin android app.
var btn_Load = FindViewById<Button>(Resource.Id.btn_LoadLibraryLayout);
btn_Load.Click += delegate
{
SetContentView(ClassLibrary1.Resource.Layout.library_layout);
};
I create a button to open the layout in class library.
Result:
You could download from the ClassLibrary_layout folder of GitHub for reference.
https://github.com/WendyZang/Test.git
Updated:
If you want to get the resource identifier, you need the package name of this class library. First, we could not get the package name in AndroidManifest.xml file or properties, because the class library does not have it.
You could get it from the steps below.
Create a activity in class library. And get the package name with the code.
var PackageNname = Application.Context.PackageName;
You could also get the resource identifier in this activity.
var resId2 = Resources.GetIdentifier("library_layout", "layout", PackageNname);
Result:
The paskage name is same to the app1 I provided.
Package Name: com.companyname.app1
You could also use the code directly in app1 activity.
int resId2 = Resources.GetIdentifier("library_layout", "layout", "com.companyname.app1");

How to use System.Security.Cryptography in Xamarin.Forms PCL

By reference to this link https://developer.xamarin.com/api/namespace/System.Security.Cryptography/
May I know how do I include this in my Xamarim.Forms PCL project? When I include in, Visual Studio is giving error as the picture below
May I know if anybody has any idea how to solve this? Thanks.
The namespace is available both in Xamarin.iOS and Xamarin.Android. You could make platform specific implementations for both platforms and then resolve them with the DependencyService
You would have your interface for whatever you need in your PCL
public interface ICryptoService // or whatever
{
string Cipher(string stringToCipher);
string Decipher(string stringToDecipher);
}
and then implement these in your platform specific projects
using System.Security.Cryptography;
namespace MyApp.Droid
{
public class CryptoService : ICryptoService
{
// implement interface
}
}
To make the implementation visible to DependencyService you have to use the DependencyAttribute
[assembly: Xamarin.Forms.Dependency(typeof(MyApp.Droid.CryptoService)]
You can now obtain an instance in your PCL with
var cryptoService = DependencyService.Get<ICryptoService>();
and then use it. The steps for iOS are basically the same. For UWP you have to register the implementation manually, see here.
Edit:
Since it's likely that the implementation will be the same for all platforms, you could introduce a shared project and put the implementation there. All you have to do now is referencing the shared project from your iOS and Android projects.
Edit 2:
Adding a shared project to an existing Xamarin.Forms solution is quite easy. Just right-click your solution in VS, choose Add -> New Project... (I only have a german localized VS at hand at the moment, but it should be something in the lines of that). Now select Shared Project, give it a name and click OK, there will be a new shared project in your solution.
Now right-click your platform specific project and choose Add -> Reference.... The window to add a reference should open and on the left you can choose the source of the reference (Assemblys, Projects, Shared Projects, COM and Browse). Select Shared Project and then the project you just created. Any code file in your shared project will now be compiled with your platform specific project as if the code file was in the platform specific project (watch for namespaces!). Repeat for the other platform specific projects.

How to migrate a Xamarin.Forms+Prism app to DryIoC from Unity?

I'm trying to change the container to DryIOC of a Xamarin.Forms + Prism app.
Visual studio is compiling and starting the app without errors, but when the app starts it doesn't fire App.OnInitialized method keeping the app on a blank screen.
What did I do?
Removed Prism.Unity, Unity and Microsoft related packages from the Android and Shared projects
Added DryIoc.dll (v2.10.7) and Prism.DryIoc.Forms (v6.3.0.1) packages to Android and Shared projects
Changed prism xmlns in App.xaml pointing to:
xmlns:prism="clr-namespace:Prism.DryIoc;assembly=Prism.DryIoc.Forms"
Changed the AndroidInitializer class in MainActivity.cs to:
public class AndroidInitializer : IPlatformInitializer
{
public void RegisterTypes(IContainer container)
{}
}
I created a blank app using the Prism DryIoC template and it works fine. Also, I compared the App.xaml, App.xaml.cs and MainActivity.cs files with those in my project and everything is OK.
What else can I do?
If you're converting over you'll need to do the following:
1) Remove the following three packages
- Prism.Unity.Forms
- Unity
- CommonServiceLocator
2) Update the xml namespace to clr-namespace:Prism.DryIoc;assembly=Prism.DryIoc.Forms in your App.xaml
3) Remove the Unity namespaces from your App.xaml.cs and replace them with Prism.DryIoc and DryIoc. Do the same in any IPlatformInitializer implementations, also changing IUnityContainer to IContainer
4) Update any service registrations since the signatures do not match between Unity and DryIoc.
5) Update any services or anything you specifically have that may directly use IUnityContainer to use IContainer.
6) Delete your obj and bin folders, clean and rebuild.
If you run into any trouble you can compare your project to either the Hamburger Menu or Tabbed Navigation samples.
UPDATE
Starting in Prism 7.2 the XML Namespace that is recommended for use is simply http://prismlibrary.com this will replace any clr-namespace declaration that you may have had previously and will work regardless of whether you are using an official Prism package or one of the Extended versions of Prism.
What worked for me was creating a new project using Prism DryIoC template and moving all my code it.

Resources