在 xaml 中包含多个 xaml 文件(Windows 通用)

Including several xaml files in an xaml (Windows Universal)

我有两个文件 MyPagePort.xaml 和 MyPageLand.xaml 用于纵向和横向。我也想要一个"integrator"MyPage.xaml, 它使用 VisualStateManager 在纵向和横向布局之间切换。

<VisualStateManager.VisualStateGroups>
        <VisualStateGroup>
            <VisualState x:Name="Portrait>
                 include MyPagePort.xaml
            </VisualState> 
            <VisualState x:Name="Landscape">
                 include MyPageLand.xaml  
            </VisualState> 
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>

问题是我找不到包含多个 XAML 的方法。我只需要 Android.

中类似于 "incluide" 的内容

我找到了许多这样做的 WPF 示例(或),但似乎都不适用于 Windows Universal。

我认为您尝试实现纵向和横向方向的方式不是最佳做法,而且是错误的。但如果您坚持以错误的方式进行操作,您可以尝试使用 Frame 元素。

<Frame x:Name="frame" HorizontalAlignment="Left" VerticalAlignment="Top"/>

并做

frame.Navigate(typeof(YourPage));

如需最佳实践,请尝试(您也可以在那里找到集成商)

最好的方法可能是使用用户控件,这样我就可以有水平和垂直布局的两个用户控件。一种方法是使一个可见,另一种折叠并根据方向切换可见性:

   <Grid x:Name="ContentPanel">
     <layouts:MainPagePort x:Name="ContentPortrait" />
     <layouts:MainMenuLand x:Name="ContentLandscape" Visibility="Collapsed" />


   </Grid> 

   <VisualStateManager.VisualStateGroups>
        <VisualStateGroup>
            <VisualState x:Name="Portrait"/>
            <VisualState x:Name="Landscape">
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames
                        Storyboard.TargetName="ContentPortrait"
                        Storyboard.TargetProperty="Visibility">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed" />
                    </ObjectAnimationUsingKeyFrames>

                    <ObjectAnimationUsingKeyFrames
                        Storyboard.TargetName="ContentLandscape"
                        Storyboard.TargetProperty="Visibility">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </VisualState>
        </VisualStateGroup >
    </VisualStatemanager.VisualStateGroups>

不过这种方法有一个很大的缺点(这可能就是为什么 Stamos 不建议支持两种布局的原因),但是有一个解决办法:如果布局很复杂,不用的会浪费 space 并使应用程序难以 运行(尤其是 phone)。解决方法是删除不必要的布局树和需要的布局树:

   public async void Window_SizeChanged  (object sender, WindowSizeChangedEventArgs e) 
   {

         ApplicationViewOrientation newOrientation = OrientationUtils.getScreenOrientation();
         if (newOrientation != curOrientation)
          {
              curOrientation = newOrientation;
              setupOrientation();
          }
     }

     private void setupOrientation()
     {
          Panel contentPane = FindName("ContentPanel") as Panel;
          if (contentPane != null) {
             var children = contentPane.Children;
             try
             {
                 children.Remove(children.Where(child => ((Control)child).Name.Equals("ContentChild")).First());
             }


              String layoutPath = "ms-appx:///MainPage" +
                  ((curOrientation == ApplicationViewOrientation.Portrait) ? "Port" : "Land") +
                         ".xaml";
              var newElement = new UserControl();
              Application.LoadComponent(newElement, new Uri(layoutPath),
                          ComponentResourceLocation.Application);
              newElement.Name = "ContentChild";
              children.Add(newElement);
        }
    }