为 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 ...
随机推荐
- Spring boot 分环境部署
一.如果配置文件为:application.properties时 1.application.properties用于填些公共文件 以下为不同环境的配置文件需要单独配置 application-de ...
- 用Martini、websocket实现单机版聊天室
ChatRoom A stand-alone ChatRoom in Martini Please Star https://github.com/renleimlj/ChatRoom Interfa ...
- JavaScript(十三)面向对象
面向对象 面向对象的过程 通过new 构造函数 生成的对象来执行, 类似于事件的执行 this指向函数,然后再把这个函数赋值给一个实例 所以在函数内的this 就指到了实例上 function ...
- python实现qq机器人qqbot
title: python实现qq机器人qqbot tags: python date: 2018-6-1 10:19:00 --- 以下内容为转载 一.介绍 qqbot 是一个用 python 实现 ...
- firefox + pentadactyl 实现纯绿色高效易扩展浏览器(同时实现修改默认状态栏样式)
这几天开始使用firefox+pentadactyl来搭建一个开源.可扩展.完全绿化的浏览器环境,以便随身带着使用,其中firefox的使用了24.0的长期支持版, 这边版本稳定, 快速, 兼容性好, ...
- 如何在Win8.1和Win2012上运用PowerShell快速生成、安装、导出自签名证书 (Self-Signed Certificate)
自签名证书用途很广,测试,开发,本地或者云端网站(比如Microsoft Azure Web Site)都会使用到.本文会介绍一种在Win8.1和Win2012 R2上使用PowerShell快速生成 ...
- Java基础(五)--内部类
内部类简单来说就是把一个类的定义放到另一个类的定义内部 内部类分为:成员内部类.局部内部类.匿名内部类.静态内部类 成员内部类:最常见的内部类 public class Outter { privat ...
- Spring框架系列(六)--事务Transaction
本文绝大部分内容为转载,原文地址:https://blog.csdn.net/trigl/article/details/50968079 除此之外,后面还有延伸内容 事务在企业日常开发中几乎是一定会 ...
- 第二节:Css重写样式
一丶 进入浏览器---->F12----->找到要修改的区域的Style 进行重写Css样式 二丶打开新页面 window.open("/Persitent/OtherIndex ...
- Object.prototype 原型和原型链
Object.prototype 原型和原型链 原型 Javascript中所有的对象都是Object的实例,并继承Object.prototype的属性和方法,有些属性是隐藏的.换句话说,在对象创建 ...