Skip to main content

Microsoft Silverlight

Answered Question XAML and multiple classesRSS Feed

(0)

PierreOttawa
PierreOt...

Member

Member

1 points

10 Posts

XAML and multiple classes

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
amyo

Contributor

Contributor

3606 points

493 Posts

Re: XAML and multiple classes

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");
        }
    }
 

Amyo Kabir
Solution Architect & Sr. Developer
Blog

PierreOttawa
PierreOt...

Member

Member

1 points

10 Posts

Re: Re: XAML and multiple classes

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

amyo
amyo

Contributor

Contributor

3606 points

493 Posts

Re: Re: XAML and multiple classes

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.

Amyo Kabir
Solution Architect & Sr. Developer
Blog

PierreOttawa
PierreOt...

Member

Member

1 points

10 Posts

Re: Re: XAML and multiple classes

Thank You.

You saved me tons of hours.

Pierre

PierreOttawa
PierreOt...

Member

Member

1 points

10 Posts

Answered Question

Re: Re: Re: XAML and multiple classes

Hello Amyo,

I've tried everyhting you said.  Looks like no error is generated...BUT...nothing seems to happen.

By the way, I'm using VB.  Here is a sample of my code.  If you could spot anything wrong, it would be very appreciated.  The XAML file contains a buffering Canvas element called BufferingArea.  There's also a media element called vid. 

Here's the portion of my code behind for Page.xaml.vb which fires when vid is buffering:

Private Sub showbuffer(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

Dim bufferCalcul As New Buffering

bufferCalcul.display(sender, e)

End Sub


And here's the custom class Buffering containing the displaybuffer method :

Public Class Buffering

Inherits UserControl

Private xamlpage As Page

Public Sub displaybuffer(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

'Buffering Verification

If xamlpage.vid.BufferingProgress < 0.99 Then

If xamlpage.splash.Visibility = Windows.Visibility.Collapsed Then

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 = "buffering " + (Math.Round((xamlpage.vid.BufferingProgress * 100), 3).ToString) + "%"

Else

xamlpage.progressload.Visibility = Windows.Visibility.Visible

xamlpage.progressload.Value = Math.Round(xamlpage.vid.BufferingProgress * 100)

End If

Else

xamlpage.BufferingArea.Visibility = Windows.Visibility.Collapsed

xamlpage.progressload.Visibility = Windows.Visibility.Collapsed

xamlpage.DataloadTXT.Visibility = Windows.Visibility.Collapsed

End If

End Sub

End Class

amyo
amyo

Contributor

Contributor

3606 points

493 Posts

Answered Question

Re: Re: Re: XAML and multiple classes

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

 

Amyo Kabir
Solution Architect & Sr. Developer
Blog

PierreOttawa
PierreOt...

Member

Member

1 points

10 Posts

Re: Re: Re: Re: XAML and multiple classes

Done and working.

Thank you for your precious help.

Pierre

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities