问题描述
我用谷歌搜索过,但没有找到任何有用的资源,所以我决定问一下.
I've googled, but did not get any useful resources, so i decided to ask.
问题:
我有一个 Windows Phone 8 C#/XAML .NET 4.5 应用程序,它将有几个页面(15 - 50)所有将具有相似的外观 + 相同的数据上下文设置为 ViewModel 的一个实例:
I have a Windows Phone 8 C#/XAML .NET 4.5 Application, that is going to have several pages (15 - 50) that are all going to have similar look + same datacontext set to one instance of ViewModel:
--------------------------
|logo usermenu(v)|
--------------------------
| |
| |
| |
| ..variable.. |
| ..content... |
| |
| |
--------------------------
问题:
我在这件事上找不到任何可用的东西,有人能解释一下怎么做吗?
I can't find anything usable in this matter, could someone explain how to do it?
(我是新手 - 意思是我很感激任何有用的信息,但更多的是对傻瓜的解释)
如何创建一个基页/祖先来派生我的页面?
How to create a base-page/ancestor to derive my pages from?
有没有办法在祖先中设置数据上下文?
Is there a way to set a datacontext in ancestor?
如何使用那个基页/祖先?
How to use that base-page/ancestor?
PS:如果您想知道为什么我想要具有相同数据上下文的页面,我问的这个问题中有更多关于它的文章之前
P.S.: In case you're wondering why I want to have pages with same datacontext, there is more written about it in This question I asked before
推荐答案
听起来您可能在这里采取了错误的方法.
It sounds like your probably taking the wrong approach here.
与其拥有 15-50 个具有相同数据上下文的相同页面,不如拥有一个页面并改变 DataContext.这将比拥有大量来自同一个基础的页面简单得多.
当然,这取决于您的实际内容的可变性.
Rather than have 15-50 identical pages with the same data context, have one page and vary the DataContext. This will be much simpler than having lots of pages which all descend from the same base.
This is, of course, dependent upon how variable your actual content is.
就您的具体问题而言:
页面与其他任何类一样,因此继承的定义方式相同.只需确保在
cs
和xaml
文件中指定祖先.
Pages are classes like any other and so inheritance is defined in the same way. Just make sure you specify the ancestor in the
cs
andxaml
file.
您不能将祖先中的数据上下文设置为与实际实例不同,如果您只是在祖先中设置它,则实例将无法使用它.您需要在实例中设置 DataContext.
You can't set the datacontext in the ancestor to be different to the actual instance and if you just set it in the ancestor it won't be available to the instance. You need to set the DataContext in the instance.
像这样:
一个非视觉(稍后会详细介绍)基本页面
A non-visual (more on this in a moment) base page
namespace SO19398590
{
using Microsoft.Phone.Controls;
public class MyBasePage : PhoneApplicationPage
{
}
}
继承自此的实际页面.
客服:
An actual page that inherits from this.
cs:
public partial class MainPage : MyBasePage
{
public MainPage()
{
InitializeComponent();
}
}
xaml(部分):
<so19398590:MyBasePage
x:Class="SO19398590.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:so19398590="clr-namespace:SO19398590"
SupportedOrientations="Portrait">
<Grid x:Name="LayoutRoot" Background="Transparent">
<!-- put stuff here -->
</Grid>
</so19398590:MyBasePage>
请注意,这是一个没有视觉效果的基本页面,我知道您要求从基类继承视觉效果.
不幸的是,Windows Phone 在视觉页面继承方面有一个非常糟糕的故事.工具(设计师)不喜欢它,很容易陷入难以诊断的问题.
Note that this is a base page with no visuals and I am aware that you asked for visual inheritance from the base class.
Unfortunately there is a very poor story with Windows Phone when it comes to visual page inheritance. The tooling (designers) doesn't like it and it's very easy to get into problems that are awkward to diagnose.
更好的方法是使用一个页面的[多个实例],但根据要显示的数据加载不同的用户控件.
一个稍微复杂一点但仍然允许标准页面导航体验的替代方法是使用自定义 PhoneApplicationFrame
并在其中包含常见的 UI 元素.
A better approach would be to use [multiple instances of] one page but load different user controls depending on the data you want to display.
A slightly more complicated alternative but which still allows for a standard page navigation experience is to use a custom PhoneApplicationFrame
and include the common UI elements there.
抱歉,这是一个相当笼统的答案,但最佳"解决方案将取决于您定义为可变内容"的空间中的实际情况.
Sorry this is quite a generic answer but the "best" solution will depend on what is actually going in the space you define as "variable content".
这篇关于WP8 如何创建基页 &用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!