我正在尝试制作一个基于WPF的通讯程序,我的解决方案包含app.xaml和app.xaml.cs、mainwindow.xaml和mainwindow.xaml.cs以及另外两个xaml页面,第一个用于连接,第二个用于Messenger核心{联系人,状态等}。我有一个agsxmpp库,可以帮助我进行连接。最好在哪个.cs文件中定义和初始化连接,并且如何从另一个.cs文件中访问它(以及它的事件处理程序)?顺便说一下,这个问题总是困扰着我 :(
一个默认的WPF模板将会像这样,带有一个StartupUri属性。
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
</Application.Resources>
</Application>
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_Startup">
<Application.Resources>
</Application.Resources>
</Application>
namespace WpfApplication1
{
public partial class App : Application
{
private XmppClientConnection _client = new XmppClientConnection ( );
private void Application_Startup(object sender, StartupEventArgs e)
{
var mainWindow = new Window1();
mainWindow.Show();
}
}
}
public partial class Window1 : Window
{
private XmppClientConnection _client;
public Window1()
{
InitializeComponent();
}
public Window1(XmppClientConnection client):this()
{
_client = client;
}
}
像这样:
private void Application_Startup(object sender, StartupEventArgs e)
{
_client = new XmppClientConnection();
var mainWindow = new Window1(client);
mainWindow.Show();
}
public XmppClientConnection Client = new XmppClientConnection();
以便从其他cs类中访问它。 - Ahmed Ghoneim