WPF中是否存在form.onload? [英] Does form.onload exist in WPF?

查看:75
本文介绍了WPF中是否存在form.onload?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在WPF中加载表单的一些代码.是否有可能做到这一点?我找不到在哪里为表单onload编写代码.

I would like to run some code onload of a form in WPF. Is it possible to do this? I am not able to find where to write code for form onload.

从下面的回答来看,似乎我要问的不是WPF中通常要做的事情吗?在Vb.Net winforms中,这很容易,您只需转到onload事件并添加需要在加载时运行的代码.无论出于何种原因,在C#WPF中似乎都很困难,或者没有标准的方法可以做到这一点.有人可以告诉我最好的方法是什么吗?

Judging by the responses below it seems like what I am asking is not something that is typically done in WPF? In Vb.Net winforms it is easy, you just go to the onload event and add the code that you need ran on load. For whatever reason, in C# WPF it seem very difficult or there is no standard way to do this. Can someone please tell me what is the best way to do this?

推荐答案

您可以订阅Window的

You can subscribe to the Window's Loaded event and do your work in the event handler:

public MyWindow()
{
  Loaded += MyWindow_Loaded;
}

private void MyWindow_Loaded(object sender, RoutedEventArgs e)
{
  // do work here
}

或者,根据您的情况,您也许可以在 OnInitialized .有关两者之间差异的讨论,请参见Loaded事件文档.

Alternatively, depending on your scenario, you may be able to do your work in OnInitialized instead. See the Loaded event docs for a discussion of the difference between the two.

这篇关于WPF中是否存在form.onload?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆