silverlight XAML 리소스에서 CLR 객체 인스턴스화
예:
먼저 Student 클래스를 생성합니다.
public class Student
{
public string Name
{
get;
set;
}
public bool IsFemale
{
get;
set;
}
public override string ToString()
{
if (IsFemale)
return Name + ": ";
else
return Name + ": ";
}
public static string StaticInformation = " Student !";
}
메인 창에서 먼저 학생 클래스가 있는 이름 공간을 도입하고'local: '접두사를 정의합니다.
xmlns:local="clr-namespace:UseCustomizeClass"
그런 다음 리소스에서 Student 객체를 만들고 속성을 초기화합니다. (이하 WPF의 XAML, SL 클래스)
<Window.Resources>
<local:Student Name=" " IsFemale="False" x:Key="student1"/>
Window.Resources>
이제 창에서 이 리소스를 사용할 수 있습니다. 다음 코드는 XAML 확장 태그인 "x:static"을 사용하여 Student 클래스의 정적 필드에 액세스합니다. <StackPanel Margin="10">
<Button Margin="10" Click="Button_Click">C# Student Button>
<TextBlock HorizontalAlignment="Center" Text="{x:Static local:Student.StaticInformation}" />
StackPanel>
C# 코드로 Student 객체에 대한 참조를 가져와 인스턴스 속성에 액세스할 수 있습니다.(WPF 예제 코드, SL 클래스)
private void Button_Click(object sender, RoutedEventArgs e)
{
Student stu = this.FindResource("student1") as Student;
if (stu != null)
MessageBox.Show(stu.ToString());
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.