Where is ReadOnly property attribute in UWP?

csharp
winrt
uwp

(Yay) #1

Hi,

I am trying to convert my .NET 4.5 desktop Application from Windows Forms to UWP App, during the process I found out that the compiler complaining about the “ReadOnly” property attribute not being found.

I can’t seem to find ReadOnly attribute on UWP, so not sure if I am supposed to add a reference to something or add some using.

any help would be appreciated.


(jms) #2

ReadOnlyAttribute Doesn’t exist in UWP & WinRT.

As you can see ReadOnlyAttribute Class is defined in system.dll, The .NET core, thus cannot be used in UWP apps, as a general rule everything in .net core is not supported in WinRT & UWP, as UWP targets the .NET framework but not the core itself.

Version Information

.NET Framework
Available since 1.1
Silverlight
Available since 3.0
Windows Phone Silverlight
Available since 7.0

From the quote above you can see ReadOnly attribute is only supported under .Net Framework (core) & Silverlight alone.


(samantha jameson) #3

In Short:

ReadOnlyAttribute Doesn’t exist in UWP & WinRT.

As a workaround you can actually change your property structure, you can get a read-only property like this:

1st Option:

public string MyProperty
{
    /* no setter (set block) */
    get { return _inner_value; }
}

No need for the setter, you can set the _inner_value variable within the same class

2nd Option:

This Option makes the property look read-only for classes other than the current class.

public string MyProperty
{
    /* private setter block, can be used on the same class */
    get { return _inner_value; }
    private set
    { 
        _inner_value = value;
        RaisePropertyChanged("MyProperty");
    }
}

Hope this helps you.


(afree) #4

ReadOnly attribute is simply not supported at WinRT

So I just simply suggest that you avoid using it and since you are converting your code maybe adding a #if..#endif block to detect when compiling for WinRT would do. Other than that simply ignore the ReadOnly attribute.