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.
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 internal
or 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.
XAML控件不可访问,它具有一定的保护级别的更多相关文章
- C#中线程对控件的访问
Control类提供了一个Invoke方法来给子线程访问主线程的控件,它的原型是酱紫的: object.Control.Invoke(Delegate method); object.Control. ...
- WPF后台设置xaml控件的样式System.Windows.Style
WPF后台设置xaml控件的样式System.Windows.Style 摘-自 :感谢 作者: IT小兵 http://3w.suchso.com/projecteac-tual/wpf-zhi ...
- 浅谈XAML控件
在win10系统内简单使用了XAML控件,由于本人英语水平有限,在自己的摸索使用.分析代码以及翻译软件.搜索引擎.室友情的帮助下了解了控件的相关功能,下面简要对XAML控件提出几点建议: 1.Cale ...
- WPF不同线程之间的控件的访问
原文:WPF不同线程之间的控件的访问 WPF不同线程之间的控件是不同访问的,为了能够访问其他线程之间的控件,需要用Dispatcher.Invoke执行一个新的活动即可. 例如: public voi ...
- Xamarin.Forms XAML控件的公共属性
Xamarin.Forms XAML控件的公共属性 Xamarin.Forms XAML控件有很多.通过官网API,可以查看每个控件的属性.但是官网只给出了控件的特有属性,而公共属性没有列出.所以 ...
- WPF--动态添加控件、访问控件
//WPF窗口采用默认的Grid布局控件,其“Name”值为“grid1”,在“grid1”中添加三个Button按钮.动态添加控件并访问这些控件的代码如下: private void button1 ...
- 获取Delphi焦点所在的控件及通过控件名称访问控件
方法一: Var I: Integer; Begin For I := To ComponentCount - Do //获取组件数量 Begin If Components[I] Is TWinCo ...
- 错误 1 “Entities.PlanPrjEntity.PlanPrjs”不可访问,因为它受保护级别限制
本人第一次是用List做父类,写了一个类PlanPrjs,如下: class PlanPrj { public int ID { get; set; } public string Name { ge ...
- 如何获取 XAML 控件的模板代码
有时候 .NET 自带提供的控件并不能满足我们的实际需求,需要进行修改,或者参考代码来建立新的控件. 可以在编辑器的文档大纲窗口中,找到所需的对象,然后在其上点右键,编辑模板,编辑副本 弹出创建 St ...
随机推荐
- Precision 7520双硬盘无法识别固态硬盘
将RAID ON 修改为AHCI,如图1,会使得 win10无法 启动,如图2 图 1 图 2 可以开legacy,如图3,让电脑可以从u盘启动,如图4,但是也无法查看到固态硬盘 图 3 图 4 网上 ...
- uCos-II中任务的同步与通信
任务的同步与通信 任务间的同步 在多任务合作工作过程中,操作系统要解决两个问题: 各任务间应该具有一种互斥关系,即对某些共享资源,如果一个任务正在使用,则其他任务只能等待,等到该任务释放资源后,等待任 ...
- Windows Server 2016 配置 IIS 的详细步骤
Ø 简介 本文主要记录 Windows Server 2016 环境下,安装配置 IIS 的详细步骤.需要说明的是,在选择"功能"或"角色服务"时不建议将所有 ...
- rcu-bp关键代码解读
1 什么是TLS 原理在网上资料很多,这里不展开. 简单点说,动态申请的每线程变量.有一类比较熟悉的每线程变量是一个带__thread的每线程变量,两者的区别在于,TLS这类每线程变量是动态 ...
- win-DOS命令整理
1 md 建 文件夹2 cd 指向文件夹方向cd .. 进入上一级文件夹cd \ 回到根目录3 rd 删除文件夹4 dir 查看文件夹里的文件 dir /a 查看文件夹内全部文件含隐藏文件 5 ren ...
- 全排列递归算法(元素有重复与无重复,C++实现)
元素无重复: 如:2,5,8,9. 思路:用递归的方法解决,对于2589,先输出所有以2开头的排列,然后输出5开头的排列.....(此处称为递归操作A).以2开头的排列中,第一位是2,后面的是589, ...
- MySQL学习12 - pymysql模块的使用
一.pymysql的下载和使用 1.pymysql模块的下载 2.pymysql的使用 二.execute()之sql注入 三.增.删.改:conn.commit() 四.查:fetchone.fet ...
- jQuery手机触屏拖动滑块验证跳转插件
HTML: <!DOCTYPE html> <html lang="en"> <head> <title>jQuery手机触屏拖动滑 ...
- 工作经验-Oracle定时数据备份
Oracle database 11g express edition http://www.oracle.com/technetwork/cn/products/express-edition/do ...
- 使用Lottie将AE项目转换为 Web 原生动画
使用Lottie转换AE项目为 Web 原生动画 首先打开链接https://github.com/airbnb/lottie-web/blob/master/build/extension/body ...