Friday, March 17, 2017

XAF Known Issue - Duplicate Caption for a Business Object class can create 'key already exists in collection' error

DevExpress XAF uses Caption (XpoDisplayNameAttribute) from Business Object attributes. If two business objects have same title/caption/displayname DevExpress Web Dashboard Designer throws an error. Datasource with Id already exists. This is because it tries to add an object with same key in a collection which already contains a Class data source with this name.

Solution is to either disable the class for DevExpress Dashboards Data source by adding the attribute IsVisibileInDashboard(false) on class or provide unique name for each class.

This applies to DevExpress Dashboard (not expand.)

[Code Sample] Retrieve Enum Description Attribute Value via Extension Method

This extension method allows retrieval of enum description attribute value in c#
 public static class EnumExtension
    {
        public static string DescriptionAttr<T>(this T source)
        {
            var fi = source.GetType().GetField(source.ToString());
            var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(
                typeof(DescriptionAttribute), false);
            if (attributes != null && attributes.Length > 0) return attributes[0].Description;
            else return source.ToString();
        }
        public static string GetEnumDescription(this Enum value)
        {
            var fi = value.GetType().GetField(value.ToString());
            var attributes =
                (DescriptionAttribute[])fi.GetCustomAttributes(
                typeof(DescriptionAttribute),
                false);
            if (attributes != null &&
                attributes.Length > 0)
                return attributes[0].Description;
            else
                return value.ToString();
        }
    }

[Code Sample] Image From Resource - IMarkupExtension

This extension allows to set image in XAML from resource images.

1. Extension code
   [ContentProperty("Source")]
    public class ImageResourceExtension : IMarkupExtension
    {
        public string Source { get; set; }
        public object ProvideValue(IServiceProvider serviceProvider)
        {
            if (Source == null)
            {
                return null;
            }
            var imageSource = ImageSource.FromResource("MobileApp.Images." + Source);
            return imageSource;
        }
    }
2. Add namespace
 xmlns:resources="clr-namespace:MobileApp.Resources;assembly=MobileApp" 
3. Usage
 <Image Source="{resources:ImageResource  image.png}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="Fill"  />
    

Xamarin (Android) Title Bar Background Color (Popup Modal and Default Title Bar)

Setting BarBackgroundColor = Color.FromHex() will set the background color for the navigation page correctly however as soon as a page is pushed or a modal is opened that modal will not have the same color applied as above.

In order to change this popup modal color follow these steps:

1. Add Styles.xml (set as AndroidResource) in Resources\values folder in your android project.
2. Create a theme style and specify title background color. See xml below.

<?xml version="1.0" encoding="utf-8" ?>
<resources>
    <style name="CustomActionBarTheme"
           parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>
    <style name="MyActionBar"
           parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">#003A63</item>
    </style>
</resources>

3. Set android application theme.
[Activity(Label = "My Label", Icon = "@drawable/icon", MainLauncher = true,
        Theme = "@style/CustomActionBarTheme",
        )]

Tuesday, March 14, 2017

Xamarin Version Matching Issue with XLabs and Designer Issues

Solution 1. Working on a Xamarin project I found out that if Xamarin Version doesn't exactly match with XLabs installed in the projects it can create hundreds of designer errors. This can be solved by ensuring that XLabs's Major.Minor.Build match with Xamarin libraries. Revision doesn't matter.

Solution 2. If you still face issues after matching, it may be that XLab team has not yet updated/compiled the package using latest xamarin and you can lookup/create a polyfill for missing resources.

Solution 3. Ensure that all packages are up to date and unnecessary old android sdks are not installed in Android SDK manager.

Worked for me after these changes above.

More to be discovered on this. Feel free to post any issues you may face after applying those fixes above.