Naming Controls in saperate Xaml Files

Hi,

I am working with the application which has main window containing frame control.

I am calling different xaml content from the saperate files based on user input.

My problem is that i am not able to give names to any controls withing thaose saperate xamle files.

It gives me following error, where btnSave is a button control and Red.xaml is a file being called from main window.

'btnSave' value cannot be assigned to property 'Name' of object 'System.Windows.Controls.Button'. Cannot register duplicate Name 'btnSave' in this scope. Error at object 'btnSave' in markup file 'Red.xaml' Line 12 Position 11.

Though, i have never asigned this name(btnSave) to any of the controls throughout the application, it gives me this error.

So, without giving name it's of no use for me.

Can you please help me in this ?

Thanks !!

[965 byte] By [Mann..] at [2008-1-10]
# 1
What version of VS are you using? I have never seen the compiler make a mistake regarding duplicate names before. Could you post red.xaml?
RadekCerny at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 2

How are you setting the content of the frame? You are using LoadComponent() to load your markup, right? It should work if you do frame.Content = Application.LoadComponent( you uri here );

Based on the error, I'm guessing you are trying to deserialize a single markup file twice within the same namescope.

Hard to say for sure without knowing what's going on in red.xaml, but using x:Name might be an option. Kinda depends on your reason for specifying the Name property.

Dr.WPF at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 3

Hi Radek,

I am using VS 2005.

Finde below the simple Red.xaml

<Page

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

x:Class="RandDonResourceDictionary.Page2"

x:Name="Page"

WindowTitle="Page"

FlowDirection="LeftToRight"

Width="640" Height="480"

WindowWidth="200" WindowHeight="100">

<Grid Background="Red" >

<Button x:Name="btnSave" HorizontalAlignment="Left" Margin="40,62,0,0" VerticalAlignment="Top" Width="85" Height="34" Content="Button" Background="#FFF40000"/>

</Grid>

</Page>

and Following line where i want to call Red . xaml at runtime.

<Frame Margin="13,0,10,5" x:Name="frmContainer" VerticalAlignment="Bottom" Height="129" Content="Frame" NavigationUIVisibility="Hidden" />

c# statment : frmContainer.Source = new Uri("pack://application:,,/" + "Red.xaml");

it gives the error.

And when later i change the source to Green. Xaml it gives the same error for Green.xaml's controls also.

Mann.. at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 4

Disregard the earlier post... It's the absolute uri that is causing you problems. For embedded XAML pages having code behind, you can only navigate the frame via a relative uri.

If you have embedded, noncompiled XAML (say something that would run in XamlPad without referencing external assemblies), then you can use a pack uri, but in your case, clearly you have a page that is calling InitializeComponent.

Any of the following should work for you:

frmContainer.Source = new Uri("Green.xaml", UriKind.Relative);

frmContainer.Navigate(new Uri("Blue.xaml", UriKind.Relative));

frmContainer.Content = Application.LoadComponent(new Uri("Red.xaml", UriKind.Relative));

Note: The latter option will result in an object on your nav stack rather than a uri. This may not be what you desire (unless you need to preserve state in the nav journal).

Dr.WPF at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 5

Thanks Dr. WPF !!

After a Long time i got this solution !!

Thank you verymuch..

KUDOS for you !!!

Mann.. at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...

Visual Studio Orcas

Site Classified