Android App crashes when using .NET MAUI RelativeLayout - c#

I am attempting to migrate a Xamarin.Forms app to .NET MAUI, however I have found that this bug also happens on a new .NET MAUI app.
The app that I am attempting to migrate over to .NET MAUI uses a RelativeLayout in a good number of its views, and whenever I attempt to run the app on an android device (I have no iOS devices to test this one, therefore I can't determine if it happens there), the app instantly crashes.
Here are the steps to reproduce:
Create a new .NET MAUI app using .NET 6.0 in Visual Studio 2022.
Replace the contents of the MainPage.xaml with the following:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:cmp="clr-namespace:Microsoft.Maui.Controls.Compatibility;assembly=Microsoft.Maui.Controls"
x:Class="Test_Maui_App.MainPage">
<ScrollView>
<cmp:RelativeLayout>
</cmp:RelativeLayout>
</ScrollView>
</ContentPage>
Replace the contents of MainPage.xaml.cs with the following:
namespace Test_Maui_App;
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
}
Attempt to run the app on an android device with or without a debugger attached.
Observe the behavior of the app crash.
Looking in the device log in ADP doesn't provide any real useful information, unfortunately.
I'm aware that RelativeLayout is on its way out in favor of the other layouts that MAUI supports, but I was wondering if it would be possible for it to still be used (and save myself some time redoing a bunch of XAML lol). If not, what's the best alternative?

AbsoluteLayout and RelativeLayout have been moved to the Compatibility namespace. I tested the code you provided, and it crashed as you described. You can try to use Grid layout. In addition, you can check this: RelativeLayoutPage by official code.
For more information, you can refer to Announcing .NET MAUI Preview 7 by official blog.
You can also raise an issue on GitHub.

Related

Migrate Xamarin Forms Effect to .NET MAUI

For a Xamarin Forms application, I have been using a solution to track touch using code from an example out of Microsoft's Xamarin Forms Sample Project, specifically the Touch Tracking Effect sample.
I took that sample and simplified it down to just directly starting up with the BoxViewDraggingPage.xaml from App.xaml.cs and removing all other XAML files. That compiled and ran fine.
I then created a .NET Maui App with the same BoxViewDraggingPage (and the same TouchActionEventArgs.cs, TouchActionEventHandler.cs, TouchActionType.cs, and TouchEffect.cs) as was in the Xamarin Forms version.
I added the same TouchEffect.cs files that were in the sample for the specific platforms (iOS also has a TouchRecognizer.cs file)
When running it at that point I got the following:
So then I tried following the instructions for Migrating Xamarin.Forms Effects. The explanation for how Xamarin.Forms effects were set up matched perfectly, but it was unclear to me from that page what class the section titled "Effects in .NET MAUI" was replacing since it had a different class name than the previous code snippets earlier in the article. It seemed like it is replacing the FocusEvent class in the shared platform code and then specifies the platform specific code within #if platform sections (#if WINDOWS, #if __ ANDROID __, etc.) and so I am guessing that means the TouchEffect.cs files are no longer needed in the platform specific folders.
I didn't have a chance to find out if that was the case or if it works, because when I try to compile, I get:
I don't find any missing Nuget packages or assemblies when I tried searching. If I click the dot after Platform, I get the following choices, with instructions to use the navigation bar to switch contexts:
But I am already targeting net6.0-windows10.0.19041.0:
so I'm not sure why it says Not Available for that.
So, I am stuck trying to get this to work.
I am looking either for a solution to get the sample code working as it is in the Xamarin.Forms project, but for .NET MAUI (whether that be set up how the sample originally had it or how it is described in the Migrating Xamarin.Forms Effects link) - it could be that I am missing something simple in how to get that Microsoft.Maui.Controls.Compatibility.Platform.UWP piece to compile.
Or, the other possibility is that same link says that .NET MAUI Handlers can also handle this type of scenario. So if someone can show all the pieces that would need to be modified in order to do the same solution but using .NET MAUI Handlers, I am open to that. But it seems that the easiest path, though, should be to get the Effects working in .NET MAUI since everything is already written that way. I am just leaving both options open in looking for a solution.
EDIT:
OK, I figured out why the UWP portion had red squiggles under it. I figured out what "using the navigation bar to switch contexts" in the above screenshot meant. It meant I needed to select the context in the dropdown pictured below:
However, this just moves the squiggles to PlatformEffect and I cannot figure out what to do with the context or anything else to resolve that:
OK, I found out that PlatformEffect now inherits from Microsoft.Maui.Controls.PlatformEffect instead of the separate Platform inheritances shown in the Migrating Xamarin.Forms Effects example. That example is out of date and I didn't realize that and so it was confusing me.

Xamarin CarouselView error: Error inflating class CarouselView

I'm trying to use CarouselView to create a horizontal ListView since a client of mine does not like the vertical ListView I have implemented in their project. I tried to follow several tutorials, but to no avail.
I have a hunch that the issue is with the nuget packages I downloaded or with the namespace I'm using in the xml. I am using Visual studio 2019 in a Xamarin Android App.
Here is a list of nuget packages I downloaded for the Carousel View:
CarouselView
CarouselView.FormsPlugin
CarouselView.FormsPlugin.Fix
Xamarin.Forms v5.0.0.2083
Here is the code in my content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:forms="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hello World!" />
<forms:CarouselView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
All I added here apart from the default page you get when you create a new project is the xmlns:forms line for the namespace to use and the CarouselView tag itself.
In my code behind OnCreate, I have this:
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
CarouselViewRenderer.Init();
SetContentView(Resource.Layout.activity_main);
Again, all I added here was the Init call on the CarouselViewRenderer. I am getting the error "Error inflating class CarouselView" when SetContentView is called.
Please let me know if I'm overlooking something stupid. I can't event get Intellisense to suggest CarouselView in xml, so I'm sure there's something wrong. Maybe it cant find the tag? Which is why I'm thinking it could be a namespace issue.
What I tried:
I tried swapping around the Init call and call it in a different order, but nothing worked.
I looked for questions similar to this in the GitHub forums but no solution seems to apply to my case.
I tried changing the tag name in xml to something like CarouselVie without the 'w' and I get the same error, which means it probably isn't finding that tag to begin with.
I tried changing the namespaces and looking for different nuget packages to download but again nothing worked.
From document What is Xamarin.Forms? ,we know that
Xamarin.Forms is an open-source UI framework. Xamarin.Forms allows
developers to build Xamarin.Android, Xamarin.iOS, and Windows
applications from a single shared codebase.
Xamarin.Forms allows developers to create user interfaces in XAML with
code-behind in C#. These interfaces are rendered as performant native
controls on each platform.
At runtime, Xamarin.Forms utilizes platform renderers to convert the
cross-platform UI elements into native controls on Xamarin.Android,
Xamarin.iOS and UWP. This allows developers to get the native look,
feel and performance while realizing the benefits of code sharing
across platforms.
So, you can use Xamarin Forms controls (CarouselView) in forms'app,and deploy the app to your android devices or ios devices.
From above description you posted, I think you're confused about CarouselView's usage.
Since the CarouselView is a control of xamarin form, you should use it in xamarin forms's app rather than referencing it in Android.
For how to use CarouselView, you can refer to the official document : Xamarin.Forms CarouselView.
And there is a sample(Xamarin.Forms - CarouselView) included in this document, you can download it and deploy it to your android device which will help you understand it better.

Images shows in design mode but not at runtime in WPF .net Core Project

I am using pack uri to get the image. It works well in design mode. The image shows without a problem but nothing appears at runtime. I am working in WPF .net Core application. Basically, I have two projects and I am trying to add the image from another project(same solution). I have added the reference for the project and set the image build action to resource already but still nothing happens.
<Image Source="pack://application:,,,/Test;component/Images/1.png" width="auto" height="auto"/>
This is the mainWindow.xaml code. Test is the name of another project.
The exact same code works without problem in .Net Framework WPF application.
If anyone has this problem How can I reference a resource (png) from a seperate project in the same solution check this post. Basically your project sdk in .csproj file should look like this:
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop>.
I ran into similar issue couple months ago during upgrading WPF applicating from .NET Frw to .NET Core.
Make sure that your resource in VS explorer is set to be in Build Action = "Resource"
and use this syntax as in the example below to referencing it for your XAML Image control:
pack://application:,,,/Resources/Images/gray.png

Can't find the Prism.Navigation.TabbedPages namespace

I'm building a xamarin.forms mobile application with Prism, and want to use INavigationService.SelectTabAsync method. This method, according to the Prism documentation, should be in the Prism.Navigation.TabbedPages namespace but Visual Studio tells me that that no such namespace exists.
I have found absolutely none with a similar issue and when I search for the method name in google I get a measly 7 results back.
I have also tried making a completely new xamarin.forms app through the Prism template pack and still nothing shows up. I even tried installing all the prism Nuget packages and messing around with adding references to the project, but nothing...
This is what the Prism docs tells me to do:
Selecting a Tab
You can select a tab programmatically from within a tab's ViewModel by using >the INavigationService.SelectTabAsync method. In order to use this method, >you must add the Prism.Navigation.TabbedPages namespace to your ViewModel.
using Prism.Navigation.TabbedPages;
async void SelectTab(object parameters)
{
var result = await _navigationService.SelectTabAsync("Tab3");
}
And this seems easy enough but when no "TabbedPages" shows up in Intellisense for me (only the Xaml one) in the Prism.Navigation namespace I'm stuck.
Version details:
Visual studio 2019 16.0.1 Preview 1.0
Visual studio 2017 15.9.5
Try ver7.2.0.1175-ci in myget org.
Add "https://www.myget.org/F/prism/api/v3/index.json" to package source and check "include prerelease".
Please, try to add the TabbedPage using the Prism Template as shown in the attached image. In addition to that, this Video by Brian Lagunas somewhere around 1:06:00 could help.

coding4fun toolkit error only in xaml page

I need to use TimeSPanPicker of coding4fun because I need to set seconds. So I installed the package from nuGet.
The TimeSpanPicker work if I declare it in the C# code, but in xaml I had this error:
i used this namespace with no error:
xmlns:c4fToolkit="clr-namespace:Coding4Fun.Phone.Controls;assembly=Coding4Fun.Phone.Controls"
And the classic silverlight toolkit is work properly.
THanks!
The namespace you are using is wrong (it changed from Coding4Fun.Phone to Coding4Fun.Toolkit since V2 for supporting Windows Store apps).
Here is the one to use:
xmlns:c4fToolkit="clr-namespace:Coding4Fun.Toolkit.Controls;assembly=Coding4Fun.Toolkit.Controls"

Categories