本文介绍了ASP.NET设计时属性编辑器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Visual Studio 2013(2)中为ASP.NET创建丰富的设计时控件。不幸的是,我甚至无法将简单的属性属性作为描述来工作。特别是我需要为Visual PropertyGrid添加自定义编辑器(当您在设计器或asp.net文件源中选择控件并按F4时)。我设法让这个工作1次尝试100次,据我所知完全无关。例如,当我使用几乎相同的代码创建新控件时,将其添加到页面,构建并尝试F4。或者在添加新的完全不相关的属性来控制或交换基类控件之后。即使这项工作,代码,构建和另一个尝试的任何更改打破以前的工作解决方案,你不能回到它,因为它不再工作。据我所知(经过一些研究)你无法调试那些编辑器,开发它就像走在完全黑暗中。



以下代码编译并运行一个浏览器。要测试它,需要添加System.Windows.Forms(?!)和System.Drawing引用。



控制代码:



I am trying to create rich design time control for ASP.NET in Visual Studio 2013(2). Unfortunately I can't get to work even simple property attributes as Description. Especially I need to add custom editor to Visual PropertyGrid (when you select control in designer or source of asp.net file and press F4). I managed to get this working 1 for hundred tries, completely unrelated as far as I can tell. For example when I created new control, with almost the same code, add it to the page, build and try F4. Or after adding new completely unrelated property to control or swapping base class of control. Even when this work, any change to code, build and another try break previous working solution and you cannot go back to it because it do not work anymore. As far as I can tell (after some research) you can't debug those editors, developing it is like walking in complete dark.

Code below compiles and runs in a browser. To test it there is need to add System.Windows.Forms (?!) and System.Drawing references.

Control code:

using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Globalization;
using System.Web.UI;
using System.Windows.Forms.Design;
using editors;

namespace WebApplication1.Controls
{
    public class UC : Control
    {
        [Browsable(true)]
        [Description("test prop")]
        [Editor(typeof(Editor), typeof(UITypeEditor))]
        public string test1 { get; set; }
    }
}

namespace editors
{
    public class Editor : UITypeEditor
    {
        private IWindowsFormsEditorService editorService;

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            value = "test1";
            if (provider != null)
            {
                editorService =
                    provider.GetService(
                    typeof(IWindowsFormsEditorService))
                    as IWindowsFormsEditorService;
            }
            if (editorService != null)
            {
                var listBox = new System.Windows.Forms.ListBox();
                listBox.Items.Add("test");
                listBox.SelectedValueChanged += listBox_SelectedValueChanged;
                editorService.DropDownControl(listBox);

                value = listBox.SelectedItem;
            }
            return value;
        }

        void listBox_SelectedValueChanged(object sender, EventArgs e)
        {
            if (editorService != null)
            {
                editorService.CloseDropDown();
            };
        }
    }
}







Web forrm代码:




Web forrm code:

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Register TagPrefix="uc" Namespace="WebApplication1.Controls" Assembly="WebApplication3" %>

<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>test</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        test
        <uc:UC runat="server"></uc:UC>
    </div>
    </form>
</body>
</html>







网页配置:






Web config:

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
    <system.web>
      <compilation debug="true" targetFramework="4.5">
        <assemblies>
          <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
          <add assembly="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
        </assemblies>
      </compilation>
      <httpRuntime targetFramework="4.5" />
    </system.web>

</configuration>

推荐答案

这篇关于ASP.NET设计时属性编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:40