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.
Related
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.
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.
I've made a big library to use with all my Xamarin.Forms projects. This lib defines styles, shares reusable code fragments, and so on. Now because I use FontAwesome multiple times in different projects, I want to share the .ttf-File with the library.
Actually the font is stored in my UWPs Assets/Fonts/ folder and is loaded like this:
<On Platform="UWP" Value="Assets/Fonts/fontawesome-webfont.ttf#FontAwesome" />
This works quiet good, and no need to implement a custom rednerer (at least for UWP).
The Problem is that the Font does not seem to get loaded, when I define it as a EmbeddedResource, so I don't know how to load it from the library Project.
Maybe someone did such a thing ever before and could me tell the solution. Thanks!
Edit:
I recreated a project, but I cannot load the Font-File without installing it on my Computer. You can download the Project here: https://github.com/DirtyNative/FATest
To make the font usable, set its build action to Content and you can refer to it as:
FontFamily="ms-appx:///LibraryName/Path/To/Font.ttf#Name"
It seems UWP is unable to load embedded resources from XAML, or at least I haven't yet found a way which would work.
I'm trying to accomplish the .aar file binding to the Xamarin Android project.
I used Bindings Library (Android) project, include my aar to there and build. Everything went Ok.
In Xamarin Android project I added a reference to the generated .dll
It appeared to have only a couple of interfaces exposed but the aar has tons of classes surfaced for sure (I can see them in Android Studio).
Where are the other classes and members?
What I did:
I modified my Metada.xml as following (to expose private declarations and suppress obfuscation). Please ignore its "(" brackets, it is for correct display.
<metadata>
<attr path="/api/package[#name='com.android.biomini']/class[#name='BioMiniAndroid']"
name="visibility">public</attr>
<attr path="/api/package[#name='com.android.biomini']/class[#name='BioMiniAndroid']"
name="obfuscated">false</attr>
</metadata>
It did not help. How to make it show me all classes and interfaces the .aar has?
Thanks.
You are on a correct path to fix these issue by making the types public.
There are bunch of reasons why JAR file classes/member are not accessible in the Xamarin.Android project, you can read about there reasons and possible solutions here
I recommend reading JAR binding documentation for better understanding
All that had been read inside out. No solution found.
The problem appeared to be a Xamarin bug. Xamarin recommendation is not to reference it to a binding project but copy .dll to somewhere aside and reference to there (via "Browse" referencing, not "Solution" one). Even though it is awkward it works. It is a viable solution for now.
Thanks.
Since moving on to a new PC I've been having trouble getting a project to run on it, specifically the problem is with some of the controls from the WPFToolkit.
On my previous set up I'd downloaded WPFToolkit and unblocked the DLL's and they'd worked (after some problems). However, this time I downloaded them via NuGet and they aren't blocked where they are stored in the packages/Extended.Wpf.Toolkit.1.9.0/lib directory.
However, I'm now getting errors like :
The name "AutoCompleteBox" does not exist in the namespace "http://schemas.xceed.com/wpf/xaml/toolkit".
or
The type reference cannot find a public type named 'AutoCompleteBox'.
I'm referencing the files in the XAML with :
xmlns:controls="http://schemas.xceed.com/wpf/xaml/toolkit"
From all the research I've done on this problem that seems the right way to do it and it worked when I was on my previous machine.
Is there something obvious I'm doing wrong here?
AutoCompleteBox is a component of the "original" WPF Toolkit which is also available on NuGet under the simple name WPF Toolkit.
The Extended WPF Toolkit Community Edition is a completely different control library developed by Xceed. The controls from the original WPF Toolkit are not included in the Extended WPF Toolkit.
In your project, simply remove the NuGet reference to the Extended WPF Toolkit and instead add a reference to WPF Toolkit.
To access the AutoCompleteBox in your XAML file, add the following namespace reference:
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"