其它namespace的代码访问控件时会出现这个问题

需要把控件状态由protected改为public

<TextBlock x:FieldModifier="public" x:Name="AccessibleTextBlock" />

The x:Name attribute in XAML creates named fields that you can use to access the controls from the code-behind. However, as opposed to WPF, in UWP these fields are private which means you can access them from the code-behind only, not from other classes. While noting it is a good idea from architectural standpoint, is it possible to change this behavior?

Normal behavior

Let’s define a simple TextBlock control in XAML.

 
1
<TextBlock x:Name="InaccessibleTextBlock" />

Now, what happens if we create a new class that takes the page as parameter of one of the methods and tries to access the TextBlock?

 
1
2
3
4
5
6
7
public static class OutsideAccess
{
    public static void ChangeTexts(MainPage page)
    {
        page.InaccessibleTextBlock.Text = "Accessed"; //does not work!
    }
}

The app will not compile, because the field is inaccessible due to its protection level.

To see what actually happens behind the scenes, let’s open the auto-generated MainPage.g.i.cs file which can be found in the obj folder. We can find the following field there:

 
1
2
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Windows.UI.Xaml.Build.Tasks"," 14.0.0.0")]
private global::Windows.UI.Xaml.Controls.TextBlock InaccessibleTextBlock;

Clearly, the field is defined as private.

x:FieldModifier directive

To change the code generation behavior, you can use the x:FieldModifier directive. This allows you to specify excatly which access modifier should be field have.

 
1
<TextBlock x:FieldModifier="public" x:Name="AccessibleTextBlock" />

Now, accessing the field from the outside works as a charm:

 
1
2
3
4
5
6
7
public static class OutsideAccess
{
    public static void ChangeTexts(MainPage page)
    {
        page.AccessibleTextBlock.Text = "Accessed";
    }
}

Note that you are not limited to public and private only, and you can also set the field to be internalor protected.

We can confirm the change of visiblity was reflected in the generated source code:

 
1
2
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Windows.UI.Xaml.Build.Tasks"," 14.0.0.0")]
public global::Windows.UI.Xaml.Controls.TextBlock AccessibleTextBlock;

WPF

If you wonder, what is the default behavior in WPF, wonder no more!

WPF’s convention is to set all named fields as internal by default:

 
1
2
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
internal System.Windows.Controls.TextBlock InaccessibleTextBlock;

You can use the x:FieldModifier directive to modify the visibility the same way as in UWP.

最新文章

  1. [ruby on rails] 深入(1) ROR的一次request的响应过程
  2. 短日期比较 js
  3. 教官的游戏(codevs 2793)
  4. ios 图片转视频
  5. jquery 图片放大
  6. ASP.NET MVC 第七回 UrlHelper
  7. D. DZY Loves Modification
  8. Swift入门教程:基本语法(二)
  9. Hive中常用的查询命令
  10. python插入记录后取得主键id的方法(cursor.lastrowid和conn.insert_id())
  11. img标签里的value获取
  12. CentOS 7 配置DHCP中继代理服务
  13. laravel5.7 migrate 时报错 Specified key was too long error 解决方案
  14. error: Build input file cannot be found: &#39;*******/node_modules/react-native/Libraries/WebSocket/libfishhook.a&#39; 问题解决记录
  15. 关于cocos2dx 关键字的问题
  16. freopen stdout 真的更快?
  17. ElasticSearch 2 (15) - 深入搜索系列之多字段搜索
  18. LeetCode 10 Regular Expression Matching(字符串匹配)
  19. hdu 5963 朋友(2016ccpc 合肥站 C题)
  20. Java-API:java.util.map

热门文章

  1. RSA加密传输代码示例
  2. Oracle DB Day01(SQL)
  3. 如何参与flink开源项目
  4. step into,step over,step out.
  5. 10、jstl标签库
  6. 使用JQuery实现图片轮播效果
  7. windows命令提示符
  8. day 19 - 1 模块
  9. javascript基础 之 jQuery教程
  10. spring boot集成netty-服务端和客户端demo