문제

Windows 7/8 전화 개발 경기장에 새로운 것입니다 ... 나는 Windows Phone을 엄청나게 즐기고 있습니다 ... 그러나 학습 곡선이 있었고, 알아야 할 것은별로 있습니다.

라고 말하고있는 것은 N 개의 피벗 페이지를 표시 할 데이터 구조에 동적으로 바인딩되는 페이지를 만드는 것입니다. 각 피벗 페이지에는 콘텐츠를 표시 할 XAML이 다릅니다.

이 코드 프로젝트 기사 ( http://www.codeproject.com/articles/113152/applying-data-templates-dynamically-wy-type-in-wp7 )를 사용하여 디스플레이를 제어하려면 목록 상자를 사용하지만 내가 관심있는 것은 똑같은 일을하고 있지만, 피벗 페이지가있는 것입니다.

나는 예를 들어 최선을 다해 배웁니다 ... 여기서 사용하고자하는 데이터를 컨트롤에 바인딩하는 클래스가 있습니다 ...

  public class ParkingLot : List<Car>
  {
    public ParkingLot() { }

    // this will be the pivot page title
    public string Lot { get; set; }

    // the list of cars will be displayed on the page
  }
  public class Car
  {
    public Car() { }

    // this will be the data that is displayed in the pivot page for each car
    public string Width { get; set; }
    public string Length { get; set; }
  }


  public class Library : List<Book>
  {
    public Library() { }

    // this will be the pivot page title
    public string Location { get; set; }

    // the list of books will be displayed on the page
  }
  public class Book
  {
    public Book() { }

    // this is the data that will be displayed for each book
    public string ISBN { get; set; }
    public string Title { get; set; }
  }
.

여기에 모든 코드를 게시하는 것이 좋을지 모르겠습니다 ... 또는 코드 프로젝트에 대한 기사를 모두 보면서, 나는 기사에서 수정 한 코드를 포스트합니다. . 누군가가 나를 도울 수 있음을 희망한다.

xaml :

    <phone:PhoneApplicationPage x:Class="dynDataTemplateTest.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:loc="clr-namespace:dynDataTemplateTest.View"

                        xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"

                        FontFamily="{StaticResource PhoneFontFamilyNormal}"
                        FontSize="{StaticResource PhoneFontSizeNormal}"
                        Foreground="{StaticResource PhoneForegroundBrush}"
                        SupportedOrientations="Portrait"
                        Orientation="Portrait"
                        mc:Ignorable="d"
                        d:DesignWidth="480"
                        d:DesignHeight="768"
                        shell:SystemTray.IsVisible="True"
                        DataContext="{Binding Main, Source={StaticResource Locator}}">

<!--LayoutRoot contains the root grid where all other page content is placed-->
<Grid x:Name="LayoutRoot"
      Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel"
                Grid.Row="0"
                Margin="24,24,0,12">
        <TextBlock x:Name="ApplicationTitle"
                   Text="{Binding ApplicationTitle}"
                   Style="{StaticResource PhoneTextNormalStyle}" />
        <TextBlock x:Name="PageTitle"
                   Text="{Binding PageName}"
                   Margin="-3,-8,0,0"
                   Style="{StaticResource PhoneTextTitle1Style}" />
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentGrid"
          Grid.Row="1">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <loc:DynamicContentControl Content="{Binding SelectedItem}"
                            HorizontalContentAlignment="Stretch"
                            VerticalContentAlignment="Stretch" />

        <controls:Pivot ItemsSource="{Binding Path=Items}" SelectedItem="{Binding Path=SelectedItem}" >

            <controls:Pivot.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=DisplayName}" FontSize="30" FontWeight="Bold" Margin="5"/>
                </DataTemplate>
            </controls:Pivot.HeaderTemplate>


            <controls:Pivot.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"></StackPanel>
                </ItemsPanelTemplate>
            </controls:Pivot.ItemsPanel>

        </controls:Pivot>

    </Grid>
</Grid>
.

여기에 DataTemplatesElector 클래스

입니다.
public static class DataTemplateSelector
{

    public static DataTemplate GetTemplate(ViewModelBase param)
    {
        Type t = param.GetType();
        return App.Current.Resources[t.Name] as DataTemplate;
    }
}
.

여기에는 동적 컨텐츠 컨트롤이 있습니다. Public Class DynamicContentControl : ContentControl. { 보호 된 재정의 void onContentChanged (Object OldContent, Object NewContent) { Base.OncontentChanged (OldContent, NewContent); 이 .ContentTemplate= msator.model.datemplateSelector.GetTemplate (뷰 모드베이스로 NewContent); }
}

여기에 첫 번째보기 XAML입니다.

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
    <TextBlock Margin="20" Foreground="Green" FontSize="32"
           FontWeight="Bold" Text="{Binding Path=FirstProperty}"
           ></TextBlock>
</Grid>
.

(두 번째보기 XAML은 첫 번째보기 일 수 있으며 색상 변경)

여기에 FirstViewModel 클래스 (기사에서)

    public class FirstViewModel : SelectableViewModel
{
    public FirstViewModel()
    {
        DisplayName = "First";
        FirstProperty = "this is the first property";
    }


    private string firstProp;
    public string FirstProperty
    {
        get { return firstProp; }
        set
        {
            if (firstProp != value)
            {
                firstProp = value;
                RaisePropertyChanged("FirstProperty");
            }
        }
    }

}
.

여기는 selectableView 모델 클래스

입니다.
 public class SelectableViewModel : ViewModelBase
 {
    public SelectableViewModel()
    {
    }

    string dispName;
    public string DisplayName
    {
        get { return dispName; }

        set
        {
            if (dispName != value)
            {
                dispName = value;
                RaisePropertyChanged("DisplayName");
            }
        }
    }
}
.

여기에 기본보기 모델 클래스입니다.

    public class MainViewModel : ViewModelBase
{
    public string ApplicationTitle
    {
        get
        {
            return "Dynamic Data Templates";
        }
    }

    public string PageName
    {
        get
        {
            return "Main page";
        }
    }

    private List<SelectableViewModel> viewModels;
    public MainViewModel()
    {
        viewModels = new List<SelectableViewModel>();

        viewModels.Add(new FirstViewModel());
        viewModels.Add(new SecondViewModel());

        SelectedItem = viewModels[0];
    }

    public List<SelectableViewModel> Items
    {
        get
        {
            return viewModels;
        }
    }

    SelectableViewModel selItem;
    public SelectableViewModel SelectedItem
    {
        get { return selItem; }
        set 
        {
            if (selItem != value)
            {
                selItem = value;
                RaisePropertyChanged("SelectedItem");
            }
        }
    }
}
.

돕기 위해 다시 감사드립니다!

도움이 되었습니까?

해결책

아직도 배우고있는 것에 따라, N 개의 피벗 항목이 왜 나쁜 아이디어인지 설명하겠습니다.

  1. 단일 페이지의 콘텐츠 양으로 인해 성능 문제가 발생할 수 있습니다. 목록을 사용하여 항목을 가상화 할 수 있습니다. 피벗 컨트롤은 동적으로 추가 된 PivotItems의 가상화를 지원하지 않습니다.

  2. 원하는 사람에게 빨리 얻을 수있는 방법이없는 경우가 많은 피벗 테마가있을 때 원하는 항목으로 이동하는 것이 어렵습니다. 피벗에 30 개 항목이 있지만 15 번째로 가고 싶었습니다. 그것은 많은 스 와이프가 필요할 것이고 그것을 빨리하고있는 경우 원하는 것을 지나서 쉽게 가기가 쉽습니다.

  3. 피벗 컨트롤은 두 가지 목적 중 하나에 사용되는 것입니다.

    1. 데이터 세트의 다른 뷰를 표시합니다. I.E. 이메일 앱은 각 PivotItem의 사서함의 다양한보기, "모두", "읽지 않음", "플래그"및 "긴급"을 위해 필터링합니다.

    2. 관련 데이터의 다른 부분을 표시합니다. I.E. 개별 연락처 / 사람을 볼 때 "프로필", "새로운 기능", "사진", "기록",

    3. 템플릿 목록의 N 개의 컬렉션과 같은 방대한 양의 콘텐츠를위한 컨테이너로서 피벗 컨트롤을 사용해야하는 것은 의무가 아닙니다.
      성능과 유용성 문제를 피하기 위해 피벗의 최대 항목 수가 7이어야한다는 것이 제안됩니다.

      모든 것이 앱을 사용하는 사람들을위한 개발자 및 유용성 문제로서의 성능 문제를 일으킬 수있는 방법 중 하나에서 피벗 컨트롤을 사용하지 않을 수 있습니다.
      둘 다 피해야 할 시나리오입니다.

      죄송합니다. 이것은 귀하의 질문에 대한 직접적인 답변이 아니라 더 나은 앱 (또는 앱)을 개발하는 데 도움이 될 것입니다. ;)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top