none
win8.1如何动态创建一个page?类似于手动新建一个A.xaml页面一样 RRS feed

  • 问题

  • 具体是这样,用类去实现一个page,我的思路是这样,写一个A类继承Page,然后再C类里面初始化 A a=new A();

     Frame.Navigate(typeof(A));//执行到这的时候报错,求解?是不是不可以这样动态创建?

    2014年6月9日 6:53

答案

全部回复

  • 补充一下,简单说就是用编码的方式实现一个在项目文件属性上右击“添加新项” 选择空白页的结果,那个空白页就是我想要的 但只能通过编码方式实现,然后我可以继续在生成的page里面动态添加控件
    2014年6月9日 7:17
  • 你好,lxdhuainan,

    一般不建议这么做,同时我也不确定你是否可以这么做,不过我建议你可以把你想要生成的内容放在一个UserControl中,然后Page中加载对应的UserControl即可。

    或者你具体想实现的功能是什么可以告诉我们吗?以便可以帮你想一个比较靠谱的办法:)

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年6月10日 1:38
    版主
  • 现在具体我不知道页面长什么样子,需要根据服务器返回的属性来定义页面的各种布局效果,所以我想通过建立几个类来实现,几个类分别继承Page,每一个类实现一部分布局,比如A类实现画矩形,B类实现在矩形里画边框或其他,然后通过A.Child.Add(B)这样把几个类(其实就是Page了)叠加在一起最终呈现一个动态生成的UI,不知道我这样说是不是很清楚?

    现在我的做法是建立了几个空的.xaml页面,但是能不能不用建.xaml页面,而且完全通过.cs去实现一个标准Page?不知道大家又没有什么好的办法?在线等

    2014年6月10日 2:44
  • 我可以理解你的意思,你可以通过一些代码生成一个动态的UI,不过我的问题也在于此,你必须要把这些UI以page的方式(或者单独的XAML)显示出来吗?因为如果是UserControl的话,只需要一个xaml页面用来承载即可。

    举个例子来说,你有N个布局效果,通过UserControl的方式来预定义,在页面中只需要导入UserControl即可实现你需要的效果。

    同时我好像没有看到过单独通过cs代码生成页面的例子。

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年6月10日 7:03
    版主
  • 不确定你的方法是不是可行,那用户控件可以嵌套用户控件吗,如何嵌套?如果我建了一个用户控件,其他用户控件可以继承该用户控件嘛?
    2014年6月10日 7:41
  • 你指的继承用户控件是什么意思?我不是很明白,不过可以嵌套用户控件,比如说我有一个UserControl1和UserControl2,

    比如说你可以通过如下的方式来把UserControl2的内容放置到UserControl1中:

    <UserControl
        x:Class="App96.MyUserControl1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App96"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300"
        d:DesignWidth="400">
        
        <Grid>
            <local:MyUserControl2></local:MyUserControl2>
        </Grid>
    </UserControl>

    h或者通过CS的方式:

        public sealed partial class MyUserControl1 : UserControl
        {
            public MyUserControl1()
            {
                this.InitializeComponent();
                MyUserControl2 userControl2 = new MyUserControl2();        
            }
        }

    --James

    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年6月10日 7:50
    版主
  • 我感觉用户控件主要是针对于重用功能的,这种解决方案感觉不妥,个人愚见!
    2014年6月10日 8:43
  • 如果建立一个类文件继承至Canvas 而不是Page,然后其他地方引用这个类文件 这样行不行
    2014年6月10日 9:03
  • ?????

    2014年6月10日 13:36