Silverlight lacks the My namespace, but for those who are addicted to the My keyword, it can still be achieved manually.
-
Open windows explorer and navigate to your project
-
Add a new file MyExtension.vb to this folder (location and filename are optional)
-
Include it in your project
-
Add the below content to it
Although the Info, Computer, and many other features are missing from the Silverlight runtime, you could use alternatives from System.Reflection etc.
Here is a MyExtension.vb sample, you can go and add more functionality as you wish.
The truth is that I never had the chance to test it at all. I only ran the program and checked if the UserControls were created, I would appreciate any comments.
Option Explicit On
Option Strict On
Imports System.Diagnostics.CodeAnalysis
Imports System.Security.Principal
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Runtime.CompilerServices
Imports System.Runtime.InteropServices
Namespace My
<HideModuleName()>
Friend Module My
''' <summary>
''' Gets the <see cref="System.Windows.Application"/> object for the current application.
''' </summary>
<SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")>
Public ReadOnly Property App As App
Get
Return DirectCast(App.Current, App)
End Get
End Property
''' <summary>
''' Gets the context that is registered as a lifetime object with the current application.
''' </summary>
<SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")>
Friend ReadOnly Property Context As WebContext
Get
Return WebContext.Current
End Get
End Property
''' <summary>
''' Returns information for the current user. If you wish to run the application with the current
''' Windows user credentials, call My.User.InitializeWithWindowsUser().
''' </summary>
<SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")>
Friend ReadOnly Property User() As IPrincipal
Get
Return Context.User
End Get
End Property
#Region "UserControls"
Private s_UserControls As New ThreadSafeObjectProvider(Of MyUserControls)
''' <summary>
''' Returns the collection of Windows defined in the project.
''' </summary>
<SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")>
Friend ReadOnly Property UserControls() As MyUserControls
<DebuggerHidden()>
Get
Return s_UserControls.Current
End Get
End Property
<EditorBrowsable(EditorBrowsableState.Never)>
<MyGroupCollection("System.Windows.Controls.UserControl", "Create__Instance__", "Dispose__Instance__", "My.MyExtenstionModule.UserControls")>
Friend NotInheritable Class MyUserControls
<DebuggerHidden()>
Private Shared Function Create__Instance__(Of T As {New, UserControl})(ByVal Instance As T) As T
If Instance Is Nothing Then
If s_UserControlBeingCreated IsNot Nothing Then
If s_UserControlBeingCreated.ContainsKey(GetType(T)) = True Then
Throw New InvalidOperationException("The UserControl cannot be accessed via My.UserControls from the UserControl constructor.")
End If
Else
s_UserControlBeingCreated = New Dictionary(Of Type, Object)
End If
s_UserControlBeingCreated.Add(GetType(T), Nothing)
Return New T()
s_UserControlBeingCreated.Remove(GetType(T))
Else
Return Instance
End If
End Function
<SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")>
<DebuggerHidden()>
Private Sub Dispose__Instance__(Of T As UserControl)(ByRef instance As T)
instance = Nothing
End Sub
<DebuggerHidden()>
<EditorBrowsable(EditorBrowsableState.Never)>
Public Sub New()
MyBase.New()
End Sub
<ThreadStatic()>
Private Shared s_UserControlBeingCreated As Dictionary(Of Type, Object)
<EditorBrowsable(EditorBrowsableState.Never)>
Public Overrides Function Equals(ByVal o As Object) As Boolean
Return MyBase.Equals(o)
End Function
<EditorBrowsable(EditorBrowsableState.Never)>
Public Overrides Function GetHashCode() As Integer
Return MyBase.GetHashCode
End Function
<SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")>
<EditorBrowsable(EditorBrowsableState.Never)>
Friend Overloads Function [GetType]() As Type
Return GetType(MyUserControls)
End Function
<EditorBrowsable(EditorBrowsableState.Never)>
Public Overrides Function ToString() As String
Return MyBase.ToString
End Function
End Class
#End Region
<EditorBrowsable(EditorBrowsableState.Never), ComVisible(False)>
Friend NotInheritable Class ThreadSafeObjectProvider(Of T As New)
Friend ReadOnly Property Current As T
<DebuggerHidden()>
Get
If m_ThreadStaticValue Is Nothing Then m_ThreadStaticValue = Activator.CreateInstance(Of T)
Return m_ThreadStaticValue
End Get
End Property
<ThreadStatic()>
Private Shared m_ThreadStaticValue As T
End Class
End Module
End Namespace