1

I am trying to set a minimum width and height for my silverlight 4 OOB application without any success so far. Can someone help me as i keep getting this error messages:

"An object reference is required for the non-static field,method, or property 'kat.MainPage.Width.get' and 'kat.MainPage.Height.get'

My code is the following:

namespace kat
{
  public partial class MainPage : UserControl
  {
    public MainPage()
    {
      InitializeComponent();
      this.SizeChanged +=new System.Windows.SizeChangedEventHandler(LayoutRoot_SizeChanged);
    }

    public double Width { get; set; }
    public double Height { get; set; }

    private void LayoutRoot_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
    {
      if (kat.MainPage.Width <500)
        kat.MainPage.Width =500;
      if (kat.MainPage.Height <500)
        kat.MainPage.Height =500;
    }
  }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • This looks like a duplicate of http://stackoverflow.com/questions/7215421/how-do-i-set-a-minimum-width-and-height-for-my-silverlight-4-oob-application. Notice how he uses "Application.MainWindow." – AlignedDev Dec 20 '11 at 14:40
  • Question is a duplicate, but their problem is more syntax than anything :) – iCollect.it Ltd Dec 20 '11 at 14:42

1 Answers1

1

I assume kat is just your namespace...

You are basically trying to access members of an object without actually using a pointer to the object. kat.MainPage is a class, not an object so any references to kat.MainPage.anything will fail with that error.

You just wanted:

private void LayoutRoot_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
{
    if (Application.MainWindow.Width < 500)
        Application.MainWindow.Width = 500;
    if (Application.MainWindow.Height < 500)
        Application.MainWindow.Height = 500;
}
iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202