Access windows forms function from WPF windw - wpf

i am trying to call a function in Form1 from WPF window and i am getting the following error
"Reference to a non-shared member requires an object reference."
also getting the same error when trying to access the Public variables in Form1 from wpf window.
is it not possiable to do it?

So according to the MSDN this error, this is a problem with trying to reference instance variables as if they are static.
If your class is Form1, you cannot access methods or variables that are not static by calling Form1.Method(). This won't work ever, not just in WPF. This is pretty basic stuff, you might want to read up more on VB. Check out Shared and Static documentation.
To access, for example, the method Show() on Form1, you must instantiate (create an instance of an object), and call the method on your object. Like this.
Dim frm As New Form1()
frm.Show()

Related

How to get all forms in a windows form application

I have a windows form application.
I want to get all of form that application has, but i just found about the function Application.OpenForms.
This function return all of forms are opening.
But I want to get all of forms in that application.
Is there any functions to get but not add to FormsColection for new Form created like this solution https://support.microsoft.com/en-us/kb/815707 ?
Thank you!
"All of the forms that an application has" typically would mean using reflection for all of the types in the assembly that inherit from the Windows.Forms.Form class.
The Application.OpenForms only tracks forms that are opened, not necessarily those that have been instantiated and not opened.
What you really need to do is track all of the forms when the form objects are instantiated. See code below:
Public Class Form1
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Globals.InstantiatedForms.Add(Me)
End Sub
End Class
Public Module Globals
Public InstantiatedForms As New List(Of Windows.Forms.Form)
End Module

Role of Parameterless constructor in WPF (XAML)

I've been reading an online tutorial on WPF, there I read a line
"All classes in WPF have parameterless constructors and make excessive usage of properties. That is done to make it perfectly fit for XML languages like XAML."
I examined above words by creating a custom class with one parameterized constructor and encountered error "Type 'custom_class_name' is not usable as an object element because it is not public or does not define a public parameterless constructor or a type converter."
I just wanted to know a specific detailed reason, how parameterless constructors help achieving this.
The WPF Framework uses the parameter-less constructors to instantiate all of the objects that we define in our XAML pages when it builds the visual tree. If it tries to instantiate an object that does not have a public parameter-less constructor, then you will throw this Exception. If you were to add a parameter-less constructor to your object and try again, then this Exception should disappear.
Please also look at the Type '{0}' is not usable as an object element page at MSDN.
Also, I believe that classes without any constructors in .NET are automatically provided with 'invisible' parameter-less constructors by default. However, if we add a parameterised constructor, then no parameter-less constructor will be provided automatically.

Detecting Design Mode using WPF in a Static Method

I am using WPF. I have a static class that performs some setup not available during design mode. This constructor gets called by a window in design mode, which results in an exception being thrown.
How do I detect design mode in a static method, so I can invoke the appropriate design mode behavior?
The recommended approach does not work for static methods.
Edit:
The static constructor is called from xaml, so I can't conditionally call it (unless I move the call to code-behind, which I'd like to avoid).
In the window: <Window ... HelpProvider.Keyword="some_help_topic.html">
In the class:
static HelpProvider()
{
// Load the .chm file from an application setting (this fails at design time)
// Add a WPF command binding
}
The possible way to solve it keeping attached property in xaml file is:
Move initialization code from static constructor to attached property changed callback. Frankly speaking, it is not good practice to do such kind of work in static constructors.
In your attached property changed callback you have a reference to your window. So you can call DesignerProperties.GetIsInDesignMode(yourwindow) there and decide, if you need to load file or whatever causes issues.

Access MainWIndow Control from a class in a separate file

I add a TextBlock to the MainWindow in XAML. And I would need to change the TextBlock Text in a separate class resided in a separate .cs file. I tried the following:
private static fooNameSpace.MainWindow tW1;
tW1 = this;
tW1.textBlock1.Text = "This is a paragraph";
It worked if the class is reside in the same file as the MainWindow class, But it throws me an null exception if the class is reside in a separate file. I have already added the using fooNameSpace; Still doesn't work
I can't figure out the right way to make a reference from a separate file class to the MainWindow and it's Control. Tips anyone?
thanks,
To answer my question - use internal instead of public.
// in MainWindow.xaml.cs internal
internal static fooNameSpace.MainWindow tW1;
// in foo.cs
MainWindow.tW1.txtBlock1.Text = "This is a paragraph";
the internal keyword allows other class in other cs file to get access to MainWindow controls.
But I'm not so sure about using internal to solve this problem as it allow my other class to get access to everything else in my MainWindow...any better option out there?
You mentioned XAML, so I will assume you are talking about a WPF application. the .xaml and .xaml.cs files go hand in hand. If you need to access anything in that "control" you will need to instantiate it or need its reference in the outside class.
As for the error, you declare the tw1 but it is not instantiated - which is the reason you are getting a Null exception error. Doing tw1 = this is also won't work.

Adding dynamic controls to Silverlight application after WCF Service Asynchronous Callback

I'm trying to add some dynamic controls to my Silverlight page after a WCF call. When I try to add a control to I get an error: Object reference not set to an instance of an object.
Here is a simplified version of my code:
using edm = SilverlightBusinessApplication.ServiceRefrence;
public partial class ListWCF : Page
{
edm.ServiceClient EdmClient = new ServiceClient();
public ListWCF()
{
EdmClient.GetTestCompleted += EdmGetTestCompleted;
EdmClient.GetTestAsync();
}
private void EdmGetTestCompleted(object sender, edm.GetTestCompletedEventArgs e)
{
//This is where I want to add my controls
Button b = new Button();
LayoutRoot.Children.Add(b); //Error: Object reference not set to an instance of an object
}
}
Is it not possible to modify the page after it has been loaded? What am I missing?
Thanks
Yes, it is possible to modify the page after it has been loaded.
The first thing you should do when you meet this kind of exception is to determine which of your variables are null. You should be able to do this via the debugger. Stick a breakpoint on this line of code (or tell VS to break when exceptions are thrown) and inspect the variables. My guess is that LayoutRoot is null.
I cannot see a call to InitializeComponent() in your class constructor. Within a Silverlight user control, this call will invoke the generated class that constructs your XAML and also locates the named elements (x:Name), allowing you to access them from your code.

Resources