0
votes

I've seen a couple of threads about this on the Internet, but I feel like none of them apply to my situation... Either that, or I just simply haven't understood a thing from them (which is likely).

The thing is: I'm writing a test for a converter method, that "converts" an Enum to an appropriate SolidColorBrush.

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    Brush result = new SolidColorBrush(Colors.White);

    if (value != null)
    {
        switch ((InstallationStatus)value)
        {
            case InstallationStatus.Draft:
                result = new SolidColorBrush(Colors.DarkGray);
                break;
            case InstallationStatus.Ready:
                result = new SolidColorBrush(Colors.Green);
                break;
            case InstallationStatus.Uploaded:
                result = new SolidColorBrush(Colors.Orange);
                break;
            case InstallationStatus.Error:
                result = new SolidColorBrush(Colors.Red);
                break;
            default:
                break;
        }
    }
    return result;
}

It works just fine on its own, when called from the app's view. I also have a test method, that basically does this:

InstallationStatusBrushConverter converter = new InstallationStatusBrushConverter();
object result = converter.Convert(null, null, null, null);

The test method calls Convert, which tries to instantiate the result field, but the UnauthorizedAccessException gets thrown... Does anyone have an idea of what's going on here?

1
I guess the test method is not executed in the UI thread. Anything UI related can only be created/accessed in the UI thread.yasen
Is this test method run on a background thread? Is the error message looks like System.UnauthorizedAccessException: Invalid cross-thread access.?kennyzx
As an aside, is there a specific reason that you are creating new brushes? How will these brushes be disposed so that you don't leak GDI handles? Instead, you can just return the built in brushes: result = Brushes.Red.Chris Dunaway
@ChrisDunaway There is no Brushes class in WP, also there are no GDI handles in WP. It's quite safe to create new Brushes. :)yasen
@yasen - Thanks. I wasn't sure if that was the case for WP or not.Chris Dunaway

1 Answers

0
votes

Can CultureInfo.CurrentCulture ever be null?

Possible source of error - System.Globalization.CultureInfo should not be null. Instead, in function parameters, change culture parameter to System.Globalization.CultureInfo.CurrentCulture