Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
XamlReader Wall
Message
De
09/03/2009 13:33:50
 
 
À
08/03/2009 15:44:18
Information générale
Forum:
ASP.NET
Catégorie:
Windows Presentation Foundation (WPF)
Titre:
Versions des environnements
Environment:
C# 3.0
Divers
Thread ID:
01386282
Message ID:
01386696
Vues:
38
>>>Hi,
>>>
>>>How to avoid the problem below (I see what the problem is : you get two items with the same name) but I can't find any working code for a fix. Simple UserControl:
UserControl x:Class="WpfApplication1.MinimalControl"
>>>    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>>>    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>>>    Height="300" Width="300">
>>>    <TextBlock x:Name="Fred" />
>>></UserControl>
Used in this form:
<Window x:Class="WpfApplication1.MinimalForm"
>>>    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>>>    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>>>    xmlns:uc="clr-namespace:WpfApplication1"
>>>    Title="MinimalForm" Height="300" Width="300">
>>>    <StackPanel>
>>>        <Canvas x:Name="ACanvas">
>>>            <uc:MinimalControl />
>>>            <uc:MinimalControl />
>>>        </Canvas>
>>>        <Button Click="Button_Click" >Try</Button>
>>>    </StackPanel>
>>></Window>
Here's the test code behind:
using System.Windows;
>>>using System.Windows.Controls;
>>>
>>>namespace WpfApplication1
>>>{
>>>    /// <summary>
>>>    /// Interaction logic for MinimalForm.xaml
>>>    /// </summary>
>>>    public partial class MinimalForm : Window
>>>    {
>>>        public MinimalForm()
>>>        {
>>>            InitializeComponent();
>>>        }
>>>
>>>        private void Button_Click(object sender, RoutedEventArgs e)
>>>        {
>>>            string s = System.Windows.Markup.XamlWriter.Save(ACanvas);
>>>            System.IO.StringReader stringReader = new System.IO.StringReader(s);
>>>            System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(stringReader);
>>>            //Exception here:
>>>            Canvas x = (Canvas)System.Windows.Markup.XamlReader.Load(xmlReader);
>>>        }
>>>    }
>>>}
Any way of manipulating the NameScope (or something) to avoid this ?
>>>Regards,
>>>Viv
>>
>>I don't see an easy way around it. But here is one solution:
>>
>>http://www.ddj.com/windows/209800469?pgno=2
>
>Hi John,
>Thanks for the link. Reading through I get a glimmer of how it might help (I started thinking along the same lines - but using XSLT instead of Regex). But I can't find the complete source code to download and play with. Any clues?
>
>UPDATE: I'm don't think my original reading of the problem was correct (although all web references that I found implied this) The same error occurs when only one instance of MinimalControl is used in the form so the conflict seems to lie in the fact that the controls name has already been set in the constructor when the XAMLReader recreates the control. As a simple test I just removed the Name attribute from the XML string before passing it to the XAMLReader and VIOLA - it worked. Currently testing with more complex examples - so far so good. But there are other issues that may still prevent XAMLWriter/Reader being a viable solution (resources become 'hard-coded', event handlers must be defined in code, etc)
>
>I'm still paralleling this with looking at the ISerializationSurrogate approach (see other thread) but not seeing a proper solution there either....
>
>Thx,
>Viv

I use the xaml writer/reader, but I'm using it with resources, so there is no codebehind to worry about?

This looked like a fragment of a save and open program. Is that correct? If not what are you trying to do here?
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform