You can create an object of the existing type (or custom type in the user project) in the. NET base class library in the resource. WhenProgramThey are automatically instantiated at runtime.
Example:
Create a student class first.
Public Class Student
{
Public String Name
{
Get ;
Set ;
}
Public Bool Isfemale
{
Get ;
Set ;
}
Public Override String Tostring ()
{
If (Isfemale)
Return Name + " : Female " ;
Else
Return Name + " : Male " ;
}
Public Static String Staticinformation = " I am a static field defined by student type! " ;
}
In the main form, first introduce the namespace where the student class is located, and define a "Local:" prefix for it;
Xmlns: Local = "CLR-namespace: usecustomizeclass"
Then, create a student object in the resource and initialize its attributes: (The following is the XAML of WPF, And the SL class is the same)
<Window. Resources>
<Local: StudentName= "James"Isfemale= "False"X: Key= "Student1"/>
</Window. Resources>
Now you can use this resource in the form.CodeUse the XAML extension to mark "X: static" to access the static field of the student class:
< Stackpanel Margin = "10" >
< Button Margin = "10" Click = "Button_click" > C # access the attributes of the student object saved in the resource using code </ Button >
< Textblock Horizontalalignment = "Center" Text =" {X: static local: Student. staticinformation} " />
</ Stackpanel >
Use the C # code to obtain the reference of the student object and then access its instance attributes: (the WPF sample code is similar to the SL class)
Private VoidButton_click (ObjectSender, routedeventargs E)
{
Student Stu =This. Findresource ("Student1")AsStudent;
If(Stu! =Null)
MessageBox. Show (STU. tostring ());
}