2014-10-18 47 views
1

我正在创建一个Windows通用应用程序(WinRT和WP8.1)。 我想要的是让用户在同一页面的两种不同布局之间进行选择。 以下是简单表示。 我想找到最简单,最懒惰的方法(我知道我可以创建两个页面,但必须有更好的方法。) 我已经创建了1页(后面有500行C#代码来处理控件交互)现在需要添加第二个布局。这两个布局具有完全相同的功能,并且控件具有相同的名称,它们的排列方式不同。背后有两个相同代码的XAML页面

在Android中我可以这样做:

if(IsLayout1Selected) 
    setContentView(R.layout.activity_d2p_layout1); 

else 
    setContentView(R.layout.activity_d2p_layout2); 

预先感谢您的帮助!

布局1:

<Page x:Name="d2pPageLayout1" 
x:Class="_MyApp.D2P" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:_MyApp" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" Unloaded="PageUnloaded" Loaded="PageLoaded" 
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <Grid x:Name="mainGrid"> 
     <Button x:Name="button1" VerticalAlignment="Top" Click="buttonClick"/> 
     <TextBlock x:Name="textblock1" Text="Hello"/> 
     <!-- Of course there are a lot more elements and they have many layout properties that differ --> 
    </Grid> 
</Page> 

布局2:

<Page x:Name="d2pPageLayout2" 
x:Class="_MyApp.D2P" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:_MyApp" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" Unloaded="PageUnloaded" Loaded="PageLoaded" 
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <Grid x:Name="mainGrid"> 
     <Button x:Name="button1" VerticalAlignment="Bottom" Click="buttonClick"/> 
     <TextBlock x:Name="textblock1" Text="Hello"/> 
     <!-- Of course there are a lot more elements and they have many layout properties that differ --> 
    </Grid> 
</Page> 

代码后面(对于相同布局1和布局2):

namespace _MyApp 
{ 
    public sealed partial class D2P : Page 
    { 
    public D2P() 
     { 
      this.InitializeComponent(); 
     } 

    private void PageLoaded(object sender, RoutedEventArgs e) 
     { 
      // do stuff here 
     } 

    private void PageUnloaded(object sender, RoutedEventArgs e) 
     { 
      // do some other stuff here 
     } 

    private void buttonClick(object sender, RoutedEventArgs e) 
     { 
      // handle buttonClick here 
      // a lot of this code also references the controls directly e.g. 
      textblock1.Text = "Button 1 was clicked"; 
     } 
    } 
} 

编辑:

感谢来自阿克沙伊SOAM尖端我就非常接近我想要的东西用的DataTemplate和ContentControl中,但现在这会导致另一个问题,打破我的代码。我的代码直接访问很多元素。 例子:

textblock1.Text = "Button 1 was clicked"; 

因为我的整个页面的内容现在包裹的DataTemplate内无法访问的元素了。我该怎么做呢?

当前实现:

XAML:

<Page.Resources> 
    <DataTemplate x:Key="template1"> 
     <Button x:Name="button1" VerticalAlignment="Bottom" Click="buttonClick"/> 
     <TextBlock x:Name="textblock1" Text="Hello"/> 
    </DataTemplate> 
</Page.Resources> 
<ContentControl x:Name="contentControl" Content="{Binding}"/> 

后面的代码:

contentControl.ContentTemplate = template1; 
this.DataContext = template1; 
+0

你可以使用一个内容控件和一个数据模板 – thumbmunkeys 2014-10-18 12:21:56

+0

@thumbmunkeys谢谢,这让我有一半在那里,我已经更新了问题与一个由此产生的问题。 – Johis 2014-10-18 19:00:08

回答

1

至于你的新的问题,您可以浏览的VisualTree并提取出来。

这里是我的话题旧答案之一:VisualTree Helper


但是不要那样做。您想要使用MVVM模式并将.Text绑定到ModelView的属性,并将该Button绑定到该模型视图的命令。通过这种方式,您可以更改属性并通过INotifyPropertyChanged ---更改.Text属性,它将自动更新带有新更改的文本框。


一个非常容易阅读的MVVM文章我写了回答相关的SO问题。它也有Button Command。不幸的是,这不是WinForms,所以你必须遵守规则。

Implement a ViewModel Single Command with CommandParamater

+0

谢谢。将来,我将确定从MVVM模式开始。但是对于这个几乎完成的应用程序,我依赖于依赖于传递给它的控件的许多函数(主要是动画内容)。将所有内容转移到MVVM将需要很长时间。我曾希望能以某种方式改变'this.InialializeComponent();'调用来基于另一个XAML页面进行初始化,就像我可以在Android中使用'setContentView'一样。我目前正试图让VisualTreeHelper工作。 – Johis 2014-10-20 23:20:45

0

你可以建立一个以上的XAML可以在同一页喜欢这里: 我建8 XAML针对不同的屏幕尺寸相同的MainPage

public sealed partial class MainPage : Page 
{ 


    public MainPage() 
    { 

     Rect bounds = ApplicationView.GetForCurrentView().VisibleBounds; 
     double scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel; 
     Size size = new Size(bounds.Width * scaleFactor, bounds.Height * scaleFactor); 



     string resourceName; 

     if (size.Width >= 1920 && size.Height >= 1080) resourceName = "ms-appx:///MainPage.layout-1920x1080.xaml"; 
     else if (size.Width >= 1680 && size.Height >= 1050) resourceName = "ms-appx:///MainPage.layout-1680x1050.xaml"; 
     else if (size.Width >= 1600 && size.Height >= 1200) resourceName = "ms-appx:///MainPage.layout-1600x1200.xaml"; 
     else if (size.Width >= 1360 && size.Height >= 768) resourceName = "ms-appx:///MainPage.layout-1360x768.xaml"; 
     else if (size.Width >= 1280 && size.Height >= 1024) resourceName = "ms-appx:///MainPage.layout-1280x1024.xaml"; 
     else if (size.Width >= 1024 && size.Height >= 768) resourceName = "ms-appx:///MainPage.layout-1024x768.xaml"; 
     else if (size.Width >= 720 && size.Height >= 480) resourceName = "ms-appx:///MainPage.layout-720x480.xaml"; 
     else resourceName = "ms-appx:///MainPage.layout-640x480.xaml"; 

     this.InitializeComponent(new Uri(resourceName, UriKind.Absolute)); 

    } 



} 

}

这些都是代码的链接,您可以将其用于您的场景
https://developer.microsoft.com/en-us/windows/iot/samples/digitalsign

https://github.com/ms-iot/samples/tree/develop/DigitalSign

相关问题