WPF,Bad实践把代码直接放在InitializeComponent后面? [英] WPF, Bad practice to put code directly after InitializeComponent?

查看:128
本文介绍了WPF,Bad实践把代码直接放在InitializeComponent后面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道什么。我有一个加载页面的框架,目前每个页面都有一个 Page_Loaded 方法,每次访问页面时都会运行。这是工作很好,但我注意到错误,如果我使用导航去到以前访问的网页。返回页面后, Page_Loaded 正被重新调用,我不想要。

I was just wondering about something. I have a frame that loads pages and currently each page has a Page_Loaded method that will run each time the page is accessed. This is working great, but I am noticing errors if I use the navigation to go to previously visited pages. Upon returning to a page, Page_Loaded is being called again which I do not want.

使用调试,我注意到 InitializeComponent 只是在第一次实现页面时调用,想知道我是否可以简单地把 Page_Loaded 这个调用像这样:

Using debugging, I noticed that InitializeComponent was only getting called the first time the page is implemented and wondered if I could simply put my Page_Loaded code after this call like so:

public partial class MyPage: Page
{
    public MyPage()
    {
        InitializeComponent();
        //======> To Here
    }

    private void Page_Loaded(object sender, RoutedEventArgs e)
    {
       //Put Code from here <======
    }
}

这将解决我的问题,但是一个坏的做法?

This would solve my problem but is a bad practice? And if so, what problems might I encounter down the road?

感谢,
Kohan

Thanks, Kohan

推荐答案

在构造函数中做一些事是合法的。我认为这很好。

It's legitimate to do something in the constructor. I think this is fine.

WPF在对象的可访问性方面不是很像ASP.NET。它有点松懈,所以在构造函数不是在ASP.NET中引用的是无。

WPF isn't quite like ASP.NET in terms of accessibility of objects / etc. It's a bit looser and so doing something in a constructor isn't quote the no-no it is in ASP.NET.

这篇关于WPF,Bad实践把代码直接放在InitializeComponent后面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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