Windows App无法使用网格成员功能

时间:2014-08-12 11:44:58

标签: c# windows xaml store

我在网格中附加Textblock有一些问题。 我不能使用SetRow(Frameworkelement,index);

ErrorMessage类似于我无法使用实例引用访问MemberFunction。 相反,我应该使用TypeName,但是如何?

 private FrameworkElement CreateGrid(int i)
        {
            double w = 775;
            double l = 1105;

            TextBlock header = CreateHeader("someRndStuffHeader");
            RowDefinition headerRowDefinition = new RowDefinition
            {
                MinHeight = header.ActualHeight,
                MaxHeight = header.ActualHeight,  
            };

            TextBlock footer = CreateFooter("someRndStuffFooter");
            RowDefinition footerRowDefinition = new RowDefinition
            {
                MinHeight = footer.ActualHeight,
                MaxHeight = footer.ActualHeight

            };

            double contentHeight = l- header.ActualHeight - footer.ActualHeight;
            RowDefinition contentRowDefinition = new RowDefinition
            {
                MinHeight = contentHeight,
                MaxHeight = contentHeight,
            };

            ColumnDefinition gridColumnDefinition = new ColumnDefinition()
            {
                MaxWidth = w,
                MinWidth = w,
            };

            Grid page = new Grid();
            string name = "printPage" + i.ToString();
            page.Name = name;

            page.RowDefinitions.Add(headerRowDefinition);
            page.RowDefinitions.Add(contentRowDefinition);
            page.RowDefinitions.Add(footerRowDefinition);
            page.ColumnDefinitions.Add(gridColumnDefinition);

            // I CANT USE THIS 
            page.SetRow(header, 1);

            return page;
        }

1 个答案:

答案 0 :(得分:0)

SetRow(FrameworkElement框架,int值)是一个静态方法。实例成员无法使用它。 使用方式如下: -

      Grid.SetRow(header,1); 

但是,在实现这一点之前,必须在新形成的网格中创建页眉和页脚TextBlocks子项,因为SetRow方法仅在框架元素是任何网格的子项时才设置框架元素的行。

所以你必须添加这两个陈述: -

  page.Children.Add(header);
  page.Children.Add(footer);

此外,这里是新的网格,即页面,它也必须被分配一个父网格。当您创建新的AppPage(BlankPage.xaml)时,默认情况下系统已呈现父网格。将此父网格命名为x:Name =“Layout”,然后将网格“page”添加到此“布局”网格中。我在这里给出完整的代码,包括xaml和.cs。我创建了一个按钮,然后当我按下按钮时,会创建新的网格

在XAMl中: -

<Grid x:Name="layout" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Button Content="Button" HorizontalAlignment="Left" Margin="587,475,0,0" VerticalAlignment="Top" Click="Button_Click"/>

</Grid>

在.cs: -

    private FrameworkElement CreateGrid(int i)
    {
        double w = 775;
        double l = 1105;

        TextBlock header = CreateHeader("someRndStuffHeader");
        RowDefinition headerRowDefinition = new RowDefinition
        {
            MinHeight = header.ActualHeight,
            MaxHeight = header.ActualHeight,
        };

        TextBlock footer = CreateFooter("someRndStuffFooter");
        RowDefinition footerRowDefinition = new RowDefinition
        {
            MinHeight = footer.ActualHeight,
            MaxHeight = footer.ActualHeight

        };

        double contentHeight = l - header.ActualHeight - footer.ActualHeight;
        RowDefinition contentRowDefinition = new RowDefinition
        {
            MinHeight = contentHeight,
            MaxHeight = contentHeight,
        };

        ColumnDefinition gridColumnDefinition = new ColumnDefinition()
        {
            MaxWidth = w,
            MinWidth = w,
        };

        Grid page = new Grid();
        string name = "printPage" + i.ToString();
        page.Name = name;

        page.RowDefinitions.Add(headerRowDefinition);
        page.RowDefinitions.Add(contentRowDefinition);
        page.RowDefinitions.Add(footerRowDefinition);
        page.ColumnDefinitions.Add(gridColumnDefinition);


        **Grid.SetRow(header, 1);
        page.Children.Add(header);
        page.Children.Add(footer);**

        return page;
    }

    private TextBlock CreateFooter(string p)
    {
        return new TextBlock() { Width=300,Height=300,Text=p};
    }

    private TextBlock CreateHeader(string p)
    {
        return new TextBlock() { Width = 300, Height = 300, Text = p };
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        layout.Children.Add(CreateGrid(1));
    }