[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.
[Example Code]: Simple Validation Framework
Recently, I built a simple validation framework which can work with WPF. I found I can use class attribute and reflection to achieve this purpose.
Firstly, I define a Required Attribute to see whether the property requires a value
Code
[AttributeUsage(AttributeTargets.All)] | |
public class RequiredAttribute : System.Attribute | |
{ | |
public readonly bool Required; | |
| |
public RequiredAttribute(bool required) | |
{ | |
this.Required = required; | |
} | |
| |
} |
Then I add IsValid method in the class, it will use the reflect to get all properties in this class. Then it will check whether the property has "required=true", if that is the case, it will check whether the property has value, if not, it will use an error.
public class PersonModel
{
[Required(true)]
public string Name {get;set;}
public IList IsValid()
{
IList ret = new List();
foreach (var prop in this.GetType().GetProperties())
{
foreach(Attribute attr1 in prop.GetCustomAttributes(typeof(RequiredAttribute),true)){
RequiredAttribute attr = (RequiredAttribute) attr1;
if (attr != null) {
if (attr.Required)
{
if (string.IsNullOrEmpty(Convert.ToString(prop.GetValue(this, null)))) {
ret.Add(string.Format("{0} is required",prop.Name));
}
}
}
}
}
return ret;
}
}
is the best practice. I cannot think another way can do it easier.
Please click here to download the example code.
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;
});
});;
[Xamarin Error]:Exception in thread "main" deployment fail
After I installed Facebook SDK for Andorid. I found I cannot deploy my app in the debug anymore. I clicked the play button in Visual Studio, no compilation errors at all. Visual Studio is only freezed in "Deploying in [Phone Name]" for a little white. Then it stoped and gave out this error message,'Exception in thread "main"'. After I did a number of random search in Google, I found some people in the Xamarin Forum mentioned they need to increase the heap size to 1G after installing Facebook SDK. Finally, I increased the heap size to 1G in (Project Properties -> Android Options->Advanced-> Java Max Heap Size). Then it works!
[Xamarin Error]:System.MissingMethodException: Method 'Android.Support.V4.Widget.DrawerLayout.AddDrawerListener' not found.
After I switched my Xamarin Forms app from Normal Form Activity to FormsAppCompatActivity. This can provide more function in Material Design, such as TextInputLayout. But I got this error:System.MissingMethodException: Method 'Android.Support.V4.Widget.DrawerLayout.AddDrawerListener' not found. I found the problem is from the libraries. After I updated the latest Xamarin Forms Library in Android Project. That works!