为 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 ...
随机推荐
- n阶完全生成图的数量
有些事不是看到了希望才去坚持,而是坚持了才会看到希望 问题 I: 星际之门(一) 时间限制: Sec 内存限制: MB 提交: 解决: [提交][状态][讨论版] 题目描述 公元3000年,子虚帝国统 ...
- SparkContext, map, flatMap, zip以及例程wordcount
SparkContext 通常作为入口函数,可以创建并返回一个RDD. 如把Spark集群当作服务端那Spark Driver就是客户端,SparkContext则是客户端的核心: 如注释所说 Spa ...
- [译]HTTP POSTing
HTTP POSTing We get many questions regarding how to issue HTTP POSTs with libcurl the proper way. Th ...
- sql server 行转列 要注意的问题 pivot
select * from ( select mvqr.VoteQuestionId,mvqr.AnswerSolution from JY_MemberVoteQuestionRef as ...
- Java常见面试问题: equals()与hashCode()的使用
目录 1 equals()与'=='的区别 2 equals()方法的重写规则 3 为什么重写equals()的同时还需要重写hashCode() 4 JDK 7中对hashCode()方法的改进 5 ...
- Java使用 POI 操作Excel
Java中常见的用来操作 Excel 的方式有2种:JXL和POI.JXL只能对 Excel进行操作,且只支持到 Excel 95-2000的版本.而POI是Apache 的开源项目,由Java编写的 ...
- Farseer.net轻量级开源框架 入门篇:Where条件的终极使用
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 查询数据详解 下一篇:Farseer.net轻量级开源框架 中级篇: 事务的使用 ...
- 北大ACM(POJ1017-Packets)
Question:http://poj.org/problem?id=1017 问题点:贪心. Memory: 224K Time: 32MS Language: C++ Result: Accept ...
- uint8_t、uint16_t、uint32_t是啥?
最近在做一个简单的按键检测,定义一个uint8_t的函数,函数作用是返回一个按键编号数字. 函数返回值 return 1/2/3/4,代表4个按键 但是按键检测结果却是错误的!!! 百思不得其解,后来 ...
- java基础学习日志--String、StringBuffer方法案例
package StringDemo; import java.util.Arrays; /* * 常用String.StringBufer类的方法 */ public class Demo1 { p ...