Maxscript interacts with WPF
I used WPF to write an interface that implements the function of buttons to switch between different pages, and usercontrol for subpages. When calling this wpf-written interface library in MaxScript, the events are written in MaxScript. Events are useful when the widgets on my main page are bound in MaxScript. But the controls I defined in the subpage have no effect. But when I define Form1.MainContent.Content = Form2 -- set the subpage as the main content
The subpage's controls are also reactive when I first start, when I switch pages. Is there a response to the button event of the subpage, what is the reason, and how to solve it
This is the code for the main page
<Window x:Class="页面切换测试.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:页面切换测试" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Title="MainWindow" Width="250" Height="60" MinHeight="60" MaxHeight="1500" ResizeMode="NoResize" SizeChanged="MainWindow_SizeChanged" SizeToContent="Height" mc:Ignorable="d"> <!-- SizeToContent="Height" --> <Grid> <!-- 使用 Grid 布局,定义两行:第一行用于按钮,第二行用于内容 --> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <!-- 第一行高度自动调整 --> <RowDefinition Height="*" /> <!-- 第二行占据剩余空间 --> </Grid.RowDefinitions> <!-- Buttons to switch between UserControls --> <StackPanel Margin="10" HorizontalAlignment="Center" VerticalAlignment="Top" Orientation="Horizontal"> <Button Width="100" Click="Button_Click" Content="Page 1" /> <Button Width="100" Click="Button_Click" Content="Page 2" /> <Button x:Name="btn1" Width="auto" x:FieldModifier="public" Content="测试" /> <!-- Add more buttons for additional pages as needed --> </StackPanel> <!-- 内容区域 --> <ContentControl x:Name="MainContent" Grid.Row="1" MinHeight="{Binding ElementName=MainWindow, Path=MinHeight}" MaxHeight="{Binding ElementName=MainWindow, Path=MaxHeight}" Margin="10" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:FieldModifier="public" /> </Grid> </Window>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace 页面切换测试 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { private UserControl currentPage; private Dictionary<string, UserControl> pageCache = new Dictionary<string, UserControl>(); public MainWindow() { InitializeComponent(); SwitchToPage("Page 1"); } public void Button_Click(object sender, RoutedEventArgs e) { Button btn = (Button)sender; switch (btn.Content.ToString()) { case "Page 1": SwitchToPage("Page 1"); break; case "Page 2": SwitchToPage("Page 2"); break; // Add cases for additional pages as needed } } public void SwitchToPage(string pageName) { if (!pageCache.ContainsKey(pageName)) { switch (pageName) { case "Page 1": pageCache[pageName] = new Page1(); break; case "Page 2": pageCache[pageName] = new Page2(); break; // Add cases for additional pages as needed } } // 强制重新计算布局 // MainContent.InvalidateArrange(); // MainContent.InvalidateMeasure(); // 订阅 SizeChanged 事件,确保在窗口大小变化时调整高度 // this.SizeChanged += MainWindow_SizeChanged; // 调整窗口高度 AdjustWindowHeight(); currentPage = pageCache[pageName]; MainContent.Content = currentPage; //AttachEventHandlers(currentPage); } //private void AttachEventHandlers(UserControl page) //{ // // 解除之前的事件绑定 // var oldButton = currentPage?.FindName("siyou") as Button; // if (oldButton != null) // { // oldButton.Click -= HandleButtonClick; // } // // 绑定新的事件处理程序 // var newButton = page.FindName("siyou") as Button; // if (newButton != null) // { // newButton.Click += HandleButtonClick; // } //} //private void MainContent_LayoutUpdated(object sender, EventArgs e) //{ // // 取消订阅 LayoutUpdated 事件,避免多次触发 // MainContent.LayoutUpdated -= MainContent_LayoutUpdated; // // 调整窗口高度 // AdjustWindowHeight(); //} private void AdjustWindowHeight() { double minHeight = this.MinHeight; double maxHeight = this.MaxHeight; // 使用 Dispatcher 延迟调整窗口高度,确保 ActualHeight 已经计算完成 this.Dispatcher.BeginInvoke(new Action(() => { double contentHeight = MainContent.ActualHeight; // System.Diagnostics.Debug.WriteLine($"ContentHeight: {contentHeight}"); if (contentHeight > 0 && contentHeight < maxHeight) { this.Height = Math.Max(contentHeight + 50, minHeight); // 确保高度不低于 MinHeight } else if (contentHeight >= maxHeight) { // 如果内容高度超过最大高度,保持窗口高度为最大值 this.Height = maxHeight; } else { // 如果内容高度小于最小高度,保持窗口高度为最小值 this.Height = minHeight; } }), System.Windows.Threading.DispatcherPriority.Render); } private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e) { // this.SizeToContent = SizeToContent.Manual; // this.SizeToContent = SizeToContent.Manual; AdjustWindowHeight(); } } }
这是子页面的界面和事件代码
<UserControl x:Class="页面切换测试.Page1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:页面切换测试" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="250" d:DesignWidth="250" mc:Ignorable="d"> <Grid> <Button x:Name="siyou" HorizontalAlignment="Center" VerticalAlignment="Center" x:FieldModifier="public" Content="这是墙体页面" FontSize="24" /> </Grid> </UserControl>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; //using ManagedServices; namespace 页面切换测试 { /// <summary> /// Page1.xaml 的交互逻辑 /// </summary> public partial class Page1 : UserControl { public Page1() { InitializeComponent(); } //private void siyou_Click(object sender, RoutedEventArgs e) //{ // ManagedServices.MaxscriptSDK.ExecuteMaxscriptCommand("print \"Hello from MaxScript!\"") //} } }
这里是maxscript调用wpf程序的代码
fn main = ( -- 加载程序集 dotnet.loadAssembly (getFilenamePath (getSourceFileName()) + @"页面切换测试.dll") global Form1 = dotnetObject "页面切换测试.MainWindow" global Form2 = dotnetObject "页面切换测试.Page1" -- 子页面对象 -- 切换到子页面 -- local mainContent = Form1.FindName "MainContent" Form1.MainContent.Content = Form2 -- 设置子页面为主内容 dataContext = Form1.DataContext Form1.Show() (dotnetobject "System.Windows.Interop.WindowInteropHelper" Form1).Owner = dotnetobject "IntPtr" (windows.GetMAXHWND()) ) fn handleButtonClick sender args = ( -- 处理子页面按钮点击事件 messageBox "子页面按钮被点击!" ) fn handleButtonClick1 sender args = ( -- 处理主页面按钮点击事件 messageBox "主页面按钮被点击!" ) -- fn startFocus arg = -- ( -- enableAccelerators = false -- ) -- -- fn stopFocus arg = -- ( -- enableAccelerators = true -- ) fn setupEventHandlers = ( -- 主页面按钮事件绑定 -- local button1 = Form1.FindName "btn1" -- if button1 != undefined do -- ( dotNet.addEventHandler Form1.btn1 "Click" handleButtonClick1 -- ) -- else -- ( -- print "Main button not found!" -- ) -- 子页面按钮事件绑定 -- local button = Form2.FindName "siyou" dotNet.addEventHandler Form2.siyou "Click" handleButtonClick dotNet.addEventHandler Form1 "GotFocus" startFocus dotNet.addEventHandler Form1 "LostFocus" stopFocus -- else -- ( -- print "Child button not found!" -- ) -- 设置生命周期控制 -- dotNet.setLifetimeControl button #dotNet dotNet.setLifetimeControl Form2.siyou #dotNet dotNet.setLifetimeControl Form1 #dotNet dotNet.setLifetimeControl Form2 #dotNet ) main() setupEventHandlers()

Comments
不要用ms写控件本身的功能,所有控件的实现由c#实现,与m
不要用ms写控件本身的功能,所有控件的实现由c#实现,与max交互由公共参数和公开方法实现,不会sdk时通过预先传递参数供控件使用,比如max主题颜色传给公共参数,控件实例化时读取公共参数,会sdk后通过sdk获取即可
Automatic Efficient Intelligent
We change the work state