I created an empty App Store project named Playwithxaml in the VS CTP, and the project's MainPage.xaml file was changed to the following:
<Pagex: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:stringx:key= "MyText">I don ' t feel good</local:string> </page.resources> <Grid> <ButtonContent="{StaticResource Resourcekey=mytext}" /> </Grid></Page>
The problem now is that we first have to add our String class to the Playwithxaml namespace.
namespace playwithxaml{ publicclass string { publicstring setprivateget;} Public Override string ToString () { return Content;}} }
Unfortunately, compile-time VS tells us not to do this:
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.
If you want to modify it, we can start with the original XAML file. For our purposes, we can adjust the namespace of the previous XAML file:
<P:pagex: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> <Stringx:key= "MyText">I don ' t feel good</String> </p:page.resources> <P:grid> <P:buttonContent="{P:staticresource Resourcekey=mytext}"/> </P:grid></P:page>
Let's focus on the declaration of the MyText string resource:
<x:key= "MyText">I don ' t feel good</String >
We changed him to:
<x:key= "MyText" Content= "I don ' t feel good"/>
The compilation is passed.
Or we can change to a more verbose, semantically identical form of another:
<x:key= "MyText"> <string.content >I don ' t feel good</string.content></String >
Does it look closer to the target? This time, a XAML node called string.content might look worse than the previous one.
Fortunately, there is a mechanism within the XAML itself that allows us to declare in the way that we expected it to be:
<x:key= "MyText">I feel quite good</String >
What we need in WPF is a custom attribute,System.Windows.Markup.ContentPropertyAttribute(click to access the MSDN documentation) . In Windows Phone app development This attribute is Windows.UI.Xaml.Markup.ContentPropertyAttribute (click to access the MSDN documentation).
All we need to do is add this attribute to our String class:
namespaceplaywithxaml{usingWindows.UI.Xaml.Markup; [Contentproperty (Name="Content")] Public classString { Public stringContent {Set;Private Get; } Public Override stringToString () {returnContent; } }}
So it's done.
Resolves "the type does not support direct content for the Windows Phone 8.1 app." The problem