开发者

pass Application as argument in WPF

i have a solution combined with several projects. this is my App.xaml.cs

public partial class App : Application
{
     private SiteManager _siteManager = new SiteManager();

    public SiteManager SiteManager
    {
        get { return _siteManager; }
        set { _siteManager = value; }
    }
}

during my run i call another project in the same solution

SiteDll.MainWindow siteManagerDialog = new SiteDll.MainWindow();
siteManagerDialog.Show();

but i dont know how to pass all vars in App.xaml.cs to SiteDll.MainWindow siteManagerDialog.

i tried:

SiteDll.MainWin开发者_如何学Cdow siteManagerDialog = new SiteDll.MainWindow((App)Application.Current);
siteManagerDialog.Show();

and cast it in SiteDll.MainWindow constructor:

public MainWindow(object me)
{
    Application app = ((App)me);
    InitializeComponent();
}

but i get casting error... what is the correct way to do it ?


You are casting YourApplication1.App type to YourApplication2.App which are different types. That's why you get a invalid cast exception.

What you can do is, you can put your common variables in a separate assembly and reference this assembly from both of your applications.


Edit I would also suggest you to add a 'WPF User Control Library' and then reference it when you are using wpf windows/pages/user controls from other assemblies, to prevent such confusions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜