繁体   English   中英

绑定在WinRT XAML中隐藏另一个属性的属性(Windows8,Metro,Windows应用商店应用)

[英]Binding a property that hides another in WinRT XAML (Windows8, Metro, Windows Store App)

在前面我想说:请不要建议替代解决方案,除非您可以在不修改BaseXXXXXX模式的类型的情况下完成它

也就是说,就我而言,这种行为远远超出了困惑,似乎使用new关键字隐藏C#中的属性意味着WinRT XAML(Windows8,Metro,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>

完全错误的结果看起来像: 到底是怎么回事

基本上,每个空白行应该填写你们中的任何一个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所以我们将看到这是怎么回事

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

我知道你说你不想要其他选择,但为了其他任何可能阅读这个问题的人,我会不理你。

您可以通过将每个属性设置为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); }
    }
}

原来这是一个“不支持的错误”无论这意味着什么...这是直接引用:

嗨,对不起我们迟到的回复。 到目前为止,我们不支持此错误。 请访问http://support.microsoft.com或致电1-800-MICROSOFT寻求帮助。 谢谢。

...希望这将在.NET 4.5.1中修复

查看错误报告: https//connect.microsoft.com/VisualStudio/feedback/details/782993/binding-a-property-that-hides-another-in-winrt-xaml

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM