How to ExpireSolution with in a Class? - C#

So, I been creating some WPF windows using the C# Script Component, I’m been using a class, but I can’t figure out how to expire the component solution, with in one of events of the class,
I have seen the @DavidRutten TrackBar Example:https://discourse.mcneel.com/t/windows-forms-track-bar-live-update-to-grasshopper-component/36731/2?u=antonio_cersosimo

public MyWindow wdw;
public static string fileName;
public class MyWindow : Window{
    public MyWindow()
    {
      LoadXAMLMethod();
      this.WindowStyle = System.Windows.WindowStyle.None;
      this.AllowsTransparency = true;
      this.Topmost = true;
    }
    System.Windows.Controls.Button ButtoninXAML;
    public void LoadXAMLMethod()
    {
      StreamReader mysr = new StreamReader(fileName);
      DependencyObject rootObject = System.Windows.Markup.XamlReader.Load(mysr.BaseStream) as DependencyObject;
      ButtoninXAML = LogicalTreeHelper.FindLogicalNode(rootObject, "button1") as System.Windows.Controls.Button;
      ButtoninXAML.Click += new RoutedEventHandler(Button_Click);
      this.Content = rootObject;
    }
    public void Button_Click(object sender, RoutedEventArgs e)
    {
      System.Windows.MessageBox.Show("Hi WPF");
      Component.ExpireSolution(true);///////////////////////////
    }
}

So, I need to be able to expire the solution with in the Button_Click Event, to update the output of the component,So Any ideas?? Thanks

Hello,
I don’t know what I’m doing is the best way and my experience is limited to precompiled Visual Studio projects only, so I don’t know this works with C# scripting component but this is what I do for exactly your situation. I saw somewhere somebody is telling Grasshopper is Windows.Forms so probably there might be issues with having WPF windows coexisted, but no problem have been observed in my environment.

I prefer this because you don’t need to have a reference to the Component class in the MyWindow class.

Component main

var wnd = new MyWindow();
wnd.SetUpdateFunction(()=>{This.Component.ExpireSolution(true)});

MyWindow Class

private Action _update();
public void SetUpdateFunction(Action F){
    _update=F;
}
public void Button_Click(object sender, RoutedEventArgs e)
{
    if(_update==null)
    {
    }else _update();
}
1 Like

Hi, thanks @mikity_kogekoge, that solution work!!!. There are some syntax corrections.

//RunScript
wdw = new MyWindow();
wdw.SetUpdateFunction(() => {this.Component.ExpireSolution(true);}); // ; expected

//Class
private Action _update; // this is not a method
public void SetUpdateFunction(Action F)
{
  _update = F;
}
public void Button_Click(object sender, RoutedEventArgs e)
{
  if(_update == null){return;}
  else
  {
    _Msg = "loool";
    _update();
  }
}

Oh, thanks for correcting…