在 WinRT 中绑定 ContentControl 的 Content 属性 [英] Binding the Content property of a ContentControl in WinRT

查看:23
本文介绍了在 WinRT 中绑定 ContentControl 的 Content 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个 Windows Store 应用程序(针对 Windows 8.1),并且在页面上有一个如下所示的 ContentControl:

Let's say I have a Windows Store app (targeting Windows 8.1), and on a page there's a ContentControl that looks like this:

<ContentControl>
  <ContentControl.Content>
    <TextBlock>Hello world</TextBlock>
  </ContentControl.Content>
</ContentControl>

这绝对没问题,但如果我尝试将内容设置为资源,如下所示:

This works absolutely fine, but if I try to set the content up as a resource, like this:

<Page.Resources>
  <TextBlock x:Key="TestContent">Hello world</TextBlock>
</Page.Resources>
<ContentControl Content="{StaticResource TestContent}" />

设计器中的一切看起来都很棒,但在运行时出现以下错误:

Everything looks great in the designer, but I get the following error at runtime:

无法分配给属性'Windows.UI.Xaml.Controls.ContentControl.Content'

Failed to assign to property 'Windows.UI.Xaml.Controls.ContentControl.Content'

我尝试在不同的地方(app.xaml、单独的资源文件等)定义资源,但每次都遇到相同的错误.

I've tried defining the resource in various places (app.xaml, separate resource files, etc.) but I get the same error each time.

所以,我有一些问题:

  1. 这在 WinRT XAML 中是否可行?我只是在做一些愚蠢的事情吗?
  2. 是否有另一种方法可以像这样为任意内容提供资源,例如路径数据?(通过为 Path 元素定义样式、在 setter 中配置路径数据,我取得了一些有限的成功,但在导航回页面时似乎没有重新绑定.不过这是另一个问题......)

推荐答案

通常,资源是共享的单个实例",并且各种 XAML 元素都引用单个共享实例.我不确定为什么设计师暗示这会起作用(除了它支持多种类型的XAML"的传统).但是,在 TextBlock 的情况下,它有点不同,因为您希望 Element 实例能够被多次复制和实例化(可能托管在多个 ContentControl>s 例如).

Normally, a resource is a shared "single instance" and various XAML elements are referring to the single shared instance. I'm not sure why the Designer implies this would work (other than its heritage in supporting multiple types of "XAML"). In the case of a TextBlock though, it's a bit different as you'd want the Element instance to be able to be replicated and instantiated multiple times (potentially being hosted in multiple ContentControls for example).

WPF 有一个特性来完成这项工作,在一个名为 x:Shared 的特殊属性中.您可以将其设置为 false 以指示 Resource 未共享,并且对该资源的每个请求都应返回一个新实例.WinRT 没有同样的功能.

WPF had a feature to make this work, in a a special attribute called x:Shared. You'd set that to false to indicate that a Resource was not shared and that each request for the resource should return a new instance. WinRT does not have this same feature.

有一个完全受支持的变通方法,但您可以考虑.

There is a fully supported work-around however that you might consider.

一种选择是使用模板而不是像您尝试的那样直接替换内容:

One option would be to use a Template instead of replacing the content directly as you have tried:

<Page.Resources>
    <Style x:Name="replacement" TargetType="ContentControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ContentControl">
                    <TextBlock FontSize="100" Foreground="Red">Hello!</TextBlock>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ContentControl Style="{StaticResource replacement}"></ContentControl>
</Grid>

在语法上,当然要长一点,但在功能上,应该是相同的结果.

Syntactically, it's a little longer of course, but functionally, it should be the same results.

如果没有 x:Shared,您只能绑定到固有数据类型的资源,例如 x:string(如下例所示)作品):

Without the x:Shared, you are limited to being able to bind to resources that are the intrinsic data types, such as x:string (as the example below works):

<Page.Resources>
    <x:String x:Key="tbResource">The Text!</x:String>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" >
    <ContentControl Content="{StaticResource tbResource}" ></ContentControl>
</Grid>

这篇关于在 WinRT 中绑定 ContentControl 的 Content 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆