I am currently attempting to create a MIPS emulator using WPF.
In a stack panel control called registerList; I created 32 stack panels (named C0 to C31) that each contain 2 textblocks (second textblock named R0 to R31).
I initially display the 32 registers contained on Procesor.RegisterBank.Registers[i] correctly using the displayRegisters() method I created.
I made a method to update all 32 values of second textblocks with the values contained on a class called Procesor.RegisterBank.
However when attempting to use this method, the GUI page will not display when I start the project.
I've been stuck on this for hours and almost considering not creating the stack panels via code.
Thanks
public partial class MainWindow : Window
{
MRA.MRA Procesor = new MRA.MRA();
public MainWindow()
{
InitializeComponent();
displayRegisters();
Procesor.RegisterBank.registers[1] = 69;
updateRegisters();
}
public void updateRegisters()
{
for (int i = 0; i < Procesor.RegisterBank.registers.Length; i++)
{
var register = (StackPanel)registerList.FindName("C"+i.ToString());
var content = (TextBlock)register.FindName("R"+i.ToString());
content.Text= Procesor.RegisterBank.registers[i].ToString();
}
}
public void displayRegisters()
{
for (int i = 0; i < Procesor.RegisterBank.registers.Length; i++)
{
var register = new StackPanel { Orientation = Orientation.Horizontal, Background = Brushes.White, Margin = new Thickness(6, 6, 6, 6), MinHeight = 25, Name = "C" + i.ToString() };
var registerName = new TextBlock { Text = "R"+i.ToString(), FontSize = 16, MinWidth = 30, Background = Brushes.DarkGray, Foreground = Brushes.White};
var registerValue = new TextBlock { Text = Procesor.RegisterBank.registers[i].ToString(), FontSize = 16, MinWidth = 100, HorizontalAlignment = HorizontalAlignment.Right, Name="R"+i.ToString()};
register.Children.Add(registerName);
register.Children.Add(registerValue);
registerList.Children.Add(register);
}
}
}
You have to use RegisterName() , so that newly added Control
can be part of the NameScope
of parent control.
Add these lines at the end in your displayRegister()
method :
NameScope.SetNameScope(register, new NameScope());
register.RegisterName(registerValue.Name, registerValue);
registerList.RegisterName(register.Name, register);