简体   繁体   English

如果我有basecontrol,如何阻止我的WPF用户控件加载

[英]How to stop my WPF usercontrol from loading, if i have basecontrol

I have a BaseControl, of which almost all my controls inherit from. 我有一个BaseControl,其中几乎所有的控件都继承自。

Right now I am working on security and I want to block users from viewing certain items if they do not have access. 现在我正致力于安全性,我想阻止用户查看某些项目,如果他们没有访问权限。

I am able to do so on the Loaded event of the base control, but this is too late, this meanse the whole control gets rendered, and then replaced. 我能够在基本控件的Loaded事件上这样做,但这太晚了,这意味着整个控件被渲染,然后被替换。 I would like to replace it before it renders 我想在渲染之前替换它

here is an example of my code: 这是我的代码示例:

public class BaseControl : UserControl
{
    public BaseControl()
    {
        this.Loaded +=BaseControl_Loaded;
    }

    private void BaseControl_Loaded(object sender, RoutedEventArgs e)
    {

        if (!userHasAccess)
        {
            this.Content = new AccessDenied();
        }
    }
}

The above code works perfectly, but a bit too late, Is there a way that I can do this before the Loaded event? 上面的代码工作得很好,但有点太晚了,有没有办法可以在Loaded事件之前做到这一点?

You want to override the OnInitialized where the constructor and properties are being set up before the Render happens. 您想要覆盖OnInitialized ,其中在Render发生之前正在设置构造函数和属性。

protected override void OnInitialized(EventArgs e)
{
  if (!userHasAccess)
  {
   this.Content = new AccessDenied();
  }
  base.OnInitialized(e);
}

More information can be found here . 更多信息可以在这里找到。

Check out following sites: 查看以下网站:

http://msdn.microsoft.com/en-us/library/ms745025(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/ms745025(v=vs.110).aspx

Also check object lifetime events that is what you asking for. 还要检查您要求的对象生命周期事件。

http://msdn.microsoft.com/en-us/library/ms754221(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/ms754221(v=vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM