WPF에 form.onload가 존재합니까?
WPF에서 폼의 코드를 로드하고 싶습니다.이거 할 수 있어요?폼의 온로드용 코드를 어디에 써야 하는지 찾을 수 없습니다.
아래 답변으로 판단하건대, 제가 묻고 있는 것은 보통 WPF에서 행해지는 것이 아닌 것 같습니다.Vb.Net winforms에서는 쉽게 실행할 수 있습니다.온로드 이벤트로 이동하여 로드 시 실행해야 하는 코드를 추가합니다.어떤 이유로든 C# WPF에서는 매우 어려워 보이거나 표준적인 방법이 없습니다.누가 제일 좋은 방법이 뭔지 말해줄래?
Window의 Loaded 이벤트에 가입하여 이벤트 핸들러에서 작업을 수행할 수 있습니다.
public MyWindow()
{
Loaded += MyWindow_Loaded;
}
private void MyWindow_Loaded(object sender, RoutedEventArgs e)
{
// do work here
}
또는 시나리오에 따라 OnInitialized에서 대신 작업을 수행할 수 있습니다.두 가지 차이점에 대한 자세한 내용은 로드된 이벤트 문서를 참조하십시오.
Window의 Loaded 이벤트를 사용합니다.이것은, 다음과 같이 XAML 로 설정할 수 있습니다.
<Window x:Class="WpfTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Your App" Loaded="Window_Loaded">
Window_Loaded 이벤트는 다음과 같습니다.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// do stuff
}
이 질문은 4년 전에 나왔지만, 이 답변은 다른 사람에게도 도움이 될 수 있습니다.-> 간단하고 빠르게 하기 위해서는 코드 뒤에 메서드로 실행하고 싶은 코드를 입력합니다.그 전에 메서드를 호출하기만 하면 됩니다.MainWindow() InitializeComponent()이는 위험을 수반하지만 대부분의 경우 컴포넌트가 창 시작/표시 전에 로딩되기 때문에 작동합니다.(이것은 제 프로젝트의 작업 코드입니다.)앱이 부팅되면 단파 파일을 재생하려고 합니다.이렇게 생겼을 거예요.
using ...
using System.Windows.Media;
namespace yourNamespace_Name
{
/// sumary >
/// Interaction logic for MainWindow.xaml
/// /sumary>
public partial class MainWindow : System.Windows.Window
{
public MainWindow()
{
/*call your pre-written method w/ all the code you wish to
* run on project load. It is wise to set the method access
* modifier to 'private' so as to minimize security risks.*/
playTada();
InitializeComponent();
}
private void playTada()
{
var player = new System.Media.SoundPlayer();
player.Stream = Properties.Resources.tada;
// add the waveFile to resources, the easiest way is to copy the file to
// the desktop, resize the IDE window so the file is visible, right
// click the Project in the solution explorer & select properties, click
// the resources tab, & drag and drop the wave file into the resources
// window. Then just reference it in the method.
// for example: "player.Stream = Properties.Resources.tada;"
player.Play();
//add garbage collection before initialization of main window
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
이것이 검색하시는 분들에게 도움이 되기를 바랍니다. :-)
Loaded프로젝트 구축 후 이벤트가 발생합니다.전에 할 일을 하려면, 오븐을 켜면 된다.OnStartup에 있어서의 방법.App.xaml.cs.
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
//...
base.OnStartup(e);
}
}
언급URL : https://stackoverflow.com/questions/2619348/does-form-onload-exist-in-wpf
'programing' 카테고리의 다른 글
| 애플리케이션 통찰력에 404개의 응답을 무시하도록 지시하는 방법 (0) | 2023.04.23 |
|---|---|
| C# 코드에서 WPF 텍스트 상자의 배경색 설정 (0) | 2023.04.18 |
| 모든 브랜치를 끌어당기고 푸시하도록 git 설정 (0) | 2023.04.18 |
| 단일 PowerShell 스크립트 매개 변수에 여러 값 전달 (0) | 2023.04.18 |
| 다양한 커밋을 선택하여 다른 브랜치로 결합하려면 어떻게 해야 합니까? (0) | 2023.04.18 |