开发者

Error Application cast in WPF

开发者 https://www.devze.com 2023-03-16 08:13 出处:网络
i have 2 projects in my solution (main is A.WPF and secondary is B.WPF) when i\'m trying to access variables inside my App.xaml.cs in B.WPF:

i have 2 projects in my solution (main is A.WPF and secondary is B.WPF)

when i'm trying to access variables inside my App.xaml.cs in B.WPF:

filename = ((App)Application.Current).ErrorLogFileName;

i get the following error:开发者_如何学Python

Unable to cast object of type 'A.App' to type 'B.App'.

i also tried the following:

filename = ((B.App)Application.Current).ErrorLogFileName;

but still the same error...

the definition in B.App is:

private string _errorLogFileName = "error log.xml";

public string ErrorLogFileName
{
    get { return _errorLogFileName; }
}

please assist...


Looks like you need to do:

filename = ((A.App)Application.Current).ErrorLogFileName;

The error is saying the type is A.App, yet in both cases you are trying to cast to B.App.

There can only be one current application also.


Application.Current refers to the current application. The only way to be allowed to cast the current App to another App-type is when the other App-type is a base class of the current App-type.

Are A.App and B.App siblings or is B.App a base class of A.App?


If you don't want B to have a reference to A (or can't as you want A to reference B and that would cause a circular reference), then you need a common type defined in a third assembly that both A and B reference. In our implementation we tend to have a ConfigurationData type that is in a separate project referenced by both Wpf projects, e.g.

public static class ConfigurationData
{

    private static string _errorLogFileName = "error log.xml";

    public string ErrorLogFileName
    {
        get { return _errorLogFileName; }
    }

}

Another approach would be to define an Interface for your ErrorLogFileName property in a 3rd assembly that both A and B reference, and then implement that interface on your Wpf Application class - A and B would then both be able to cast to that type. If you wanted your A project to set the values on that at runtime, you could make the ErrorLogFileName a read-write property instead and initialize it in your application startup.

I personally prefer using a separate ConfigurationData type from the Wpf app object for this kind of stuff (ErrorLogFileName etc.) as it can then also be used for code that might execute in a unit test and therefore might not be running under a Wpf application - it also avoids having to do casts all over the place (ConfigurationData.ErrorLogFileName instead of ((IAppConfigurationData)Application.Current).ErrorLogFileName.

BTW, if you have an Application object in both assemblies it sounds like you might have both assemblies configured to build as Output type: Windows Application in your project properties. You should only really have one assembly that is configured as the Windows Application and the rest should be Class Library to avoid confusing numbers of Application classes being generated - only the one in the main EXE (and it's related resources) will get created at runtime.

0

精彩评论

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

关注公众号