I am going through some WPF example I found.
I have a class here which is inherited from Application:
public partial class DataBindingLabApp : Application
{
private ObservableCollection<AuctionItem> auctionItems = new ObservableCollection<AuctionItem>();
public ObservableCollection<AuctionItem> AuctionItems
{
get { return this.auctionItems; }
set { this.auctionItems = value; }
}
}
As you can see this class have a property called AuctionItems.
Because it inherits from Application it also contains property called 'Current' which provides access to the Application instance (according to MSDN).
Then in the code I have:
((DataBindingLabApp)Application.Current).AuctionItems.Add(item);
I do not understand it.
Since we can have many classes which may inherit from Application then how we know that Application.Current actually contains object of class 'DataBindingLabApp'?
Thank you!
Because Visual Studio generates entry point in the partial generated class of custom application type(DataBindingLabApp in your case) by default (You can find it by searching in the root directory of solution).
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static void Main() {
DataBindingLabApp app = new DataBindingLabApp();
app.InitializeComponent();
app.Run();
}
And after application has been ran Application.Current contains instanse of DataBindingLabApp.
Since we can have many classes which may inherit from Application
That isn't relevant. What matters is that there is only ever one instance of the Application class. The one-and-only application that's running. Be sure to distinguish types from objects.
Related
Like what I said in the title, how can we access the property in App in a different project? I want to access it from normal class like a service. Not in viewmodel. Hope we can do something like Application as App.
Access properties in App.xaml.cs from different project UWP
You may try to use one App.xaml.cs for the two projects. For example, if the second project wants to access App.xaml.cs in the first project without reference the first project, you may consider remove the App.xaml.cs which is belonged to the second project, and Add-ExitingItem to add the App.xaml.cs from the first project. In that case, the two projects will share the same App.xam.cs and then you can directly access the properties as Marian Dolinský mentioned.
Otherwise, the two projects may not be able to communicate with each other directly. If the above method is not suit for you, please detail why you need this feature and we may need to consider other ways without accessing the App.xaml.cs.
Method 1
You could cast Application.Current to App:
App app = (App)Application.Current;
app.YourProperty = something;
Method 2
Create some static property holding the reference of App. In my projects I do it by creating a new property called Current as follows:
// in App.xaml.cs
public static new App Current { get; private set; }
public App()
{
Current = this;
// Another code
}
I'm trying to establish the feasibility of using our company's product with COM. To do this, I've written a (MyAppCom) dll within the solution and referenced the original (MyApp)exe project. MyAppCom creates and instance of the MyApp (mainform).
The main problem I am coming up against is that the application entry point is an application type (with settings, startup forms, etc.). Generally, this isn't so bad, but there are a couple issues that I'm banging my head against.
The app config uses the calling exes name to search for the application configuration file (i.e. if I'm calling a com instance of MyApp via Python, its looking for python.exe.config instead of MyApp.exe.config. I've sidestepped this issue for the moment by just copying the settings file, but if anybody knows how to reference this specific config settings, any help would be appreciated.
The second point is a bit more sticky. It seems that MyApp has set mainform as its startup form, which seems to create a global instance of it. I've looked everywhere and there's no explicit initializations of mainform except through MyAppCom. This becomes a problem when I initialize mainform from MyAppCom and child forms are trying to reference the global MyApp.mainform. In this case, it is obviously not initialized (since I didn't create it from MyApp).
Is this something other people have done? I've searched for a couple days now with no luck but I don't imagine I'm one of few who've tried. A little background on the product - it was originally written in VB6 and now is updated to VB.NET with new projects in C#. The wrapper is also written in C#.
Here is the com wrapper (MyAppCom) (names have been changed to protect the not-so-innocent):
public interface IMainCom
{
void Init();
}
[ClassInterface(ClassInterfaceType.None)]
public class MainCom : IMainCom
{
private MDIMain mMDIMain = null;
public void Init()
{
OpenMain();
}
private void OpenMain()
{
mMDIMain = new MDIMain();
mMDIMain.Show();
}
}
Here also is the App.Designer for MyApp:
Namespace My
Partial Friend Class MyApplication
<Global.System.Diagnostics.DebuggerStepThroughAttribute()> _
Public Sub New()
MyBase.New(Global.Microsoft.VisualBasic.ApplicationServices.AuthenticationMode.Windows)
Me.IsSingleInstance = false
Me.EnableVisualStyles = false
Me.SaveMySettingsOnExit = true
Me.ShutDownStyle = Global.Microsoft.VisualBasic.ApplicationServices.ShutdownMode.AfterMainFormCloses
End Sub
<Global.System.Diagnostics.DebuggerStepThroughAttribute()> _
Protected Overrides Sub OnCreateMainForm()
Me.MainForm = Global.MyApp.MDIMain
End Sub
End Class
End Namespace
VS2012 for desktop .net framework 4.5 normal windows forms applications, not WPF
Hello, I tried to search for an answer, but I'm not sure of the correct terminology. I've managed to break my code, and can't understand what I've done wrong. (i didn't think i had changed anything, but ...)
I have a solution which contains 2 projects. The first project is an executable program, and the second is a DLL, which is loaded at run time and used by the first project.
the first project contains a form, and a static class with public static strings in the same namespace. (and some other unconnected classes). specifically:
namespace project1_namespace
{
static class settings
{
public static string some_words = "some words in a string";
}
class dll_callback{
//.. some public methods here
}
dll_callback dllcallback; // instance is initialised in the code (not shown)
Form form;
public partial class frm_splash : Form
{
private void frm_splash_FormClosing(object sender, FormClosingEventArgs e)
{
// this function actually loads the DLL, ensuring its the last step
//... some error checking code removed for brevity
Assembly assembly = Assembly.LoadFrom("c:\dllpath\project2.dll");
Type type_init = assembly.GetType("project2_class");
object init = Activator.CreateInstance(type_init, form, dllcallback);
//... some error checking code removed for brevity
}// end method
}// end form class
}// end namespace
when the form is closing, the method shown above is called which calls the second projects class project2_class constructor.
in project 2, the DLL, there is:
namespace project2_namespace
{
// how did i get this working to reference "settings" class from project 1??
public class project2_class
{
public project2_class(project2_namespace.Form1 form_ref, object callback)
{
settings.some_words = "the words have changed";
//... some more stuff
}
}
}
Now, i was experimenting with some code in an entirely different part of project2, and VS2012 suddenly started refusing to compile stating:
error CS0103: The name 'settings' does not exist in the current context
the standard solution to this appears to be to add a reference to project2, but that would create circular dependencies because project 1 calls 2 as a DLL.
I really honestly don't think i had changed anything relevant to this, but also clearly I have.
looking at it, i cant see how project 2 would have access to a class in project 1 without a reference, but the list of arguments to the project2_class constructor doesn't include one, and I am absolutely positive that it hasn't changed (and I cant change it for backwards compatibility reasons).
would really appreciate help with this, as its been a lot of work to get this working.
as a side note, I've definitely learned my lesson about not using source control. and not making "how this works" comments instead of "what this does" comments.
may dynamic help you? You can not get the setting string at complie time.
I am not a seasoned pro at C# and am running into an error I cannot find a discrete solution to. Currently I have one class that is part of my namespace
namespace BlackBoxStudio
{
public partial class Project : Form
{
This Project class is what implements the various functions of my form. What I am trying to do is make a label.text field accessable to another .cs class within the same namespace. As so, I have made the corresponding fields public:
public string lb_InformationText
{
get { return lb_Information.Text; }
set { lb_Information.Text = value; }
}
To call these I get or set methods in my RsaFunctions class I make a new private project as so:
namespace BlackBoxStudio
{
class RsaFunctions
{
//private Project proj = new Project();
However the line that is commented our causes my vshost32.exe application to fail and the program dies. Why is this causing the vshost32.exe to fail when I un-comment the corresponding line? Or how could I better structure my classes so this does not happen? Any tips would be much appreciated about my strategy for making new classes and such.
My current solution has 3 project with 2 app.config (one for common settings and another for service settings). As of now I'm simply creating static classes to act as a mediator to access values. I do this so I don't have to write ConfigurationManager.AppSettings["SomeKey"] everywhere. This works fine until you want to access an app.config file from a different project.
Here is what I'm currently doing (all properties omitted for brevity).
public class ServiceConfiguration
{
public static readonly string SyncEvery = ConfigurationManager.AppSettings["SyncEveryMinutes"];
}
How can I access an app.config file located in another project? I thought perhaps setting VS to copy the file to the output directory would do the trick however my configuration object is still null.
I can't imaging many good reasons to read another app's configuration in the first place, it just opens a can of worms that isn't worth dealing with.
Expose a class that exposes the project's configured values as properties, and access them from a consuming class.
public class FirstProjectClass
{
public static int SyncEveryMinutes
{
get { return (int)ConfigurationManager.AppSetting["SyncEveryMinutes"] };
}
}
public class SecondProjectClass
{
public void ShowConfigedValue()
{
Console.Writeline("Syncing every {0} minutes", FirstProjectClass.SyncEveryMinutes);
}
}
if you've got complex configuration requirements you can also look into custom configuration sections
ConfigurationManager.OpenExeConfiguration can be helpfull:
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.openexeconfiguration.aspx
Also: what Jason said - it is usually a bad idea.