為 Windows Phone 8.1 app 解決“The type does not support direct content.”的問題

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   for   檔案   

我在 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.”的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.