【问题标题】:Windows App can't use Grid MemberFunctionWindows 应用程序无法使用 Grid MemberFunction
【发布时间】:2014-10-05 11:19:07
【问题描述】:

在我的网格中附加文本块时遇到一些问题。 我不能使用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;
        }

【问题讨论】:

    标签: c# windows xaml store


    【解决方案1】:

    SetRow(FrameworkElement framework,int value) 是一个静态方法。实例成员不能使用它。 像这样使用:-

          Grid.SetRow(header,1); 
    

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

    所以你必须添加这两个语句:-

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

    此外,这里是新网格,即页面,它还必须分配一个父网格。当您创建一个新的 AppPage (BlankPage.xaml) 时,默认情况下系统已经呈现了一个父网格。将此父网格命名为 x:Name="Layout",然后将网格“页面”添加到此“布局”网格。我在这里给出了完整的代码,包括 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));
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多