标签: user-controls

"无法修改Controls集合,因为控件包含代码块"

我正在尝试创建一个简单的用户控件,它是一个滑块.当我向用户控件添加AjaxToolkit SliderExtender时,我得到了这个(*&$#()@#error:

Server Error in '/' Application. The Controls collection cannot be modified because the control contains code blocks (i.e. `<% ... %>`). Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. `<% ... %>`).

Source Error:

An unhandled exception was generated …
Run Code Online (Sandbox Code Playgroud)

c# asp.net user-controls

365
推荐指数
7
解决办法
39万
查看次数

用户控件库和自定义控件库之间有什么区别?

我刚刚开始加速WPF,并希望创建一个可重用的WPF控件.

当我查看在Visual Studio中创建项目的选项时,我看到"WPF用户控件库"和"WPF自定义控件库".我不清楚他们之间的区别是什么,我的谷歌搜索没有找到任何体面的解释.

我想了解它们之间的差异,理想情况下看一些何时使用一个而不是另一个的例子.

wpf user-controls custom-controls

161
推荐指数
3
解决办法
5万
查看次数

如何在C#中获取我所在页面的完整URL

我需要能够从用户控件获取我所在页面的完整URL.这只是将一堆Request变量连在一起的问题吗?如果是这样的?还是有更简单的方式?

c# asp.net user-controls

156
推荐指数
8
解决办法
18万
查看次数

处置WPF用户控件

我创建了一个自定义WPF用户控件,供第三方使用.我的控件有一个一次性的私有成员,我想确保一旦包含窗口/应用程序关闭,它的dispose方法将始终被调用.但是,UserControl不是一次性的.我尝试实现IDisposable接口并订阅Unloaded事件,但在主机应用程序关闭时都不会被调用.如果可能的话,我不想依赖我控制的消费者记住调用特定的Dispose方法.

 public partial class MyWpfControl : UserControl
 {
     SomeDisposableObject x;

     // where does this code go?
     void Somewhere() 
     {
         if (x != null)
         {
             x.Dispose();
             x = null;
         }

     }
 }
Run Code Online (Sandbox Code Playgroud)

到目前为止,我找到的唯一解决方案是订阅Dispatcher的ShutdownStarted事件.这是一种合理的方法吗?

this.Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;
Run Code Online (Sandbox Code Playgroud)

.net c# wpf user-controls dispose

114
推荐指数
5
解决办法
9万
查看次数

如何修复用户控件中的闪烁

在我的应用程序中,我不断从一个控件移动到另一个控件.我创造了没有.用户控件,但在导航过程中我的控件闪烁.更新需要1或2秒.我试着设置这个

SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
or
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true); 
SetStyle(ControlStyles.DoubleBuffer, true);
Run Code Online (Sandbox Code Playgroud)

但它没有帮助......每个控件都有相同的背景图像和不同的控件.那么它的解决方案是什么..
谢谢.

c# user-controls flicker winforms

105
推荐指数
4
解决办法
9万
查看次数

带有C#参数的'UserControl'构造函数

叫我疯了,但我喜欢那种喜欢带参数的构造函数(如果需要)的人,而不是没有参数的构造函数,后跟设置属性.我的思考过程:如果需要实际构造对象的属性,它们应该进入构造函数.我有两个好处:

  1. 我知道当构造一个对象(没有错误/异常)时,我的对象是好的.
  2. 它有助于避免忘记设置某个属性.

这种心态开始在形式/用户控制开发方面受到伤害.想象一下UserControl:

public partial class MyUserControl : UserControl
{
  public MyUserControl(int parm1, string parm2)
  {
    // We'll do something with the parms, I promise
    InitializeComponent();
  }
}
Run Code Online (Sandbox Code Playgroud)

在设计时,如果我将其UserControl放在表单上,​​我得到一个Exception:

无法创建组件'MyUserControl'...
System.MissingMethodException - 没有为此对象定义的无参数构造函数.

对我来说,似乎唯一的办法是添加默认构造函数(除非其他人知道某种方式).

public partial class MyUserControl : UserControl
{
  public MyUserControl()
  {
    InitializeComponent();
  }

  public MyUserControl(int parm1, string parm2)
  {
    // We'll do something with the parms, I promise
    InitializeComponent();
  }
}
Run Code Online (Sandbox Code Playgroud)

不包括无参数构造函数的重点是避免使用它.我甚DesignMode至无法使用该属性执行以下操作:

public partial class MyUserControl : UserControl …
Run Code Online (Sandbox Code Playgroud)

c# parameters user-controls constructor winforms

97
推荐指数
4
解决办法
5万
查看次数

如何使用NAMED内容创建WPF UserControl

我有一组带有附加命令和逻辑的控件,它们以相同的方式不断重用.我决定创建一个包含所有常用控件和逻辑的用户控件.

但是我还需要控件才能保存可以命名的内容.我尝试了以下方法:

<UserControl.ContentTemplate>
    <DataTemplate>
        <Button>a reused button</Button>
        <ContentPresenter Content="{TemplateBinding Content}"/>
        <Button>a reused button</Button>
    </DataTemplate>
</UserControl.ContentTemplate>
Run Code Online (Sandbox Code Playgroud)

但是,似乎无法命名放置在用户控件内的任何内容.例如,如果我以下列方式使用控件:

<lib:UserControl1>
     <Button Name="buttonName">content</Button>
</lib:UserControl1>
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

无法在元素"Button"上设置Name属性值"buttonName".'Button'属于元素'UserControl1'的范围,在另一个范围内定义时,已经注册了一个名称.

如果我删除buttonName,然后编译,但我需要能够命名内容.我怎样才能做到这一点?

c# wpf xaml user-controls controls

90
推荐指数
2
解决办法
4万
查看次数

WPF UserControl如何继承WPF UserControl?

以下WPF UserControl称为DataTypeWholeNumber,它可以正常工作.

现在我想创建一个名为DataTypeDateTimeDataTypeEmail等的UserControl .

许多依赖属性将由所有这些控件共享,因此我想将它们的公共方法放入BaseDataType中,并使每个UserControl都继承自此基类型.

但是,当我这样做时,我得到错误:部分声明可能没有不同的基类.

那么如何使用UserControls实现继承,以便共享功能全部在基类中?

using System.Windows;
using System.Windows.Controls;

namespace TestDependencyProperty827.DataTypes
{
    public partial class DataTypeWholeNumber : BaseDataType
    {
        public DataTypeWholeNumber()
        {
            InitializeComponent();
            DataContext = this;

            //defaults
            TheWidth = 200;
        }

        public string TheLabel
        {
            get
            {
                return (string)GetValue(TheLabelProperty);
            }
            set
            {
                SetValue(TheLabelProperty, value);
            }
        }

        public static readonly DependencyProperty TheLabelProperty =
            DependencyProperty.Register("TheLabel", typeof(string), typeof(BaseDataType),
            new FrameworkPropertyMetadata());


        public string TheContent
        {
            get
            {
                return (string)GetValue(TheContentProperty);
            } …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml user-controls

87
推荐指数
1
解决办法
6万
查看次数

具有嵌套控件的DesignMode

有没有人在开发控件时找到了解决DesignMode问题的有用方法?

问题是,如果嵌套控件,则DesignMode仅适用于第一级.第二级和更低级别的DesignMode将始终返回FALSE.

标准的hack一直在查看正在运行的进程的名称,如果它是"DevEnv.EXE"那么它必须是studio,因此DesignMode真的是真的.

问题是寻找ProcessName通过注册表和其他奇怪的部分工作,最终结果是用户可能没有查看进程名称所需的权限.另外这条奇怪的路线很慢.所以我们不得不堆积额外的黑客来使用单例,如果在请求进程名称时抛出错误,则假设DesignMode为FALSE.

确定DesignMode的一个很好的干净方法是有序的.让微软在框架内部修复它会更好!

.net user-controls

86
推荐指数
5
解决办法
2万
查看次数

用户控件未显示在工具箱中

我有一些我在ProjectA中创建的UserControl.我有一个ProjectB,它有一个我想要控件的窗体.这两个项目都在一个解决方案中.从ProjectB引用了ProjectA,因此它可以"看到"UserControls.

但是,UserControls不会显示在工具箱中,以便我拖动到Windows窗体.

我试过重建.我还删除了'bin'目录以强制重建所有.

如何让VS2008用我的UserControls填充工具箱?

user-controls toolbox visual-studio-2008

71
推荐指数
7
解决办法
9万
查看次数