如何在xaml代码中的字段上修复CA1051?

时间:2011-11-25 15:13:53

标签: silverlight code-analysis fxcop

我正在针对我的Silverlight代码运行FxCop,并且似乎得到了很多CA1051错误代码:

Error, Certainty 90, for DoNotDeclareVisibleInstanceFields

Target       : #msgTextBox  (IntrospectionTargetMember)
Resolution   : "Because field 'MessagingControl.msgTextBox' is 
               visible outside of its declaring type, change its accessibility 
               to private and add a property, with the same accessibility 
               as the field has currently, to provide access to it."
Help         : http://msdn.microsoft.com/library/ms182141(VS.100).aspx  (String)
Category     : Microsoft.Design  (String)
CheckId      : CA1051  (String)
RuleFile     : Design Rules  (String)
Info         : "Instance fields that are visible outside of the type 
               that declares them, limit your ability to change the 
               implementation details for those data items. Use properties 
               instead. They do not compromise usability or performance 
               and they do provide flexibility in that they conceal 
               the implementation details of the underlying data."
Created      : 25/11/2011 10:59:54  (DateTime)
LastSeen     : 25/11/2011 14:57:49  (DateTime)
Status       : Active  (MessageStatus)
Fix Category : NonBreaking  (FixCategories)

其中大部分似乎来自xaml中的x:Name所以我认为它不能修复?有什么办法可以解决这些错误而不是压制它们吗?

1 个答案:

答案 0 :(得分:2)

控制字段的默认可见性设置为内部。

使用x:FieldModifier="<modifier>"例如<TextBox x:Name="msgTextBox" x:FieldModifier="private" />来控制此行为。