DateTime in ASP.Net Core at Azure
I did the ASP.Net Core MVC web app in Azure. I found all DateTime values are displayed in US format. Even I set in Startup.cs set DefaultRequestCulture to be en-au. It does not work. Finally, I used to use DisplayFormat attribute in The object.
Code
[DisplayFormat(DataFormatString = @"{0:dd\/MM\/yyyy}", ApplyFormatInEditMode = true)] | |
public DateTime? DateTime { get; set; } |
That works in display form. Moreover, the input field will display value in this format.
Nest Objects in View Model in ASP.Net MVC Core
I started to use ASP.Net Core in our web development. The first challenge I faced is about the nested View Model.
I have not used much in nested view model in the previous of MVC,
I got the View Models like this:
Code
public Order{ | |
public int Id {get;set;} | |
public Customer Customer {get;set;} | |
} | |
| |
public Customer{ | |
public string FirstName {get;set;} | |
public string LastName {get;set;} | |
} |
If I wish to do the binding in the razor html
You have to add the bind in the controller
Code
public async Task add the binding in the Customer class too | |
|
mc_code_odd">[Bind("FirstName,LastName")]
public Customer{
public string FirstName {get;set;}
public string LastName {get;set;}
}
[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.
[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.