【问题标题】:How to access Components/Elements of UserControl in Windows Phone 8如何在 Windows Phone 8 中访问 UserControl 的组件/元素
【发布时间】:2013-02-18 14:17:16
【问题描述】:

我为我的 windows phone 8 开发了 UserControl,如下所示。

<UserControl x:Class="SpinrWindowsMobile.UserControls.ProgressiveLongListSelector"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="480" d:DesignWidth="480">

    <Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <phone:LongListSelector Grid.Row="0"  Name="longlistselector">
        </phone:LongListSelector>
        <StackPanel Grid.Row="1">
            <ProgressBar Name="listProress" IsIndeterminate="True"></ProgressBar>
            <TextBlock Name="ProgressText" Text="Loading..."></TextBlock>
        </StackPanel>
    </Grid>
</UserControl>

正如您在上面的 xaml 中看到的,我在 Grid Control 中使用了 LongListSelector 和 StackPanel。 我在我的 MainPage.xaml 中使用了这个控件,如下所示。

<phone:PhoneApplicationPage
    x:Class="SpinrWindowsMobile.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:UserControls="clr-namespace:SpinrWindowsMobile.UserControls"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">
    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">     
            <UserControls:ProgressiveLongListSelector>

            </UserControls:ProgressiveLongListSelector>

     </Grid>    
</phone:PhoneApplicationPage>

到目前为止,这很好,但我想做如下的事情。

  <UserControls:ProgressiveLongListSelector>
                  <UserControls:ProgressiveLongListSelector.longlistselector 
                 ItemsSource="Binding" ItemTemplate="{staticresource myTemplate}">
                  </UserControls:ProgressiveLongListSelector.longlistselector>
    </UserControls:ProgressiveLongListSelector>

如何访问 UserControl 的元素/组件 的 longlistselector? 这样做的好处是I can directly set the LongListSelector Properties in the xaml(in which i am embedding My usercontrol) itself。对我来说,这种东西是今天的要求。

谁能指导我如何做到这一点?

【问题讨论】:

    标签: c# xaml windows-phone-8 windows-phone longlistselector


    【解决方案1】:

    由于您正在扩展和修改 LongListSelector,我建议对 LongListSelector 进行子类化和重新模板化,而不是将其放在 UserControl 中。这将允许您访问 LongListSelector 上的所有现有属性和方法,并像使用 LongListSelector 一样使用新的 ProgressiveLongListSelector。

    首先,您可以创建一个继承 LongListSelector 的新类:

    public class ProgressiveLongListSelector : LongListSelector {
    
        public ProgressiveLongListSelector() {
            DefaultStyleKey = typeof(ProgressiveLongListSelector);
        }
    }
    

    注意 DefaultStyleKey。这就是新控件模板的来源。

    现在您可以将以下样式放置在您的 App.xaml 资源中。请注意,TargetTypeProgressiveLongListSelector。这就是 DefaultStyleKey 将如何找到您的新默认样式。

        <Style TargetType="phoneApp2:ProgressiveLongListSelector">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="phoneApp2:ProgressiveLongListSelector">
                        <Grid Background="{TemplateBinding Background}" d:DesignWidth="480" d:DesignHeight="800">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="ScrollStates">
                                    <VisualStateGroup.Transitions>
                                        <VisualTransition GeneratedDuration="00:00:00.5" />
                                    </VisualStateGroup.Transitions>
                                    <VisualState x:Name="Scrolling">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="VerticalScrollBar" />
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="NotScrolling" />
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Grid Margin="{TemplateBinding Padding}">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="auto" />
                                </Grid.ColumnDefinitions>
                                <ViewportControl x:Name="ViewportControl" HorizontalContentAlignment="Stretch" VerticalAlignment="Top" />
                                <ScrollBar x:Name="VerticalScrollBar" Grid.Column="1" Margin="4,0,4,0" Opacity="0" Orientation="Vertical" />
                            </Grid>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    

    此样式/模板是默认 LongListSelector 模板(从 Blend 中提取)的副本。从这里,您可以将 UserControl 中的其他元素(例如 ProgressBar 和 TextBlock)添加到模板中。

    【讨论】:

    • 嗨,格雷,我将尝试实施您的回答,并让您知道进展如何,谢谢哥们。
    • Gray 在 XAML 中使用 ProgressiveLongListSelector 时,编译器给出错误,无法将指定的值分配给集合。预期以下类型:UIElement。
    • 嗨NeedForSpeed,我刚刚测试了它,没有任何问题。你做过修改吗?你是如何在 XAML 中使用它的?如果您可以发布两者的示例,我可以看看。
    【解决方案2】:

    您可以做的是使用 DependencyProperty 机制公开 UserContorl 的属性。然后,您可以从使用该 UserControl 的页面中将它们设置为 XAML。我不确定您是否要公开 VisualTree 的所有位,因为这可能会在将来发生变化。但是您可以公开一些间接影响 UserControl 行为的属性。

    这是一个如何做到这一点的示例:

    (这个例子取自我的代码,但我想你可以弄清楚如何适应它)

    首先你在你的 UserControl 中声明一个 DependencyProperty:

    public partial class MyUserControl : UserControl
    {
        public bool IsEditingMode
        {
            get { return (bool)GetValue(IsEditingModeProperty); }
            set { SetValue(IsEditingModeProperty, value); }
        }
    
        public static readonly DependencyProperty IsEditingModeProperty =
            DependencyProperty.Register("IsEditingMode", typeof(bool), typeof(MyUserControl), new PropertyMetadata(false, IsEditingModeChanged));
    }
    
    private static void IsEditingModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        // this will be called when someone would set the exposed property to some new value
    }
    

    接下来将 MyUserControl 添加到某个 Page 并设置该属性:

    <phone:PivotItem Header="{Binding Path=LocalizedResources.MyPivotHeader, Source={StaticResource LocalizedStrings}}" Margin="0">
        <my:MyUserControl x:Name="People" IsEditingMode="True"/>
    </phone:PivotItem>
    

    注意IsEditingMode 是如何在 XAML 中设置的。当然,您也可以从代码中设置它,使用 public bool IsEditingMode 围绕 IsEditingModeProperty 的属性包装器。

    【讨论】:

    • 能否请您举例说明如何实现?
    • 感谢您的回答但我想在 xaml 中公开 LongListSelector,以便我可以直接设置特定于 LongListSelector 的属性,例如 itemTemplate 属性、itemSource 属性我已经编辑了问题,以便您获得更多见解我想要达到的目标。
    • 在已编辑的问题中,您可以看到我想如何在 XAML 中使用 LonglistSelector(我的控件的组件之一)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多