Val
Val

Reputation: 639

Silverlight MainPage object

I am playing around with testing Silverlight application. One of the tutorials on the web uses MainPage object, where MainPage is the main Silverlight class. Eg: MainPage mp = new MainPage(); It's all good, but MainPage XAML has some controls that I can access at MainPage class code, eg, I can use txtPhotoUrl.Text; to access The problem is after creating an object of class MainPage I cannot access the XAML controls - I cannot see them in the list of MainPage object properties. Is this by design or am I missing something?

Upvotes: 1

Views: 690

Answers (2)

Vladimir Dorokhov
Vladimir Dorokhov

Reputation: 3839

By default MainPage controls has internal access modifier.So, you can access MainPage controls from the same assembly. For instance,

MainPage mp = new MainPage();
string text = mp.txtPhotoUrl.Text;

You can change access modifier using x:FieldModifier="[private/internal/public]" attribute in XAML for some control. For instance,

<!-- Accessible only from Code Behind-->
<TextBlock x:Name="txtPhotoUrl" x:FieldModifier="private" />

<!-- Accessible from other assemblies-->
<TextBlock x:Name="txtPhotoUrl" x:FieldModifier="public" />

Upvotes: 2

AnthonyWJones
AnthonyWJones

Reputation: 189457

The identifier txtPhotoUrl refers to a field that has internal accessiblity. Hence to access it your code would need to be in the same project. You could use the InternalsVisibleTo attribute in the target probject so that the external code can access members marked as internal but your external code needs to be strongly named.

Upvotes: 1

Related Questions