为 Windows Phone 8.1 app 解决“The type does not support direct content.”的问题
我在 VS 14 CTP 中新建了一个空的 app store 项目名叫 PlayWithXaml ,项目的 MainPage.xaml 文件改为了以下内容:
<Page
x:Class="PlayWithXaml.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:PlayWithXaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Resources>
<local:String x:Key="MyText">
I don't feel good
</local:String>
</Page.Resources>
<Grid>
<Button Content="{StaticResource ResourceKey=MyText}" />
</Grid>
</Page>
现在的问题是我们先要在 PlayWithXaml 名字空间加入我们的 String 类。
namespace PlayWithXaml
{
public class String
{
public string Content { set; private get; }
public override string ToString()
{
return Content;
}
}
}
不幸的是,编译时 VS 告诉我们不能这么做:
Error 1 Missing Content Property definition for Element 'String' to receive content 'I don't feel good'
Error 2 Unknown member '_UnknownContent' on element 'String'
Error 3 The type 'String' does not support direct content.
如果要修改,我们可以从原来的 xaml 文件下手。为我们的目的着想,可以调整一下前面的 xaml 文件的名字空间:
<p:Page
x:Class="PlayWithXaml.MainPage"
xmlns:p="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="using:PlayWithXaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{p:ThemeResource ApplicationPageBackgroundThemeBrush}">
<p:Page.Resources>
<String x:Key="MyText">
I don't feel good
</String>
</p:Page.Resources>
<p:Grid>
<p:Button Content="{p:StaticResource ResourceKey=MyText}"/>
</p:Grid>
</p:Page>
我们把注意力集中在 MyText 字符串资源的声明上:
<String x:Key="MyText">I don't feel good</String>
我们把他改成:
<String x:Key="MyText" Content="I don't feel good"/>
这下编译就通过了。
或者我们可以改为更冗长的、语义相同的另一种形式:
<String x:Key="MyText" >
<String.Content>I don't feel good</String.Content>
</String>
是不是看上去更接近目标了?这次多了一个叫 String.Content 的 XAML 节点,也许看上去比前一个方案更糟糕了。
幸运的是, XAML 本身内建了一种机制,允许我们以一开始期望的那种方式做声明:
<String x:Key="MyText">I feel quite good</String>
在 WPF 中我们需要的是一个自定义的 attribute ,System.Windows.Markup.ContentPropertyAttribute(点击访问 MSDN 文档)。在 Windows Phone app 开发中这个 attribute 是 Windows.UI.Xaml.Markup.ContentPropertyAttribute (点击访问 MSDN 文档)。
我们需要做的就是给我们的 String 类添加这个 attribute :
namespace PlayWithXaml
{
using Windows.UI.Xaml.Markup;
[ContentProperty(Name = "Content")]
public class String
{
public string Content { set; private get; }
public override string ToString()
{
return Content;
}
}
}
于是大功告成。
为 Windows Phone 8.1 app 解决“The type does not support direct content.”的问题的更多相关文章
- Windows Phone 8 - 建立App专属联络人资讯(ContactStore)
原文:Windows Phone 8 - 建立App专属联络人资讯(ContactStore) 在WP7的时候介绍了如何操作联络人的功能,例如:<Windows Phone 7 - 存取联络人与 ...
- Windows 8.1 store app 开发笔记
原文:Windows 8.1 store app 开发笔记 零.简介 一切都要从博彦之星比赛说起.今年比赛的主题是使用Bing API(主要提到的有Bing Map API.Bing Translat ...
- Windows远程桌面卡的解决办法
Windows远程桌面卡的解决办法 如果在网络没有什么大问题的情况下,可以尝试以下操作. 1.显示中颜色选择 增强色15位 2.体验中 设置成下图的样子 然后在尝试连接试试有没有好点
- 【转】windows 控制台cmd乱码的解决办法
windows 控制台cmd乱码的解决办法 我本机的系统环境: OS Name: Microsoft Windows 10 企业版 OS Version: 10.0.14393 N/A Build 1 ...
- linux和windows下TIME_WAIT过多的解决办法
http://www.51testing.com/html/48/202848-249774.html linux和windows下TIME_WAIT过多的解决办法 http://m.sohu.com ...
- windows 控制台cmd乱码的解决办法
windows 控制台cmd乱码的解决办法 我本机的系统环境: OS Name: Microsoft Windows 10 企业版 OS Version: 10.0.14393 N/A Build 1 ...
- win7系统 windows update 总是更新失败解决方法:
win7系统 windows update 总是更新失败解决方法: 右键单击桌面“计算机”选择“管理“. 进到“计算机管理“窗口后,展开”服务和应用程序“并双击”服务“,在窗口右侧按照名称找到”Win ...
- Windows上Tomcat安装以及解决乱码问题
Windows上Tomcat安装以及解决乱码问题 下载tomcat8 1.进入tomcat官网 官方网站 2.选择windows的版本 解压 确定自己配置好了jdk jdk的相关配置 配置好tomca ...
- windows defender antivirus占用内存解决教程
[windows defender关闭常见问题] windows defender antivirus占用内存解决教程: 1.按下"Win+R"打开"运行" 2 ...
随机推荐
- Asp.NET 知识点总结(一)
1.简述 private. protected. public. internal 修饰符的访问权限. 答 . private : 私有类,私有成员, 在类的内部才可以访问. protected : ...
- DP BestCoder Round #50 (div.2) 1003 The mook jong
题目传送门 /* DP:这题赤裸裸的dp,dp[i][1/0]表示第i块板放木桩和不放木桩的方案数.状态转移方程: dp[i][1] = dp[i-3][1] + dp[i-3][0] + 1; dp ...
- 二分搜索 HDOJ 2675 Equation Again
题目传送门 /* 二分搜索:式子两边取对数,将x提出来,那么另一边就是一个常数了,函数是:lnx/x.二分搜索x,注意要两次 */ #include <cstdio> #include & ...
- 题解报告:poj 1321 棋盘问题(dfs)
Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...
- CSS 样式的优先级小结
1. 同一元素引用了多个样式时,排在后面的样式属性的优先级高 例如,下面的 div,同时引用了 [.default] 和 [.user] 中的样式,其中 [.user] 样式中的 width 属性会替 ...
- 神奇的Object.defineProperty
vue.js和avalon.js 都是通过它实现双向绑定的. 对象是由多个名/值对组成的无序的集合.对象中每个属性对应任意类型的值.定义对象可以使用构造函数或字面量的形式: var obj={}; o ...
- 前端er怎样操作剪切复制以及禁止复制+破解等
前言 有时候我们经常会碰到这些场景:玩掘金.知乎的时候复制一段文字,总是会在内容后面加上一些版权信息,以及像小说网站等都有禁止选中,禁止复制这种功能,还有点击自动复制账号的功能. 我也经常遇到这些场景 ...
- 在Redux中使用插件createAction之后
我们知道在React的Redux的中的action在项目开发过程中,一般时使用createAction来生成 举个栗子,小李子: const createTodo=createACtion('CREA ...
- 项目经验——Sql server 数据库的备份和还原____还原数据库提示“介质集有2个介质簇,但只提供了1个。必须提供所有成员” .
在对数据库备份与还原的过程中,我遇到一个问题“介质集有2个介质簇,但只提供了1个.必须提供所有成员”,下面详细的介绍一下遇到问题的经过与问题解决的方法! 一.备份与还原遇到的问题描述与解决方法: 前两 ...
- Codeforces_766_C_(dp)
C. Mahmoud and a Message time limit per test 2 seconds memory limit per test 256 megabytes input sta ...