Skip to main content
Home Forums Silverlight Programming Programming with .NET - General XAML and multiple classes
7 replies. Latest Post by PierreOttawa on November 25, 2008.
(0)
PierreOt...
Member
1 points
10 Posts
11-22-2008 11:35 AM |
Hello All,
I built a recent application for Silverlight 2.0 and everything works great. For the moment, all I have is one XAML file and a big fat code behind file.But, for multiple reasons, now I need to extract some methods and create classes with them. Everything is going smoothly...until I need some buttons or other elements from the XAML with a click event (I only have one XAML file) to access the eventhandler which is in another class. How can we make the XAML file access them? xmnls namespace?
Bear with me...I'm relatively new to web development.Thank you.
If you have any smaples or examples, that would help me a lot understand everything. I'm using VB.Pierre
amyo
Contributor
3606 points
493 Posts
11-22-2008 12:50 PM |
Either you can call the method thought instance of that class.
private void LayoutRoot_MouseEnter(object sender, MouseEventArgs e) { var myClass= new MyClass(); myClass.LayoutRoot_MouseEnter(sender, e); }
Or you can create new partial class of your XAML.cs class.
Create a partial class that holds your events:
public partial class Page { private void LayoutRoot_MouseEnter(object sender, MouseEventArgs e) { MessageBox.Show("Mouse"); } }
11-22-2008 7:21 PM |
Thank you. This works perfectly.
Now, I have the opposite question also. How can multiple classes target elements from a single XAML?
Let's say I have a XAML with a button, which, with your explanation call a method inside another class. This is working. Then, inside this method, I want to target a XAML element (let's say a TextBlock) and change the content. How can this be done?
Thank you for your help.
Pierre
11-22-2008 10:36 PM |
Suppose you other class is MyClass that wants to access elements of Page.XAML
You can pass the ref of Page to that constructor.
Sample:
public class MyClass { private Page _motherRef; public MyClass(Page ref) { _motherRef=Ref; //Now you can access all elements of Page through _motherRef } }
Hope this will help you.
11-22-2008 11:17 PM |
Thank You.
You saved me tons of hours.Pierre
11-24-2008 4:06 PM |
Hello Amyo,
bufferCalcul.display(sender, e)
xamlpage.BufferingArea.Visibility = Windows.Visibility.Visible
Canvas.SetLeft(xamlpage.BufferingArea, Application.Current.Host.Content.ActualWidth / 2 - 75)
Canvas.SetTop(xamlpage.BufferingArea, Application.Current.Host.Content.ActualHeight / 2 - 100)
xamlpage.ConnectingText.Text =
xamlpage.progressload.Visibility = Windows.Visibility.Visible
xamlpage.progressload.Value = Math.Round(xamlpage.vid.BufferingProgress * 100)
xamlpage.BufferingArea.Visibility = Windows.Visibility.Collapsed
xamlpage.progressload.Visibility = Windows.Visibility.Collapsed
xamlpage.DataloadTXT.Visibility = Windows.Visibility.Collapsed
End
11-24-2008 10:38 PM |
You need to initiate xamlpage(Private xamlpage As Page)
You can make this as public and then
Private Sub showbuffer(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Dim bufferCalcul As New Buffering
bufferCalcul.xamlpage=Me
bufferCalcul.displaybuffer(sender, e)
End Sub
11-25-2008 10:05 AM |
Done and working.
Thank you for your precious help.