如何根据属性值更新设计时 UserControl 界面? [英] How to update design-time UserControl interface based on property value?

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

问题描述

我创建了一个具有以下公共属性的 UserControl:

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

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

pnlLookupTable 是一个面板控件,您可能已经猜到了.我可以在属性"窗口中更改 Text 属性的值,它会像应有的那样反映在标记中.但是,页面内 UserControl 的设计视图不会显示面板的更新 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>

为了保护代码的专有性质,我更改了一些标识符和其他琐碎的东西.

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

我还应该重申,我正在查看添加了控件的设计模式的 Web 表单,并且我正在更改 Text 属性.我想在设计器中直观地看到 Panel 的 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 代码...

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 文件中的代码永远不会在 Visual Studio 中编译或运行.这是为了防止由于在 CLR 中无法卸载已加载的程序集而导致的内存泄漏.为了在您的用户控件中运行代码,Visual Studio 必须将您的 ASCX 编译成一个 DLL,然后加载它,然后运行代码.每次对 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.

由于 CLR 中的这一限制,用户控件设计器实际上并不编译或运行 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. 您可以编写从 Control 派生的常规自定义控件,而不是使用 ASCX 用户控件,并将代码编写在 CS 或 VB 文件中.
  2. 将 ASCX 编译成 DLL.David Ebbo 写了一篇关于如何执行此操作的博文.

这两个解决方案应该起作用的原因是它们都涉及将代码编译成 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).

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

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