Reputation: 1529
I am working on silverlight and i am beginner. I know the question is very basic but as I am beginner I need help in achieving this. Yet I have written xaml code to create GUI but now I have to create GUI using c# and my problem is :
I know how to create a GUI but i dont know how to render it.
Like i know how to create a slider. I done it like this:
void createGrid()
{
Grid childGrid = new Grid();
ColumnDefinition colDef1 = new ColumnDefinition();
ColumnDefinition colDef2 = new ColumnDefinition();
ColumnDefinition colDef3 = new ColumnDefinition();
RowDefinition rowdef = new RowDefinition();
childGrid.ColumnDefinitions.Add(colDef1);
childGrid.RowDefinitions.Add(rowdef);
Slider sl = new Slider();
Grid.SetColumn(sl, 1);
Grid.SetRow(sl, 0);
childGrid.Children.Add(sl);
}
But the problem is how to render it. I mean i just created project named "NoMoreLife
" and after i created this function CreateGrid()
and called it from constructor like this:
public MainPage()
{
InitializeComponent();
createGrid();
}
My MainPage class is: (which contains the code)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace NoMoreLife
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
createGrid();
}
void createGrid()
{
Grid childGrid = new Grid();
ColumnDefinition colDef1 = new ColumnDefinition();
ColumnDefinition colDef2 = new ColumnDefinition();
ColumnDefinition colDef3 = new ColumnDefinition();
RowDefinition rowdef = new RowDefinition();
childGrid.ColumnDefinitions.Add(colDef1);
childGrid.RowDefinitions.Add(rowdef);
Slider sl = new Slider();
Grid.SetColumn(sl, 1);
Grid.SetRow(sl, 0);
childGrid.Children.Add(sl);
}
}
}
and MainPage.xaml is:
<UserControl x:Class="NoMoreLife.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
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 x:Name="LayoutRoot" Background="White">
</Grid>
</UserControl>
How should I render this slider create which i have created using c#? Because currently when I run the program I see nothing (no slider).
EDIT: After Dan's comment:
I have added check.cs class in my project now this class contains the code:
namespace NoMoreLife
{
public static class check
{
public static void createGrid()
{
Grid childGrid = new Grid();
ColumnDefinition colDef1 = new ColumnDefinition();
ColumnDefinition colDef2 = new ColumnDefinition();
ColumnDefinition colDef3 = new ColumnDefinition();
RowDefinition rowdef = new RowDefinition();
childGrid.ColumnDefinitions.Add(colDef1);
childGrid.ColumnDefinitions.Add(colDef2);
childGrid.ColumnDefinitions.Add(colDef3);
childGrid.RowDefinitions.Add(rowdef);
Slider sl = new Slider();
Grid.SetColumn(sl, 1);
Grid.SetRow(sl, 1);
childGrid.Children.Add(sl);
// LayoutRoot.Children.Add(childGrid);
this.Content = childGrid;
}
}
}
And my call to the function is:
namespace NoMoreLife
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
check.createGrid();
}
}
}
here this.Content = childGrid; gives error.
Note: Please note that I have to code in c# only not in xaml.
Upvotes: 1
Views: 155
Reputation: 34198
You are creating a Grid
in code, but you're not doing anything with it. Typically, you might want to include that grid as the content of your form. Try adding this line at the bottom of your createGrid
method:
this.Content = childGrid;
EDIT (in response to the updated code in the question):
Since your code is in a separate class to the window, you probably want to do this instead:
Change your method createGrid
from a void
to instead return childGrid
Use the result of the method in your page's constructor:
public MainPage()
{
InitializeComponent();
this.Content = check.createGrid();
}
Upvotes: 3