Categories
.NET Programming

Using .NET COM component in ASP (classic) application

Recently I had to reinstall an old ASP (classic, not ASP.NET) application which used COM components which were made in .NET.
Unfortunately the components could not be instantiated (via Server.CreateObject) and I would always get an error 80131700.
The solution was that you need to enable the .NET CLR on your Application Pool even though your are not normally using the .NET runtime.
Hopefully this helps someone else who has the same problem.

Categories
.NET Programming Xamarin Forms

Using accent color as ListView selected cell color (Xamarin Forms Android)

The Xamarin Forms ListView control displays a list of items and can also indicate currently selected item. However, if you modified the base colors of your Android application the background color of the currently selected item is not matching the Android accent color.
This can be achieved by a custom cell renderer which will set the background color of the list view selected Cell to the accent color in case that the Cell is selected.

Here is the code:

public class CustomCellRenderer : ViewCellRenderer
{

    private Dictionary _cells = new Dictionary();

    protected override global::Android.Views.View GetCellCore(Cell item, global::Android.Views.View convertView, ViewGroup parent, Context context)
    {
        // Generate the View for the cell
        var view = base.GetCellCore(item, convertView, parent, context);
        bool isSelected;

        // Determine if the cell is selected by checking if the owner ListView SelectedItem property matches the cell BindingContext
        // Unfortunately although the cell internally tracks if it is selected or not, we can not access that information because its private
        Xamarin.Forms.ListView lv = item.Parent as Xamarin.Forms.ListView;
        if (lv != null)
            isSelected = lv.SelectedItem == item.BindingContext;
        else
            isSelected = false;

        // Store the current cell selection status
        _cells[item] = (view, isSelected);

        // Set the color of the underlying view immediately
        SetColor(view, isSelected);
        return view;
    }

    protected override void OnCellPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnCellPropertyChanged(sender, e);
        if(e.PropertyName == "IsSelected")
        {
            // Unfortunately although the cell internally tracks if it is selected or not, we can not access that information because its private
            // However we can catch the firing of the event when the cell IsSelected property changes
            var item = _cells[(Cell)sender];
            // Set the color of the underlying view
            SetColor(item.view, !item.selected);
            // Invert the selected flag
            _cells[(Cell)sender] = (item.view, !item.selected);
        }
    }

    private void SetColor(global::Android.Views.View view, bool isSelected)
    {
        global::Android.Graphics.Color backgroundColor;
        if (isSelected)
        {
            // Use the accent color if the cell is selected
            backgroundColor = new global::Android.Graphics.Color(global::Android.Support.V4.Content.ContextCompat.GetColor(view.Context, Resource.Color.colorAccent));
        }
        else
        {
            backgroundColor = global::Android.Graphics.Color.Transparent;
        }
        view.SetBackgroundColor(backgroundColor);
    }

}
Categories
.NET .NET Core Programming

TIL: Double.Equals works with Double.NaN values

The .NET standard defines that the double value of NaN (Double.NaN) will never be equal to any other Double value, including itself.
In other words:

double first = Double.NaN;
double second = Double.NaN;
bool equal = first == second; // false;

However this does not hold for Double.Equals. Double.Equals checks for object 'Identity' and not object 'Equality'.

double first = Double.NaN;
double second = Double.NaN;
bool equal = first.Equals(second); // true;

This is in general a nice thing if you have a generic methods which rely on object equality. Object.Equals will work even for Double and Single types.

Categories
.NET Programming

Mixing string verbatim (@) and string interpolation ($) characters

It is possible to combine the string verbatim (@) and string interpolation ($) characters when entering string literals in your C# code. In order to do that you must first use the $ character followed by @ character.
Example:

int id = 1;
var result = await _con.QueryAsync(
                $@"select *
                    from   A
                    where A.ID = {id}");
Categories
.NET

Created small note taking application – introducing OP.Notes

I have spent few days to create a small note taking application as I really needed something more then just text editor (Notepad++ is a fine application) to add notes, have simple TODO lists etc. I've placed the app on Codeplex as other people might find it usefull as well: OP.Notes. Here is a screenshot of how it looks like:

OP.Notes