TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Evgenie Tivaniuk
NA
82
11.7k
Accessing XAML from non-main class
Jun 19 2017 7:06 AM
Hello,
I am making a video game. Some time ago, one person recommended me to use separate classes, and not have everything in one file. I did manage to separate some of functions and variables, but unfortunatelly, only those that don't affect xaml elements.
Mostly, xaml elements that change during game: label called "TextOutput", a few grids (each grid is a room) and a large number of images and buttons.
I will give an example, there is a door that can be opened.
So, the code for clicking on door:
public
void
tmrHibernation1RightDoorOpening(
object
sender, EventArgs e)
{
tmrDoorOpening.Stop();
AreaTransition();
ShowLockerRoom();
areaLockerRoom.Visibility = Visibility.Visible;
areaLockerRoom.IsEnabled =
true
;
if
(Resolution ==
"1600x900"
)
areaLockerRoom.Margin =
new
Thickness(19, 31, 0, 0);
if
(Resolution ==
"1366x786"
)
areaLockerRoom.Margin =
new
Thickness(16, 26, 0, 0);
if
(Resolution ==
"1024x768"
)
areaLockerRoom.Margin =
new
Thickness(12, 27, 0, 0);
if
(Resolution ==
"1440x900"
)
areaLockerRoom.Margin =
new
Thickness(17, 31, 0, 0);
(Resolution ==
"1920x1080"
)
areaLockerRoom.Margin =
new
Thickness(23, 38, 0, 0);
if
(Resolution ==
"800x600"
)
areaLockerRoom.Margin =
new
Thickness(10, 20, 0, 0);
ActionsReset();
}
This works perfectly - in the main class. But if I try to move the void to another class, or even make a brand new void, that is supposed to change xaml elements, I get an error:
An
object
reference
is
required
for
the nonstatic field, method, or property
I don't really understand how to bypass this. Xaml elements aren't static, and I already read that I shouldn't change them to be static, even if it's possible.
And if I create a variable of Button\Image\Grid etc, this will allow me to change their own attributes, not of those I need.
I found out this article:
http://www.c-sharpcorner.com/UploadFile/theLizard/how-to-access-a-control-in-another-form/
It says about accessing controls from other FORMS - so I am not sure if it will work in my case, since I am using WPF, and not sure that this approach will avoid "object reference required" error.
What can be done to solve this?
Thank you in advance,
Evgenie
Reply
Answers (
4
)
windows service application can detect its own status?
Crud operation in grid view