zoukankan      html  css  js  c++  java
  • XAML控件不可访问,它具有一定的保护级别

    其它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.

    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?

    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:

    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.

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

    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:

    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:

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

  • 相关阅读:
    基础Linux命令总结
    [博客美化]新年啦,给自己博客加个雪花效果吧~
    自制操作系统Antz(5)——深入理解保护模式与进入方法
    自制操作系统Antz(4)——进入保护模式 (下) 实现内核并从硬盘载入
    自制操作系统Antz(3)——进入保护模式 (中) 直接操作显存
    自制操作系统Antz(2)——进入保护模式 (上) jmp到保护模式
    自制操作系统Antz(1)——Boot Sector
    Java爬取B站弹幕 —— Python云图Wordcloud生成弹幕词云
    Kali Day01 --- arpspoof命令进行断网攻击(ARP欺骗)
    手写杀毒软件——放心的安全卫士
  • 原文地址:https://www.cnblogs.com/xpvincent/p/10348605.html
Copyright © 2011-2022 走看看