2013-04-03 17 views
6

in primo piano voglio dire: per favore non suggerire soluzioni alternative, a meno che non sia possibile eseguirlo senza modificare i Tipi che sono di il modello BaseXXXXXXAssociazione di una proprietà che ne nasconde un'altra in WinRT XAML (Windows8, Metro, App Store Windows)

che ha detto, questo comportamento va ben al di là perplessi per quanto mi riguarda, sembrerebbe che utilizzando la parola chiave new per nascondere una proprietà in C# significa che WinRT XAML (Windows8, Metro, Windows App Store) non funziona più correttamente. Non ho idea del perché questo sia.

Ecco un esempio:

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> 

Il risultato fin troppo corretta sembra qualcosa di simile: what is going on

in pratica, ognuna di quelle righe vuote deve essere riempita qualcuno di voi XAML hotshots ha idea del perché questi binding non stanno riuscendo e c'è qualcosa che può essere fatto per aggirare ciò che posso solo supporre sia un bug insignificante ? Qualsiasi aiuto o intuizione sarebbe molto apprezzato, grazie in anticipo ... -CK

aggiornamento: discarica uscita ho dimenticato

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

Aggiornamento:

Bug depositato presso Microsoft: https://connect.microsoft.com/VisualStudio/feedback/details/782993/binding-a-property-that-hides-another-in-winrt-xaml quindi vedremo come va

+1

C'è qualcosa nell'output affermando che il fallito vincolante? Presumo che si confonda da quando 2 proprietà dell'oggetto hanno lo stesso nome, anche se non riesco a trovare alcuna letteratura sull'argomento. –

+0

@KevinDiTraglia scusa ho dimenticato di includere il dump del bind, grazie per avermelo ricordato, l'errore ha ancora meno senso dato che sembra che nessuna proprietà di quel nome esista ... un messaggio di conflitto che almeno comprenderei – ckozl

+1

Hmm , qualcosa è finito. Ho appena caricato il tuo codice testualmente e ha funzionato correttamente (riempito ogni riga con WTF sta succedendo con questo). Potrebbe trattarsi di un problema con INotifyPropertyChanged (ma ne dubito se si sta compilando tutto nel costruttore), o forse il DataContext del modulo non è impostato su ciò che si pensa che sia? Non sono sicuro, ma copiare quello che hai in un progetto vuoto sembra funzionare per me. È questo .NET 4.0? (Inoltre ho usato una finestra WPF, piuttosto che una pagina di Windows Phone, che può anche fare la differenza?) –

risposta

0

scopre questo è un "bug non supportato" qualunque cosa significhi ... Ecco la citazione diretta:

Ciao, mi dispiace per la nostra risposta in ritardo. Fino ad ora non supportiamo questo bug. per favore vai a http://support.microsoft.com o chiama 1-800-MICROSOFT per assistenza . Grazie.

... spero che questo sarà fissato in .NET 4.5.1

visualizzare il bug report su: https://connect.microsoft.com/VisualStudio/feedback/details/782993/binding-a-property-that-hides-another-in-winrt-xaml

1

Sono d'accordo che sembra un bug.

So che hai detto che non vuoi alternative, ma per il bene di chiunque altro possa leggere questa domanda, ti ignorerò.

È possibile risolvere questo problema è di rendere le proprietà ciascuno in un 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

il motivo per non modificare la base è perché fa parte di un altro progetto e ** non possono ** essere modificati. se è possibile modificare la classe base, perché dovresti preoccuparti della sottoclassi e dell'uso dell'operatore 'new'? farlo non è corretto, se è possibile modificare la base basta renderlo il tipo giusto in primo luogo. "aggiungere" o "modificare" le proprietà su un oggetto di trasporto dati per il livello di persistenza o UI tramite sottoclassi, in particolare in un pattern MVVM, è comune. Grazie per il tuo impegno e il tuo tempo, ma questo non aiuterà nessuno perché la possibilità di modificare la classe base alleggerirà già il problema. – ckozl

+0

@ckozl sì, ho capito che non volevi alternative e indovinato era per quella ragione esatta. Anche se tu potessi, posso immaginare un paio di motivi per cui non cambieresti semplicemente il tipo della classe base (come ad esempio la riprogettazione di una classe estesa che stai usando estensivamente in altre parti del progetto). – Murkaeus

Problemi correlati