为什么每一页只能有一个 Silverlight UserControl 实例?
它非常蹩脚,但我尝试将两个 UserControl
添加到我的 Silverlight Page
中,并且收到一个异常,告诉我(经过大量挖掘)相同的控件名称已被使用视觉树两次。因此,我的 UserControl
内的控件已命名,因此我一次只能将其中一个 UserControl
包含到 Page
中。这是真的吗,还是我没有意识到什么?微软真的放弃了代码重用吗?这是非常蹩脚的。
编辑
对于来到这里的人,我遇到了 System.Resources.MissingManifestResourceException 异常。花了一些时间才发现问题是可视化树中的名称不明确。希望这对您有帮助。以下是完整的异常文本。
进一步编辑
这是我的 XAML:
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock x:Name="HeaderText"
res:Strings.Assignment="Text=MachiningView_Name"/>
<sdk:TabControl Grid.Row="2">
<sdk:TabItem Header="Projects">
<ScrollViewer>
<Grid>
<controls:Organizer Margin="4" ItemsSource="{Binding ProjectSummaries}" Height="24" VerticalAlignment="Top">
<controls:Organizer.GroupDescriptions>
<controls:OrganizerGroupDescription res:Strings.Assignment="Text=NoGroupingText" IsDefault="True" />
<controls:OrganizerGroupDescription res:Strings.Assignment="Text=GroupOwnerEmailText">
<controls:OrganizerGroupDescription.GroupDescription>
<helpers:OwnerEmailGroupDescription />
</controls:OrganizerGroupDescription.GroupDescription>
</controls:OrganizerGroupDescription>
<controls:OrganizerGroupDescription res:Strings.Assignment="Text=GroupStoreNumberText">
<controls:OrganizerGroupDescription.GroupDescription>
<helpers:StoreNumberGroupDescription />
</controls:OrganizerGroupDescription.GroupDescription>
</controls:OrganizerGroupDescription>
</controls:Organizer.GroupDescriptions>
</controls:Organizer>
</Grid>
</ScrollViewer>
</sdk:TabItem>
<sdk:TabItem Header="Machine Queues">
<ScrollViewer>
<Grid>
<controls:Organizer Margin="4" ItemsSource="{Binding ProjectSummaries}" Height="24" VerticalAlignment="Top">
<controls:Organizer.GroupDescriptions>
<controls:OrganizerGroupDescription res:Strings.Assignment="Text=NoGroupingText" IsDefault="True" />
<controls:OrganizerGroupDescription res:Strings.Assignment="Text=GroupOwnerEmailText">
<controls:OrganizerGroupDescription.GroupDescription>
<helpers:OwnerEmailGroupDescription />
</controls:OrganizerGroupDescription.GroupDescription>
</controls:OrganizerGroupDescription>
<controls:OrganizerGroupDescription res:Strings.Assignment="Text=GroupStoreNumberText">
<controls:OrganizerGroupDescription.GroupDescription>
<helpers:StoreNumberGroupDescription />
</controls:OrganizerGroupDescription.GroupDescription>
</controls:OrganizerGroupDescription>
</controls:Organizer.GroupDescriptions>
</controls:Organizer>
</Grid>
</ScrollViewer>
</sdk:TabItem>
</sdk:TabControl>
</Grid>
这是我的用户控件的 XAML:
<StackPanel Orientation="Horizontal" DataContext="{Binding ElementName=UserControl}">
<sdk:Label Content="Page:" VerticalAlignment="Center" Margin="0,0,5,0"/>
<sdk:DataPager Name="_projectSummariesPager"
Margin="0,0,5,0"
DisplayMode="FirstLastPreviousNextNumeric"
Source="{Binding Path=ItemsSource}"
PageSize="10"/>
<ComboBox Name="_itemPerPageCombo"
Margin="0,0,5,0"
SelectionChanged="_itemPerPageCombo_SelectionChanged"
SelectedItem="{Binding Path=SelectedPageSize, Mode=TwoWay}"
SelectedValuePath="Value"
ItemsSource="{Binding Path=PageSizes}"/>
<ComboBox Name="_groupDescription"
Margin="0,0,5,0"
SelectionChanged="_groupDescription_SelectionChanged"
SelectedItem="{Binding Path=SelectedGroupDescription, Mode=TwoWay}"
ItemsSource="{Binding Path=GroupDescriptions}"/>
</StackPanel>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Xaml 中元素的名称在名称范围内必须是唯一的。 LoadComponent 的每次执行都会创建一个新的名称范围。因此,当使用控件的多个实例时,
UserControl
中的名称在可视化树中不会发生冲突。所以现在问题的答案是:因为你做错了什么。
目前尚不清楚“某物”是什么。也许如果您在问题中包含您的 xaml,我们可以帮助您。
编辑
因此,从字里行间看出,我猜你正在做的事情。您有一个具有多个属性的 UserControl,并且您希望 UserControl 中的控件绑定到这些属性,因此您执行以下操作:-
这表明您已将 元素。
Name="UserControl"
添加到xaml 顶部的我不太能找到一种方法来重现您的问题,但我知道早期版本的 SL 存在问题,这种方法是一个问题。我个人认为最好避免设置真正属于组件外部使用者的属性(这取决于使用 UserControl 的页面,它的名称是什么以及它是否应该有名称)。
因此,我解决这种“绑定到 UserControl 本身”问题的方法是:-
其中 LayoutRoot 是顶层
Grid
的名称,它是 UserControl 内容的根。这仍然通过 Grid 的Parent
属性绑定到 UserControl 本身。但是,这不需要将名称添加到 UserControl 本身的 xaml 中。Names given to elements in Xaml need to be unique within the namescope. Each execution of LoadComponent creates a new namescope. Hence the names within the
UserControl
will not conflict in the visual tree when multiple instances of the control were used.So the answer to question as it stands right now is: because you are doing something wrong.
What the "something" is is unclear right now. Perhaps if you included your xaml in the question we can help you.
Edit
So reading between the lines this is what I'm guessing you are doing. You have a UserControl that has a number for properties and you want controls within the UserControl to bind to these properties so you are doing this:-
This would indicate that you have added
Name="UserControl"
to the<UserControl..
element at the top of its xaml.I can't quite find a way to reproduce your problem but I am aware of problems with earlier versions of SL where this approach is a problem. Personally I think its better to avoid setting properties that really belong to the external consumer of the component (its up to the page that is using you UserControl what its name is and whether it should have name at all).
Hence my approach to solve this "Bind to the UserControl itself" sort of Problem:-
where LayoutRoot is the name of the top level
Grid
which is the root of the UserControl content. This still binds to the UserControl itself via the Grid'sParent
property. However this does not require a name to be added to the UserControl itself in its own xaml.添加 UserControl 时,请确保为其分配唯一的名称,即:
When you add the UserControl, make sure to assign it a unique name, ie: