【发布时间】:2017-03-19 12:52:25
【问题描述】:
我有一个场景,在女巫我需要打开新的 WPF 窗口表单 Web 表单页面,并在该窗口中绑定十个属性并将其显示在更新面板中,因此网页上的数据会随着 WPF 中的用户输入而变化窗口。
我试过这样的:
public partial class WorkerPanel : System.Web.UI.Page
{
private MainWindow _mainWindow;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
_mainWindow = new MainWindow();
_mainWindow.Show();
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
rptTransactions.DataSource = _mainWindow.Distributors;
rptTransactions.DataBind();
}
}
但它给了我以下错误:
调用线程必须是 STA,因为很多 UI 组件都需要这个。
根据这个问题https://stackoverflow.com/questions/2329978我将我的代码更改为:
Thread t = new Thread(() =>
{
_mainWindow = new MainWindow();
_mainWindow.Show();
System.Windows.Threading.Dispatcher.Run();
});
t.SetApartmentState(ApartmentState.STA);
t.IsBackground = true;
t.Start();
有了这个,我的网页和 WPF 窗口加载正常,但我无法绑定到这个窗口属性,因为它在新线程中运行。这种绑定是可能的还是我应该采取不同的方法?
【问题讨论】:
-
您知道这只会发生在网络服务器上吗?通过网络连接到您的页面的任何人都不会看到 WPF 窗口。
-
要回答您的问题,请采用不同的方法。创建客户端(例如 WPF)可以发布到的 Web 服务。如果您想将其保留在网络表单中,可以使用简单的 HttpHander (.asmx) 来实现。
-
我知道这只会发生在服务器上,它有一个目的。感谢您的建议,然后将尝试使用网络服务。
标签: c# asp.net .net wpf webforms