Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在IronPython中关闭WPF窗口而不关闭.NET应用程序_C#_.net_Ironpython - Fatal编程技术网

C# 如何在IronPython中关闭WPF窗口而不关闭.NET应用程序

C# 如何在IronPython中关闭WPF窗口而不关闭.NET应用程序,c#,.net,ironpython,C#,.net,Ironpython,所以我正在用IronPython编写一个WPF应用程序。如果我通过命令“ipy.exe wpf.py”在IronPython REPL之外运行脚本,一切都会很好。但是,如果脚本是通过命令“execfile('wpf.py')”在IronPython REPL内部运行的,则第一次运行OK时,第二次出错时,“SystemError:无法在同一AppDomain中创建多个System.Windows.Application实例” 根据我的理解,这是因为每次您在REPL外部运行AppDomain时,它都

所以我正在用IronPython编写一个WPF应用程序。如果我通过命令“ipy.exe wpf.py”在IronPython REPL之外运行脚本,一切都会很好。但是,如果脚本是通过命令“execfile('wpf.py')”在IronPython REPL内部运行的,则第一次运行OK时,第二次出错时,“SystemError:无法在同一AppDomain中创建多个System.Windows.Application实例”

根据我的理解,这是因为每次您在REPL外部运行AppDomain时,它都会创建一个新的AppDomain,而在REPL内部运行AppDomain时,它将共享同一个域,并且您可以两次初始化
应用程序。问题是我必须在同一AppDomain中多次运行它,因为它不是一个独立的IronPython应用程序。我尝试过很多方法,比如在
app=Application()
之后添加add
app.ShutdownMode=ShutdownMode.OnExplicitShutdown
来更改关机模式,但这只会挂起整个REPL

有人能帮我解释一下吗?多谢各位

import clr
clr.AddReference("PresentationFramework")
clr.AddReference("PresentationCore")
clr.AddReference("System.Xml")

from System.Xml import XmlReader
from System.IO import StringReader
from System.Windows.Markup import XamlReader
from System.Windows import Application

s = XmlReader.Create(StringReader('''
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="IronPython MVVM Demo2"
    Width="450"
    SizeToContent="Height">
    <Grid Margin="15" x:Name="grid1">  
        <StackPanel Margin="5">
            <Button Margin="5">One</Button>
            <Button Margin="5">Two</Button>
            <Button Margin="5">Three</Button>
        </StackPanel>   
    </Grid>
</Window>
'''))

win = XamlReader.Load(s)

app = Application()     
app.Run(win)
print("The End.")   
导入clr
clr.AddReference(“PresentationFramework”)
clr.AddReference(“PresentationCore”)
clr.AddReference(“System.Xml”)
从System.Xml导入XmlReader
从System.IO导入StringReader
从System.Windows.Markup导入XamlReader
从System.Windows导入应用程序
s=XmlReader.Create(StringReader(“”)
一
二
三
'''))
win=XamlReader.Load(s)
app=应用程序()
应用程序运行(win)
打印(“结尾”)

我认为您需要创建一个长时间运行的STA线程来承载应用程序,并通过应用程序的Dispatcher与之通信。下面是C#中的一个例子:

使用系统;
使用System.IO;
使用系统线程;
使用System.Windows;
使用System.Xml;
名称空间控制台EAPP1
{
班级计划
{
静态void显示窗口(字符串Xaml)
{
var s=XmlReader.Create(新的StringReader(Xaml));
var win=(Window)System.Windows.Markup.XamlReader.Load;
win.ShowDialog();
}
静态void Main(字符串[]参数)
{
应用程序app=null;
var UIThread=新线程(()=>
{
app=新应用程序();
app.ShutdownMode=ShutdownMode.OnExplicitShutdown;
app.Run();
});
SetApartmentState(ApartmentState.STA);
UIThread.Start();
while(app==null)
睡眠(100);
app.Dispatcher.Invoke(()=>Console.WriteLine(“启动”));
var xaml=@”
一
二
三
";        
对于(int i=0;i<3;i++)
{
Application.Current.Dispatcher.Invoke(()=>
{
显示窗口(xaml);
});
}
Application.Current.Dispatcher.Invoke(()=>
{
Application.Current.Shutdown();
});
}
}
}

谢谢你,大卫!我将尝试将其移植到IronPython并报告。
using System;
using System.IO;
using System.Threading;
using System.Windows;
using System.Xml;


namespace ConsoleApp1
{
    class Program
    {
        static void ShowWindow(string Xaml)
        {
            var s = XmlReader.Create(new StringReader(Xaml));
            var win = (Window)System.Windows.Markup.XamlReader.Load(s);
            win.ShowDialog();
        }
        static void Main(string[] args)
        {

           Application app = null;
           var UIThread = new Thread(() =>
           {
               app = new Application();
               app.ShutdownMode = ShutdownMode.OnExplicitShutdown;
               app.Run();
           });

            UIThread.SetApartmentState(ApartmentState.STA);
            UIThread.Start();

            while (app == null )
                Thread.Sleep(100);

            app.Dispatcher.Invoke(() => Console.WriteLine("Started"));


            var xaml = @"
        <Window
            xmlns = ""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
            xmlns:x = ""http://schemas.microsoft.com/winfx/2006/xaml""
            Title = ""IronPython MVVM Demo2""
            Width = ""450""
            SizeToContent = ""Height"">
            <Grid Margin = ""15"" x:Name = ""grid1"">
                <StackPanel Margin = ""5"">
                    <Button Margin = ""5""> One </Button>
                    <Button Margin = ""5""> Two </Button>
                    <Button Margin = ""5""> Three </Button>
                </StackPanel>
            </Grid>
        </Window>";        

            for (int i = 0; i < 3; i++)
            {

                Application.Current.Dispatcher.Invoke(() =>
                {
                    ShowWindow(xaml);
                });
            }

            Application.Current.Dispatcher.Invoke(() =>
            {
                Application.Current.Shutdown();
            });

        }
    }
}