Binding the Content property of a ContentControl in WinRT

后端 未结 2 556
萌比男神i
萌比男神i 2020-12-19 05:41

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:


  

        
2条回答
  •  有刺的猬
    2020-12-19 06:16

    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 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:

    
        
    
    
        
    
    

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

    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):

    
        The Text!
    
    
        
    
    

提交回复
热议问题