如何更新基于属性值设计时用户控件界面? [英] How to update design-time UserControl interface based on property value?

查看:116
本文介绍了如何更新基于属性值设计时用户控件界面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了下面的公共财产一个用户控件:

I've created a UserControl with the following public property:

[Browsable(true)]
public string Text
{
    get { return pnlLookupTable.GroupingText; }
    set { pnlLookupTable.GroupingText = value; }
}

pnlLookupTable是,你可能已经猜到了,一个Panel控件。我可以改变在属性窗口中的Text属性的值,它反映的标记喜欢它应该是。然而,一个页面中用户控件的设计视图中不显示面板更新GroupingText。我怎样才能做到这一点?

pnlLookupTable is, as you may have guessed, a Panel control. I can change the value of the Text property in the Properties window and it's reflected in the markup like it should be. However, the design view of the UserControl inside a page does not show updated GroupingText for the Panel. How can I get this to happen?

修改

根据要求,这里是它的财产属于整个类。你可以看到有什么特别的:

By request, here is the entire class to which that property belongs. You can see there's nothing special going on:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class LookupTable : System.Web.UI.UserControl
{
    [Browsable(true)]
    public string Text
    {
        get { return pnlLookupTable.GroupingText; }
        set { pnlLookupTable.GroupingText = value; }
    }
}

和这里的.ascx文件的相关部分:

And here's the relevant part of the .ascx file:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="LookupTable.ascx.cs" Inherits="LookupTable" %>
<asp:Panel ID="pnlLookupTable" runat="server" GroupingText="Lookup Table">
    <%-- Irrelevant content here. --%>
</asp:Panel>

我已经改变了一些标识符和其他琐碎的事情来保护code的专有性。

I have changed some identifiers and other trivial things to protect the proprietary nature of the code.

我还要重申,我期待在设计模式,Web表单有我的控制,并将我改变了Text属性。我想直观地看到在设计师的变化小组的GroupingText。

I should also reiterate that I'm looking at a Web form in design mode that has my control added, and I'm changing the Text property. I want to see the change to the Panel's GroupingText visually in the designer.

推荐答案

我不得不想回位到UserControlDesigner code ...

I'd have to think back a bit to the UserControlDesigner code...

简单地说:我不认为这是可能的。

Short story: I don't think it's possible.

和这里的很长的故事:

从我记得,位于ASCX文件的用户控件从不在设计运行。也就是说,ASCX或ASCX.CS文件内的code为从未编译或在Visual Studio中运行。这是造成的事实,即在CLR不能卸载已加载的程序集prevent内存泄漏。为了运行在你的用户控件code,Visual Studio中必须编译ASCX成DLL,然后加载它,然后运行code。您更改到ASCX每一次,它会再次执行此操作。每当这个操作发生更多的内存将从您的ASCX产生的额外加载的DLL被消耗掉。

From what I recall, User Controls located in ASCX files are never run in the designer. That is, the code inside the ASCX or ASCX.CS file is never compiled or run in Visual Studio. This is to prevent memory leaks caused by the fact that in the CLR you cannot unload assemblies that you have loaded. In order to run the code in your User Control, Visual Studio would have to compile your ASCX into a DLL, then load it, and then run the code. Every time you make a change to the ASCX, it would have to perform this operation again. Every time this operation happens more memory will be consumed by the additionally loaded DLL generated from your ASCX.

。相反,它解析ASCX文件,并查找其内部控制和加载这些控件来代替。对于找到的ASCX文件中的每个控制它会创建相关的控制设计者和呈现控件的设计时HTML。

Because of this limitation in the CLR the User Control designer doesn't actually compile or run the ASCX file. Instead, it parses the ASCX file and looks for controls inside it and it loads those controls instead. For each control it finds in the ASCX file it will create the associated control designer and render that control's design time HTML.

有几个方法可以解决此问题:

There are a couple of ways to work around this:


  1. 而不是使用ASCX用户控件,您可以编写从控制和code派生被写在CS或VB文件定期自定义控件。

  2. 编译ASCX成DLL。大卫Ebbo写一篇关于如何做到这一点的博客文章

  1. Instead of using an ASCX user control you can write a regular custom control that derives from Control and the code is written in a CS or VB file.
  2. Compile the ASCX into a DLL. David Ebbo wrote a blog post on how to do this.

这是这两种解决方案应该工作的原因是,它们都涉及到具有code编译成一个DLL。这个想法是,该DLL不经常改变,因此它是安全的Visual Studio加载DLL而无需重新加载它每次修改DLL(和内存泄漏)的风险。

The reason that these two solutions should work is that they both involve having the code compiled into a DLL. The idea is that the DLL doesn't change very often so it is safe for Visual Studio to load the DLL without risk of having to reload it for each time the DLL changes (and leak memory).

这篇关于如何更新基于属性值设计时用户控件界面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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