2013-04-03 80 views
6

我想说:请不要建议替代解决方案,除非您可以在不修改类型图案BaseXXXXXX在WinRT XAML(Windows8,Metro,Windows应用商店应用)中绑定另一个属性

中说,这种行为竟把我而言远远超出傻了眼,它似乎是为了隐藏在C#中的属性使用new关键字指的WinRT XAML(Windows8中,地铁中,Windows Store App)绑定不再正常运行。我不知道这是为什么。

下面是一个例子:

C#:

namespace WinRtSandbox 
{ 
    public class BaseClass 
    { 
     public string Property1 { get; set; } 
     public int[] Property2 { get; set; } 
     public object Property3 { get; set; } 
    } 


    public class ModifiedClass : BaseClass 
    { 
     public new string Property1 { get; set; } 
     public new long[] Property2 { get; set; } 
     public new string Property3 { get; set; } 
    } 

    public sealed partial class MainPage : Page 
    { 

     public BaseClass Normal { get; set; } 
     public ModifiedClass Modified { get; set; } 

     public MainPage() 
     { 
      this.Normal = new BaseClass 
      { 
       Property1 = "WTF", 
       Property2 = new[] { 2, 3, 4 }, 
       Property3 = "Work?" 
      }; 

      this.Modified = new ModifiedClass 
      { 
       Property1 = "WTF", 
       Property2 = new[] { 2L, 3L, 4L }, 
       Property3 = "Work?" 
      }; 

      this.InitializeComponent(); 
     } 
    } 
} 

的WinRT XAML:

<Page 
    x:Class="WinRtSandbox.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:WinRtSandbox" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    DataContext="{Binding RelativeSource={RelativeSource Self}}" 
    mc:Ignorable="d"> 

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> 
     <Border Background="#22000000" Padding="40" Width="400" Height="500"> 
      <Grid> 

       <Grid.Resources> 
        <Style TargetType="Rectangle"> 
         <Setter Property="Height" Value="1"/> 
         <Setter Property="HorizontalAlignment" Value="Stretch"/> 
         <Setter Property="Margin" Value="0,15,0,15"/> 
         <Setter Property="Fill" Value="{StaticResource ApplicationForegroundThemeBrush}"/> 
        </Style> 
       </Grid.Resources> 

       <Grid.ColumnDefinitions> 
        <ColumnDefinition/> 
        <ColumnDefinition/> 
       </Grid.ColumnDefinitions> 

       <StackPanel Grid.Column="0"> 

        <ItemsControl> 
         <TextBlock Text="this.Normal"/> 
         <Rectangle/> 
         <TextBlock Text="this.Normal.Property1"/> 
         <Rectangle/> 
         <TextBlock Text="this.Normal.Property2"/> 
         <Rectangle/> 
         <TextBlock Text="this.Normal.Property3"/> 
        </ItemsControl> 

        <Rectangle Fill="Red"/> 

        <ItemsControl> 
         <TextBlock Text="this.Modified"/> 
         <Rectangle/> 
         <TextBlock Text="this.Modified.Property1"/> 
         <Rectangle/> 
         <TextBlock Text="this.Modified.Property2"/> 
         <Rectangle/> 
         <TextBlock Text="this.Modified.Property3"/> 
        </ItemsControl> 

       </StackPanel> 

       <StackPanel Grid.Column="1"> 

        <ItemsControl DataContext="{Binding Normal}"> 
         <TextBlock Text="{Binding}"/> 
         <Rectangle/> 
         <TextBlock Text="{Binding Property1}"/> 
         <Rectangle/> 
         <TextBlock Text="{Binding Property2}"/> 
         <Rectangle/> 
         <TextBlock Text="{Binding Property3}"/> 
        </ItemsControl> 

        <Rectangle Fill="Red"/> 

        <ItemsControl DataContext="{Binding Modified}"> 
         <TextBlock Text="{Binding}"/> 
         <Rectangle/> 
         <TextBlock Text="{Binding Property1}"/> 
         <Rectangle/> 
         <TextBlock Text="{Binding Property2}"/> 
         <Rectangle/> 
         <TextBlock Text="{Binding Property3}"/> 
        </ItemsControl> 

       </StackPanel> 
      </Grid> 
     </Border> 

    </Grid> 
</Page> 

的全太不正确的结果看起来类似: what is going on

基本上,每一个这些空行都应该填充你们中的任何一个XAML人员都知道为什么这些绑定失败了,是否有任何可以解决的问题我只能假设是一个令人发指的bug ?任何帮助或洞察力将不胜感激,谢谢提前... -ck

更新:输出转储我忘记

Error: BindingExpression path error: 'Property2' property not found on 'WinRtSandbox.ModifiedClass'. BindingExpression: Path='Property2' DataItem='WinRtSandbox.ModifiedClass'; target element is 'Windows.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String') 
Error: BindingExpression path error: 'Property3' property not found on 'WinRtSandbox.ModifiedClass'. BindingExpression: Path='Property3' DataItem='WinRtSandbox.ModifiedClass'; target element is 'Windows.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String') 

更新:

错误与微软提起:https://connect.microsoft.com/VisualStudio/feedback/details/782993/binding-a-property-that-hides-another-in-winrt-xaml所以我们会看看这是怎么回事

+1

是否有输出,指出绑定失败什么?我认为这只是让人困惑,因为该对象的两个属性具有相同的名称,尽管我找不到关于该主题的任何文献。 – 2013-04-03 20:50:23

+0

@KevinDiTraglia对不起,我忘了包括绑定转储,感谢提醒我,错误使得它听起来没有那个名字的属性听起来那么没有任何属性......冲突消息,我至少会明白 – ckozl 2013-04-03 20:53:35

+1

嗯,有些事情已经结束。我只是逐字地加载了你的代码,并且它正确地工作了(用WTF填充每行都是这样)。这可能是INotifyPropertyChanged的一个问题(但是我怀疑如果你在填充构造函数中的所有内容),或者表单的DataContext没有设置为你认为的那样?不确定,但将你已经成为一个空白项目,似乎适用于我。这是.NET 4.0吗? (也用WPF窗口,而不是Windows手机页面,这可能也有所作为) – 2013-04-03 21:01:25

回答

1

我同意它似乎是一个错误。

我知道你说你不想要替代品,但为了其他人可能会读这个问题,我会忽略你。

可以解决这个问题是通过使每个属性到一个DependencyProperty

public class BaseClass : DependencyObject 
{ 
    public static readonly DependencyProperty Property1Property = DependencyProperty.Register(
     "Property1", 
     typeof(string), 
     typeof(BaseClass), 
     new PropertyMetadata(null)); 

    public string Property1 
    { 
     get { return (string)GetValue(Property1Property); } 
     set { SetValue(Property1Property, value); } 
    } 

    public static readonly DependencyProperty Property2Property = DependencyProperty.Register(
     "Property2", 
     typeof(int[]), 
     typeof(BaseClass), 
     new PropertyMetadata(null)); 

    public int[] Property2 
    { 
     get { return (int[])GetValue(Property2Property); } 
     set { SetValue(Property2Property, value); } 
    } 

    public static readonly DependencyProperty Property3Property = DependencyProperty.Register(
     "Property3", 
     typeof(object), 
     typeof(BaseClass), 
     new PropertyMetadata(null)); 

    public object Property3 
    { 
     get { return GetValue(Property3Property); } 
     set { SetValue(Property3Property, value); } 
    } 
} 


public class ModifiedClass : BaseClass 
{ 
    public static readonly new DependencyProperty Property1Property = DependencyProperty.Register(
     "Property1", 
     typeof(string), 
     typeof(ModifiedClass), 
     new PropertyMetadata(null)); 

    public new string Property1 
    { 
     get { return (string)GetValue(Property1Property); } 
     set { SetValue(Property1Property, value); } 
    } 

    public static readonly new DependencyProperty Property2Property = DependencyProperty.Register(
     "Property2", 
     typeof(long[]), 
     typeof(ModifiedClass), 
     new PropertyMetadata(null)); 

    public new long[] Property2 
    { 
     get { return (long[])GetValue(Property2Property); } 
     set { SetValue(Property2Property, value); } 
    } 

    public static readonly new DependencyProperty Property3Property = DependencyProperty.Register(
     "Property3", 
     typeof(string), 
     typeof(ModifiedClass), 
     new PropertyMetadata(null)); 

    public new string Property3 
    { 
     get { return (string)GetValue(Property3Property); } 
     set { SetValue(Property3Property, value); } 
    } 
} 
+0

不修改基地的原因是因为它是一个不同的项目和**不能**被修改。如果你可以修改基类,为什么你会打扰它的子类并使用'new'运算符?这样做是不正确的,如果你可以修改基地只是使其成为正确的类型。在数据传输对象上为持久性或UI层“增加”或“更改”属性,特别是在MVVM模式中,通常是通过子类来实现的。感谢你的努力和时间,但这不会对任何人有所帮助,因为修改基类的能力已经可以缓解这个问题。 – ckozl 2013-05-06 12:29:41

+0

@ckozl是的我明白你不想要替代品,并猜测它是出于这个确切原因。即使你可以,我可以想象为什么你不会仅仅改变基类的类型(比如重用你在项目其他地方广泛使用的大类)的一些原因。 – Murkaeus 2013-05-06 14:50:10

相关问题