I have a wpf project without StartupUri. I set the MainWindow as TestWindow in app.cs. But i can't use the DoNothing method. How can i access to methods of TestWindow?
MainWindow.DoNothing(); //No such method. Get error when try to build
Project:
TestWindow.cs
public partial class TestWindow : BaseWindow
{
...
public void DoNothing()
{
return;
}
...
}
app.xaml
<Application x:Class="Test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Test"/>
app.cs
...
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow = new TestWindow();
}
...
Cast MainWindow to your type:
(MainWindow as TestWindow)?.DoNothing();
Or store a reference to the TestWindow in a variable of your own and access the window directly using this one:
public partial class App : Application
{
private readonly TestWindow testWindow = new TestWindow();
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow = testWindow;
...
testWindow.BeginInit();
}
}
Related
I created a page and added a button to this page.
And I put this page in a frame in the main window.
MainUi.Content = new Page1();
I want to start a thread in mainwindow when I click the button.
In MainWindow
namespace WpfApp3
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public void SendMessage()
{
MessageBox.Show("This is a test massage.");
//And more
}
}
In Page
namespace WpfApp3.Pages
{
public partial class Page1: Page
{
public Page1()
{
InitializeComponent();
}
private void SendMessage_Click(object sender, RoutedEventArgs e)
{
SendMessage();
//I want run this thread from here
}
}
}
Thanks.
You could inject the Page with a reference to the window as suggested by #Nawed Nabi Zada, or you could get a reference to the parent window of the page using the static Window.GetWindow method:
private void SendMessage_Click(object sender, RoutedEventArgs e)
{
MainWindow win = Window.GetWindow(this) as MainWindow;
win.SendMessage();
}
I would like to use a BaseWindow for other windows in WPF, but I'm getting errors that I don't know how to fix.
I created a base class:
namespace Evolutio.FluXus.WPFApp
{
public partial class WindowBase : Window
{
}
}
And changed my LoginView.xaml.cs:
public partial class LoginView : WindowBase, ITransientDependency
{
private readonly IUserAppService _userAppService;
public LoginView(IUserAppService userAppService)
{
_userAppService = userAppService;
InitializeComponent();
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
And I'm getting an error in the Close() statement.
Can anyone help me?
You also have to inherit WindowBase in the other part of of your parial LoginView class (the Xaml code):
<local:WindowBase x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Evolutio.FluXus.WPFApp"
I as the title says. I want to be able to change the GUI mainwindow from other classes. When i have all methods in the mainwindow class everything workes fine. But when i use another class in same namespace it doesn't work. My code compiles but when i click on button nothing happens.
class w_Kcal
{
MainWindow mw;
public w_Kcal(MainWindow mw)
{
this.mw = mw;
mw.maintenanceButton.Click += MaintenanceButton_Click;
}
public void MaintenanceButton_Click(object sender, RoutedEventArgs e)
{
mw.maintenanceBox.Visibility = Visibility.Visible;
mw.maintenanceOKBtn.Visibility = Visibility.Visible;
}
}
Mainwindow:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
It happens because you do not instantiate w_Kcal anywhere. Create this class in main window code and it should work:
public partial class MainWindow : Window
{
private w_Kcal helper;
public MainWindow()
{
InitializeComponent();
helper = new w_Kcal(this);
}
}
Hope this helps
I have a WPF app, in file Main.xaml.cs I have the following constructor:
public MainWindow()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
from another class:
In App.xaml.cs
I need to fire an event which will make run method MainWindow_Loaded in Main.xaml.cs
Any idea how to do it?
You can do this by manually creating the MainWindow in your App class. To do it, remove the StartUp attribute from the App.xaml so that it looks like this...
<Application x:Class="Anything.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
</Application>
In your App.xaml.cs class, override the OnStartup method like this...
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow mw = new MainWindow();
mw.Loaded += mw_Loaded;
mw.Show();
}
void mw_Loaded(object sender, RoutedEventArgs e)
{ // loaded event comes here
throw new NotImplementedException();
}
}
This override manually creates the MainWindow and shows it. It also subscribes to the Loaded event and receives the notification in the mw_Loaded method. You can also call the window's method directly because you have the window instance.
Alternatively, you can overload the MainWindow constructor and pass it an Action delegate. It would look like this...
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow mw = new MainWindow(DoSomething);
mw.Show();
}
public void DoSomething()
{
}
}
And the MainWindow would look like this...
public partial class MainWindow
{
private readonly Action _onLoaded;
public MainWindow(Action onLoaded)
{
_onLoaded = onLoaded;
InitializeComponent();
Loaded += MainWindow_Loaded;
}
void MainWindow_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
_onLoaded();
}
}
That gives you two alternatives, there are other ways also, but these are the most expedient. As Sheridan pointed out, tinkering with a window's loaded event can have confounding side effects, like re-entrancy. The WPF forefathers envisioned it as a lifetime event.
I have a button in a window that basically does a rerun through some code (retry button)
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void btnRetry_Click(object sender, RoutedEventArgs e)
{
//TODO retry function
//MainWindow.Connect();
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
I basically want to use the connect method in my main window class. However I can't do that unless it's a public static. However, if I do change Connect() to a public static, all the controls in it require to be static. I'm trying to minimize the amount of static controls I have; can anybody please help with a retry method?
Another solution is to make Window1.btnRetry visible to the parent form
public MainWindow : Window
{
private void ShowWindow1()
{
var window1 = new Window1();
window1.btnRetry.Click += OnRetryClicked;
window1.ShowDialog();
}
private void OnRetryClicked(object sender, EventArgs e)
{
// will be called when window1.btnRetry is clicked.
// retry the connection.
Connect();
}
}
First, you do need to make the method public (or internal) so that Window1 can use the method. However, do not make it static.
Instead, accept an instance of MainWindow in the Window1 constructor, and store it so that you can invoke the Connect() method on it later.
For example:
public partial class Window1 : Window
{
private MainWindow mainWindow;
public Window1(MainWindow mainWindow)
{
if (mainWindow == null) {
throw new ArgumentNullException("mainWindow");
}
this.mainWindow = mainWindow;
InitializeComponent();
}
private void btnRetry_Click(object sender, RoutedEventArgs e)
{
mainWindow.Connect();
}
// ...
}
Then, if constructing the instance from inside of MainWindow change your constructor invocation from new Window1() to new Window1(this).