2

它很蹩脚,但我尝试UserControl在我的 SilverlightPage中添加两个,我得到一个异常,告诉我(通过大量挖掘)相同的控件名称已用于视觉树两次。所以我里面的控件被命名了UserControl,所以我一次只能包含其中一个。这是真的,还是我没有意识到什么?微软真的放弃了代码重用吗?这很蹩脚。UserControlPage

编辑

对于来这里的人,我得到了 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>
4

2 回答 2

1

为Xaml中的元素指定的名称在名称范围内必须是唯一的。LoadComponent 的每次执行都会创建一个新的名称范围。因此,UserControl当使用控件的多个实例时,可视化树中的名称不会发生冲突。

所以现在的问题的答案是:因为你做错了什么。

目前还不清楚“某物”是什么。也许如果您在问题中包含您的 xaml,我们可以为您提供帮助。

编辑

所以在字里行间阅读这就是我猜你正在做的事情。您有一个具有属性编号的 UserControl,并且您希望 UserControl 中的控件绑定到这些属性,因此您可以这样做:-

 <StackPanel Orientation="Horizontal" DataContext="{Binding ElementName=UserControl}">  

这将表明您已添加Name="UserControl"<UserControl..其 xaml 顶部的元素。

我找不到重现问题的方法,但我知道早期版本的 SL 存在问题,这种方法存在问题。就我个人而言,我认为最好避免设置真正属于组件的外部使用者的属性(它取决于使用 UserControl 的页面它的名称是什么以及它是否应该有名称)。

因此,我解决这种“绑定到 UserControl 本身”之类的问题的方法:-

 <StackPanel Orientation="Horizontal" DataContext="{Binding ElementName=LayoutRoot.Parent}">

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's Parent property. However this does not require a name to be added to the UserControl itself in its own xaml.

于 2011-09-14T19:11:45.857 回答
0

添加 UserControl 时,请确保为其分配一个唯一名称,即:

<Page ...
    <StackPanel>
         <local:YourUserControl x:Name="Foo" />
         <!-- Make sure not to duplicate x:Name/Name here! -->
         <local:YourUserControl x:Name="Bar" />
    </StackPanel>
</Page>
于 2011-09-14T18:44:06.180 回答