2

我为我的 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 中看到的,我在网格控件中使用了 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

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

4

2 回答 2

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)添加到模板中。

于 2013-02-24T03:12:34.083 回答
0

您可以做的是使用 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>

请注意IsEditingModeXAML 中的设置方式。当然,您也可以从代码中设置它,public bool IsEditingMode使用IsEditingModeProperty.

于 2013-02-18T14:41:44.097 回答