0

I have a XAML page with WebView inside (for example MainPage.xaml). Also I have WinRT Component with class marked with [AllowForWeb] attribute. This component is referenced from project where MainPage.xaml located and in code-behind AddWebAllowedObject method is used. And I can't reference main project back because of circular dependency.

How to call MainPage.xaml.cs methods from component class? Very usual situation. Is there are some standard way to do it?

For example. I have a method inside RT component that could be called from JavaScript

     public void ShowMessage(string message)
    {
       // I want to call here function from MainPage.xaml.cs
    }
Alexej Sommer
  • 2,677
  • 1
  • 14
  • 25
  • By "How to call methods inside XAML page with WebView from component class?" you mean you want to call the methods in component class,and the methods are defined inside XAML code-behind? – Elvis Xia - MSFT Jul 04 '16 at 03:05
  • Right. I want from component class method call method inside MainPage.xaml.cs – Alexej Sommer Jul 04 '16 at 05:48

2 Answers2

1

How to call MainPage.xaml.cs methods from component class? Very usual situation. Is there are some standard way to do it?

Yes, you can pass the method from MainPage.xaml.cs to Windows Runtime Component through delegate(Currently it's very limited to use delegate in Runtime Component using C#, see this case, so I use C++ as demo).

For Runtime Component Class MyClass.h:

public delegate Platform::String^ MyFunc(int a, int b);
public ref class MyClass sealed
{
public:
    MyClass();
    static Platform::String^ MyMethod(MyFunc^ func)
    {
        Platform::String^ abc=func(4, 5);
        return abc;
    }
};

And you can use the delegate in code behind like below:

using MyComponentCpp;
private void myBtn_Click(object sender, RoutedEventArgs e)
{
   String abc=MyClass.MyMethod(MyMethod);
   myTb.Text = abc;
}
private String MyMethod(int a, int b)
{
    return (a.ToString() + b.ToString());//replace this line with your own logic.
}

And here is the complete Demo: TestProject.

Community
  • 1
  • 1
Elvis Xia - MSFT
  • 10,801
  • 1
  • 13
  • 24
  • Unfortunately this answer doesn't give option to call MyMethod only from MainPage.xaml.cs In this case it's something like callback. – Alexej Sommer Jul 04 '16 at 15:48
  • You are very close to answer. In [this question](http://stackoverflow.com/questions/14048235/circular-dependency-in-two-projects-in-c-sharp) Action is proposed. I could work well, but Action is not runtime type – Alexej Sommer Jul 04 '16 at 18:26
  • Yes I also tried with Action<> or Func<> with C# at first place. But they are not available in Runtime Component. So I used C++ and it works well. – Elvis Xia - MSFT Jul 05 '16 at 01:46
0

Thankfully to @Elvis Xia who has gived me idea, I has found a solution how to do it without C++.

I have create a third project as Class Library. It doesn't has restrictions to use Action. This library I have referenced from main project and from WinRT component. Code of class inside library:

    public class BridgeClass
{
    public static event Action<string> MessageReceived;

    public static void Broadcast(string message)
    {
        if (MessageReceived != null) MessageReceived(message);
    }
} 

Code inside main project with webview is:

  // place somewhere 
  BridgeClass.MessageReceived += ShowMessage;

  // ....... and add a method
   void ShowMessage(string msg)
    {

    }

And now i can call this code from WinRT component:

 public void ShowMessage(string message)
{
      BridgeClass.Broadcast("lalala");
}
Alexej Sommer
  • 2,677
  • 1
  • 14
  • 25