在后面的代码中访问扩展控件的属性 [英] Accessing Extension Control's Property in Code Behind

查看:83
本文介绍了在后面的代码中访问扩展控件的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在此处发布了扩展的TextBox控件. http://code.msdn.microsoft.com/Extended-Text-Box-b8c7b911

针对Dave的问题 来自这篇文章 关于控制文本框行为的bool属性,我向 控制.为了访问控件(不仅是文本框),我还修改了代码以返回控件(),而不是 this.TextBox .  这是代码的样子

 

 object    IContentVisual  .Control
        {
          < span style ="color:blue">得到  {  ; }
       }
 
      < span style ="color:blue"> void    IContentVisual .展示()
        {
       } 

 

这是LSML部分.

      <   ControlProperty 
               "color:red">名称  = "  IsApply 
                 "color:red"> PropertyType   = " :Boolean 
                 "color:red">类别名称  = " 外观
                 "color:red"> EditorVisibility   = "  PropertySheet "       <   ControlProperty.Attributes  > 
       <   DisplayName       = " 可以自动加载吗?"  /> 
        <  说明 span>      = " 可以自动加载吗?"  /> 
     </  ControlProperty.Attributes  > 
 
      <   ControlProperty.DefaultValueSource  > 
       <   ScreenExpressionTree  > 
         <   ConstantExpression      ResultType   = < :布尔值"     Value   = "  True " /> 
       </  ScreenExpressionTree  > 
       </  ControlProperty.DefaultValueSource  > 
     </  ControlProperty     </  Control.Properties  跨线程访问";错误.  然后,我将代码移到了主调度程序中,但是现在我收到了" UnauthorizedAccess Exception ".

 

这里有什么问题?


-Bala

解决方案

txtBox.IsApply =! txtBox.IsApply

您在哪里使用此代码?如果尝试从LightSwitch应用程序用户代码中使用它,则问题在于您无法从屏幕的调度程序访问或更改对象(这是导致跨线程访问异常的原因).你只能 从主调度程序执行此操作(这是Silverlight的限制).

什么引发了UnauthorizedAccessException?在哪一行代码上?您能提供一个调用栈吗?您可以通过从工具"->选项"->调试"中关闭我的代码",然后打破所有异常(至少打破UnauthorizedAccessException)来实现此目的.


I've posted a Extended TextBox control over here. http://code.msdn.microsoft.com/Extended-Text-Box-b8c7b911

In response to Dave's question from this post regarding a bool property to control the textbox's behavior, I added a IsApply Property to the control.  In order to access the control (not just the TextBox), I also modified the code to return the Control (this) instead of this.TextBox.  This is what the code looks like

 

        object IContentVisual.Control
        {
            get { return this; }
        }
 
        void IContentVisual.Show()
        {
        }

 

This is the LSML part.

      <ControlProperty
                Name="IsApply"
                PropertyType=":Boolean"
                CategoryName="Appearance"
                EditorVisibility="PropertySheet">
        <ControlProperty.Attributes>
          <DisplayName Value="Can Load Automatically?" />
          <Description Value="Can Load Automatically?" />
        </ControlProperty.Attributes>
 
        <ControlProperty.DefaultValueSource>
          <ScreenExpressionTree>
            <ConstantExpression ResultType=":Boolean" Value="True"/>
          </ScreenExpressionTree>
        </ControlProperty.DefaultValueSource>
      </ControlProperty>
    </Control.Properties>

 

Everything works perfect on the Design Time/Runtime designers. But when I tried to access the property from Code-Behind, something like...

txtBox.IsApply = !txtBox.IsApply

I receiving "Cross Thread Access" error.  Then I moved the code inside the Main Dispatcher, but now I'm getting a "UnauthorizedAccess Exception". 

 

Whats the issue here?


-Bala

解决方案

txtBox.IsApply = !txtBox.IsApply

Where are you using this code? If you are trying to use it from LightSwitch application user code, the problem is that you cannot access or mutate objects from the screen's dispatcher (this is what causes the cross thread access exception). You must only do it from the main dispatcher (this is a restriction by Silverlight).

What is throwing the UnauthorizedAccessException and on what line of code? Could you provide a callstack. You can get this by turning off Just My Code from Tools->Options->Debug and then breaking on all exceptions (at least break on the UnauthorizedAccessException).


这篇关于在后面的代码中访问扩展控件的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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