为什么每一页只能有一个 Silverlight UserControl 实例?

发布于 2024-12-04 17:49:09 字数 5203 浏览 1 评论 0 原文

它非常蹩脚,但我尝试将两个 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>

Its pretty lame, but I tried adding two UserControl's to my Silverlight Page, and I get an exception telling me (with extensive digging) that the same control name has been used to the visual tree twice. So the controls inside my UserControl are named, so therefore I can include only one of this UserControl into the Page at a time. Is this true, or am I not realizing something? Has Microsoft truly given up on code-reuse? This is very lame.

Edits

For people who come here, I was getting this a System.Resources.MissingManifestResourceException exception. It took some time to figure out that the problem was ambiguous names in the visual tree. Hopefully this helps you. Below is the full exception text.

Further Edits

Here is my 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>

And here is my user control's 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亣腦蒛氧 2024-12-11 17:49:09

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

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

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

编辑

因此,从字里行间看出,我猜你正在做的事情。您有一个具有多个属性的 UserControl,并且您希望 UserControl 中的控件绑定到这些属性,因此您执行以下操作:-

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

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

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

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

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

其中 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:-

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

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:-

 <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.

锦欢 2024-12-11 17:49:09

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

<Page ...
    <StackPanel>
         <local:YourUserControl x:Name="Foo" />
         <!-- Make sure not to duplicate x:Name/Name here! -->
         <local:YourUserControl x:Name="Bar" />
    </StackPanel>
</Page>

When you add the UserControl, make sure to assign it a unique name, ie:

<Page ...
    <StackPanel>
         <local:YourUserControl x:Name="Foo" />
         <!-- Make sure not to duplicate x:Name/Name here! -->
         <local:YourUserControl x:Name="Bar" />
    </StackPanel>
</Page>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文