我在 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.”的问题的更多相关文章

  1. Windows Phone 8 - 建立App专属联络人资讯(ContactStore)

    原文:Windows Phone 8 - 建立App专属联络人资讯(ContactStore) 在WP7的时候介绍了如何操作联络人的功能,例如:<Windows Phone 7 - 存取联络人与 ...

  2. Windows 8.1 store app 开发笔记

    原文:Windows 8.1 store app 开发笔记 零.简介 一切都要从博彦之星比赛说起.今年比赛的主题是使用Bing API(主要提到的有Bing Map API.Bing Translat ...

  3. Windows远程桌面卡的解决办法

    Windows远程桌面卡的解决办法 如果在网络没有什么大问题的情况下,可以尝试以下操作. 1.显示中颜色选择 增强色15位 2.体验中 设置成下图的样子 然后在尝试连接试试有没有好点

  4. 【转】windows 控制台cmd乱码的解决办法

    windows 控制台cmd乱码的解决办法 我本机的系统环境: OS Name: Microsoft Windows 10 企业版 OS Version: 10.0.14393 N/A Build 1 ...

  5. linux和windows下TIME_WAIT过多的解决办法

    http://www.51testing.com/html/48/202848-249774.html linux和windows下TIME_WAIT过多的解决办法 http://m.sohu.com ...

  6. windows 控制台cmd乱码的解决办法

    windows 控制台cmd乱码的解决办法 我本机的系统环境: OS Name: Microsoft Windows 10 企业版 OS Version: 10.0.14393 N/A Build 1 ...

  7. win7系统 windows update 总是更新失败解决方法:

    win7系统 windows update 总是更新失败解决方法: 右键单击桌面“计算机”选择“管理“. 进到“计算机管理“窗口后,展开”服务和应用程序“并双击”服务“,在窗口右侧按照名称找到”Win ...

  8. Windows上Tomcat安装以及解决乱码问题

    Windows上Tomcat安装以及解决乱码问题 下载tomcat8 1.进入tomcat官网 官方网站 2.选择windows的版本 解压 确定自己配置好了jdk jdk的相关配置 配置好tomca ...

  9. windows defender antivirus占用内存解决教程

    [windows defender关闭常见问题] windows defender antivirus占用内存解决教程: 1.按下"Win+R"打开"运行" 2 ...

随机推荐

  1. Math teacher's homework

    Title:[Math teacher's homework] Description 题目大意:给你n个数m1,m2...mn,求满足X1 xor X2 xor ... xor Xn=k,0< ...

  2. [译]HTTP POSTing

    HTTP POSTing We get many questions regarding how to issue HTTP POSTs with libcurl the proper way. Th ...

  3. [转]ASP.NET MVC Domain Routing

    本文转自:http://blog.maartenballiauw.be/post/2009/05/20/ASPNET-MVC-Domain-Routing.aspx ASP.NET MVC Domai ...

  4. dubbo面试题

    40 道 Dubbo 面试题及答案:https://blog.csdn.net/BinshaoNo_1/article/details/83024303 (原地址奉上:https://mp.weixi ...

  5. [ USACO 2017 FEB ] Why Did the Cow Cross the Road III (Gold)

    \(\\\) \(Description\) 给定长度为\(2N\)的序列,\(1\text ~N\)各出现过\(2\)次,\(i\)第一次出现位置记为\(a_i\),第二次记为\(b_i\),求满足 ...

  6. 连接服务器的mysql

    在服务器配置好Mysql 数据库,在客户端连接,报错: 解决方法: 1.在MySQL 数据库中修改user表,将host 中的localhoust 改为 %: 2.配置访问数据库的全选 根据需要配置权 ...

  7. iOS 声明属性关键字讲解

    atomic: 原子操作(原子性是指事务的一个完整操作,操作成功就提交,反之就回滚. 原子操作就是指具有原子性的操作)在objective-c 属性设置里面 默认的就是atomic ,意思就是 set ...

  8. cookie、json详解

    什么是cookie 1.cookie是存储于访问者计算机中的变量2.cookie是浏览器提供的一种机制3.可以由js控制(设置.读取.删除)4.cookie可以实现跨页面全局变量可以跨越同域名下多个网 ...

  9. HDU_2955_Robberies_01背包

    A - Robberies Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submi ...

  10. SpringBoot基于websocket的网页聊天

    一.入门简介正常聊天程序需要使用消息组件ActiveMQ或者Kafka等,这里是一个Websocket入门程序. 有人有疑问这个技术有什么作用,为什么要有它?其实我们虽然有http协议,但是它有一个缺 ...