Category: "Xamarin"
Best way to download in MAUI
Due to the discontinuation of Google Podcasts, I am writing my own Podcast App for Android. I chose C# MAUI because I am a C# Developer, and I have used Xamarin (become a part of MAUI now) for years.
My first problem is the Podcast App; I need to load the RSS (XML) via HTTP. That is so easy. Same as other C# programs. Also, you can use to load any JSON information in different use case
Code
var client = new HttpClient(); | |
var string1 = client.GetStringAsync("https://podcast.rthk.hk/podcast/novamanage.xml").Result; | |
// deserailize |
Wi-Fi debugging in xarmain
I got an old MacBook Pro which only has two USB ports. I used an external keyboard and mouse. Both require USB ports. They left no room to connect a phone for debugging. Recently, I found there is a feature in visual studio. This new feature allows the developers to connect the devices via wifi. This measure can free up USB ports. Moreover, this method can do the debugging wireless, that is more convenient, I can carry the debugging device around and without wires, which can keep my desk cleaner.
Although it said that is wireless, you still need to connect with USB and open the device windows in the XCode. Then you need to check the box to enable wifi debug. After that, that is very easy. You select the device in Visual Studio when the device connected to the same wifi networks.
[Xamarin]Get the Screen Height and convert for Height Request
That is impossible to detect screen size in the Xamarin Form. You have to do in the native Android code.l suggest to do in MainActivity and then put in the static variable in the Forms PCL project like this:
Android.Util.DisplayMetrics displayMetrics = new Android.Util.DisplayMetrics();
this.WindowManager.DefaultDisplay.GetMetrics(displayMetrics);
App.Height=(int)(displayMetrics.HeightPixels/displayMetrics.Density);
Also, the unit of HeightRequest is device-independent units. But I found if you use HeightPixel divide by Density of the screen, then it will convert into the unit for HeightRequest or WidthRequest
[Xamarin-Android] LocationListener
If you want to use RequestLocationUpdates method in LocationManager, you need to add a class which implements ILocationListener to receive the callback from LocationManager. Most of the examples on the internet used the Activity as ILocationListener. That is easy if you choose the implementation in this way. But I wish to build a self-contained class which does not inherit any UI class.
locationManager.RequestLocationUpdates(LocationProviders.GPS, 10, 10, this);
Firstly, I just implemented all methods from the interface. Then I got an invalid casting exception. It is because LocationManager required the class is a sub-class of Java.lang.Object. In the native Android SDK, all classes are sub-class of Java.lang.Object, but we are using Xamarin, that is .Net, not Java. Thus we need to inherit our class to be a Java Object. After I make my class to be a sub-class of Java.lang.The object, I got an exception about the invalid handle. That is because ILocationListener has a property
public IntPtr Handle
I built a property to get from Application Context
Application.Context.Handle
Actually, Handle Property is in the Java.lang.Object. I do not need to build my own, I need to remove the section. Then it works.
Asynchronous method to update UI element in Xamarin
I got an app to call api in my server for loading the data. This make sense to use asynchronous methods for loading the data. I don't want the whole UI is locked for waiting the data. But I found
model.GetData().ContinueWith((items) =>
{
list.ItemSource= items;
});
It won't works. I found the reason is all Asynchronous methods are executed by a different thread. You have to call UI main thread to bind the data to UI.
model.GetData().ContinueWith((items) =>
{
evice.BeginInvokeOnMainThread(() =>
{
list.ItemSource= items;
});
});;