Communicate between pages in wpf - c#

I have two Pages and one MainWindow.. I load the Pages in two Frames.. Now I want to execute methods from each other.. How can I do this?
This is Page1.cs:
public partial class Page1 : Page
{
public Method1()
{
doSomething;
}
}
This is Page2.cs:
public partial class Page2 : Page
{
public Method2()
{
doSomethingElse;
}
}
In my MainWindow the following happens:
Frame1.Source = new Uri("/Source/Pages/Page1.xaml", UriKind.RelativeOrAbsolute);
Frame2.Source = new Uri("/Source/Pages/Page2.xaml", UriKind.RelativeOrAbsolute);
Is there any way, to execute Method2 from Page1.cs, and Method1 from Page2.cs?
Regards

One way to do this is through their common parent, the window.
Looking at this (modified accordingly)
public partial class MainWindow : Window
{
public Page1 Page1Ref = null;
public Page1 Page2Ref = null;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Frame1.Source = new Uri("/Source/Pages/Page1.xaml", UriKind.Relative);
Frame1.ContentRendered += Frame1_ContentRendered;
// do the same for the Frame2
}
private void Frame1_ContentRendered(object sender, EventArgs e)
{
var b = Frame1.Content as Page1; // Is now Home.xaml
Page1Ref = b;
if(Page2Ref != null) // because you don't know which of the pages gets rendered first
{
Page2Ref.Page1Ref = Page1Ref; // add the Page1Ref prop to your Page2 class
Page1Ref.Page2Ref = Page2Ref; // here the same
}
}
// do the same for the other page
}
from this question
you should be able to set a reference once a page is loaded to the other page .
Better yet, you might want to let the Pages know of their window parent and access the other page through it. Either way, is bad design, I'm telling you.
Is not a solution to be proud of, you might better look into MVVM, and go with it.
Let me know if it worked for you.

Related

best practice to pass parameters between open pages

I'm developing a Windows application (UWP) that has two pages, I want the best practice to pass parameters between pages.
it's my scenario:
We have two pages, each open and remain at the middle of the screen and a Button on each page, which send the message to the other page when we click on it.
I also want to pass information continuously and repeatedly.
in Page1.cs:
Page2 page2;
public Page1()
{
this.InitializeComponent();
CreatPage2();
}
// creat page 2
private async void CreatPage2()
{
var NewWindow = CoreApplication.CreateNewView();
int NewWindowid = 0;
await NewWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
{
Frame newframe = new Frame();
newframe.Navigate(typeof(Page2), this);
Window.Current.Content = newframe;
Window.Current.Activate();
ApplicationView.GetForCurrentView().Title = "page2";
NewWindowid = ApplicationView.GetForCurrentView().Id;
});
await Windows.UI.ViewManagement.ApplicationViewSwitcher.TryShowAsStandaloneAsync(NewWindowid);
}
//Button
private void ChangeP2_Click(object sender, RoutedEventArgs e)
{
// send a message to the texblock in the page2
page2.TexBlock2.Text=$"From page1 :{e.ToString()}";
// change text color of the texblock in the page2
page2.Foreground= new SolidColorBrush(Windows.UI.Colors.Red);
}
in Page2.cs:
Page1 page1;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
page1 = e.Parameter as Page1;
base.OnNavigatedTo(e);
}
public Page2()
{
this.InitializeComponent();
}
//Button
private void ChangeP1_Click(object sender, RoutedEventArgs e)
{
// send a message to the texblock in the page1
page1.TexBlock1.Text=$"From page2 :{e.ToString()}";
// change text color of the texblock in the page1
page1.Foreground= new SolidColorBrush(Windows.UI.Colors.Red);
}
the above code just work for the page2 to the page1. (it can change the textblock of pagea).
Please help me, I can't find a solution that work on two pages
Naah… the best way is to use a standard pattern that consist of an app ViewModel class, which contains all the common app data that you want to use in the logic layer.
I always do it like this:
1) I use the MainPage automatically created as the "shell" of the app, with a property that is the AppViewModel.
The MainPage (and thus the AppViewModel) can be accessed from everywhere in the app, by setting itself as a static field in its own class.
This is the code, simpler than you think:
public sealed partial class MainPage : Page
{
public AppViewModel ViewModel { get; set; } = new AppViewModel();
public static MainPage Current { get; set; }
public MainPage()
{
this.InitializeComponent();
Current = this;
}
}
2) The AppViewModel itself is a class that must implement the INotifyPropertyChanged interface, in order to enable bindable properties and functions.
It is common, among developers, to create a base class that implements it and then derive all the classes that needs bindable properties from it.
Here it is:
public class BaseBind : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string propertyName = null) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
protected bool SetProperty<T>(ref T storage, T value,
[CallerMemberName] String propertyName = null)
{
if (object.Equals(storage, value)) return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
}
Then you derive AppViewModel class (and all the other model and viewmodel classes) from it… populating it with all the common properties that you need to share across pages.
I have even added a derived property, in order to show how you can share even multiple data types at once, and a function:
public class AppViewModel : BaseBind
{
public AppViewModel()
{
// ...
}
// All common app data
private string sampleCommonString;
public String SampleCommonString
{
get { return sampleCommonString; }
set { SetProperty(ref sampleCommonString, value); OnPropertyChanged(nameof(SampleDerivedProperty1)); OnPropertyChanged(nameof(SampleDerivedProperty2)); }
}
public String SampleDerivedProperty1 => "return something based on SampleCommonString";
public String SampleDerivedProperty2
{
get
{
<<evaluate SampleCommonString>>
return "Same thing as SampleDerivedProperty1, but more explicit";
}
}
// This is a property that you can use for functions and internal logic… but it CAN'T be binded
public String SampleNOTBindableProperty { get; set; }
public void SampleFunction()
{
// Insert code here.
// The function has to be with NO parameters, in order to work with simple {x:Bind} markup.
// If your function has to access some specific data, you can create a new bindable (or non) property, just as the ones above, and memorize the data there.
}
}
3) Then, in order to access all this from another Page, just create an AppViewModel field in that page, as seen below:
public sealed partial class SecondPage : Page
{
public AppViewModel ViewModel => MainPage.Current.ViewModel;
public SecondPage()
{
this.InitializeComponent();
}
}
...and you can easily bind XAML controls properties to the AppViewModel itself:
<TextBlock Text="{x:Bind ViewModel.SampleCommonString, Mode=OneWay}"/>
<Button Content="Sample content" Click="{x:Bind ViewModel.SampleFunction}"/>
(Mode=OneWay is for real-time binding, in order that the property is immediately updated even in the UI, while Mode=TwoWay is used for those properties that can be edited from the control itself, by the user, in order to interact with app logic).
Hope this helped.
Best regards and happy new year.

WPF: dispatcher.invoke do not running

I write my first WPF application, which consist of several pages:
Welcome page with some logo
Login page with login form
Main page with account info
MainWindow contains <Frame> WPF Control, and I use animation to show next/previous page.
I write my own MainAnimation class to perform animation.
This application works fine on my laptop, but when I try to run it on the machine of my friend animation just do nothing.
I think that trouble related with Dispatcher.Invoke() method calling, and I tried to find solution over the web (here here here and here) and I tried:
use Application.Current.Dispatcher
use Dispatcher.BeginInvoke() instead of Dispatcher.Invoke()
but it does nothing.
So, I show Welcome page only 2 seconds and Login page must loaded automatically.
This is the code of WelcomePage.xaml.cs file:
public partial class WelcomePage : Page {
public WelcomePage (MainWindow parent) {
InitializeComponent();
this.parent = parent;
Task.Factory.StartNew(() => ShowLoginForm());
}
private MainWindow parent;
private void ShowLoginForm()
{
Thread.Sleep(2000);
this.parent.GoToLoginForm();
}
}
This is the code of MainWindow.xaml.cs file:
public partial class MainWindow : Window {
public MainWindow () {
InitializeComponent();
animation = new MainAnimation(this, this, Main, new WelcomePage(this));
}
private MainAnimation animation;
public void GoToLoginForm() => animation.ShowNextPage(new LoginPage(this));
public void GoToVideosForm() => animation.ShowNextPage(new MainPage(this));
}
And this is related parts on MainAnimation class (MainAnimation.cs):
public class MainAnimation
{
public MainAnimation(FrameworkElement resourcesOwner, DispatcherObject dispatcherOwner, Frame currentPageContainer, Page firstPage)
{
this.resourcesOwner = resourcesOwner;
this.dispatcherOwner = dispatcherOwner;
this.currentPageContainer = currentPageContainer;
pages = new Stack<Page>();
pages.Push(firstPage);
currentPageContainer.Content = pages.Peek();
}
private Stack<Page> pages;
private FrameworkElement resourcesOwner;
private DispatcherObject dispatcherOwner;
private Frame currentPageContainer;
private void ShowPageForward()
{
dispatcherOwner.Dispatcher.Invoke((Action)delegate {
if (currentPageContainer.Content != null)
{
var page = currentPageContainer.Content as Page;
if (page != null)
{
page.Loaded -= NextPage_Loaded;
UnloadPageForward(page);
}
}
else
{
LoadPageForward();
}
});
}
private void UnloadPageForward(Page page)
{
Storyboard sb = (resourcesOwner.FindResource("SlideForwardOut") as Storyboard).Clone();
sb.Completed += StoryboardForward_Completed;
sb.Begin(currentPageContainer);
}
private void StoryboardForward_Completed(object sender, EventArgs e)
{
LoadPageForward();
}
private void LoadPageForward()
{
pages.Peek().Loaded += NextPage_Loaded;
currentPageContainer.Content = pages.Peek();
}
private void NextPage_Loaded(object sender, RoutedEventArgs e)
{
Storyboard sb = resourcesOwner.FindResource("SlideForwardIn") as Storyboard;
sb.Begin(currentPageContainer);
}
}
I'm new with WPF and may be just don't understand some details, so I will be happy if you help me to solve this small but very offensive issue.
Update #1: software versions
OS for development: Windows 10 x64
OS for test: Windows 8.1 x64
VS version: Visual Studio 2017 Community Edition
Application target framework: v.4.5
Since WPF controls have thread affinity it doesn't make much sense to create them on a background thread in most cases.
If you want to wait for 2 seconds before you show the login page, you could either use a DispatcherTimer or wait asynchronously:
public partial class WelcomePage : Page
{
public WelcomePage(MainWindow parent)
{
InitializeComponent();
this.parent = parent;
ShowLoginForm();
}
private MainWindow parent;
private async void ShowLoginForm()
{
await Task.Delay(2000);
this.parent.GoToLoginForm();
}
}
Then you won't need any calls to Dispatcher.Invoke.

C# How do i change a label text from another form

So i have 2 forms.
Form 1 is my main form, and form 2 is where I enter text in a textbox to be displayed on a label on form 1. Also the button to 'confirm' and actually change the entered text of my label is on form 2 which needs to stay that way.
for some reason this does not work.
Form 2 has a text-box and a button, when I press the button, it changes the string value of the designated string.
This string is linked to a label on form 1. the string is being changed so that is not the problem I confirmed this by a adding a button which pops up a message box showing the new string value.
While searching for an answer I found that is must be some sort of refreshing problem, I tried a lot of methods with no success. Only methods that did work where those who would put my button onto form 1 instead of 2.
I've been googling for 3 hours straight on how to fix this problem but either the methods don't work or they change my button from form 2 to my main form (form 1).
Please don't call me lazy I really can't find a method that works!
EDIT:
Code
GameScreen.cs
namespace _2pGame
{
public partial class GameScreen : Form
{
public GameScreen()
{
InitializeComponent();
P1NameLabel.Text = gm.P1Name;
P1ClassLabel.Text = gm.P1Class;
P2NameLabel.Text = gm.P2Name;
P2ClassLabel.Text = gm.P2Class;
}
private void PlayerInfoButton_Click(object sender, EventArgs e)
{
PlayerInfo playerinfoload = new PlayerInfo();
playerinfoload.Show();
}
}
}
PlayerInfo.cs
namespace _2pGame
{
public partial class PlayerInfo : Form
{
public PlayerInfo()
{
InitializeComponent();
}
public void ConfirmPlayerInfo_Click(object sender, EventArgs e)
{
gm.P1Class = P1ClassChoice.Text;
gm.P1Name = P1TextBox.Text;
gm.P2Class = P2ClassChoice.Text;
gm.P2Name = P2TextBox.Text;
}
}
}
Refs.cs
namespace _2pGame
{
public partial class gm
{
public static string
P1Class,
P2Class,
P1Name,
P2Name;
}
}
An approach to this very well know situation is through delegates....
In your PlayerInfo form declare
public partial class PlayerInfo : Form
{
// define the delegate type (a parameterless method that returns nothing)
public delegate void OnConfirmPlayer();
// declare a public variable of that delegate type
public OnConfirmPlayer PlayerConfirmed;
.....
public void ConfirmPlayerInfo_Click(object sender, EventArgs e)
{
gm.P1Class = P1ClassChoice.Text;
gm.P1Name = P1TextBox.Text;
gm.P2Class = P2ClassChoice.Text;
gm.P2Name = P2TextBox.Text;
// Check is someone is interested to be informed of this change
// If someone assign a value to the public delegate variable then
// you have to call that method to let the subscriber know
if (PlayerConfirmed != null)
PlayerConfirmed();
}
}
Then in your GameScreen form, just before showing the PlayerInfo form, set the public PlayerInfo.PlayerConfirmed to a method into the GameScreen form class
private void PlayerInfoButton_Click(object sender, EventArgs e)
{
PlayerInfo playerinfoload = new PlayerInfo();
// Subscribe to the notification from PlayerInfo instance
playerinfoload.PlayerConfirmed += PlayerHasBeenConfirmed;
playerinfoload.Show();
}
// Method that receives the notification from PlayerInfo
private void PlayerHasBeenConfirmed()
{
P1NameLabel.Text = gm.P1Name;
P1ClassLabel.Text = gm.P1Class;
P2NameLabel.Text = gm.P2Name;
P2ClassLabel.Text = gm.P2Class;
}
This approach has the advantage to avoid a coupling between the GameScreen and the PlayerInfo. No need to know inside the PlayerInfo the existance of a GameScreen form and the name of its properties. You just publish a delegate that a subscriber could register to be informed of the changes and let the subscriber acts on its own code.
You need a reference to your main form and assign the textbox values each time they need to be updated.
public partial class PlayerInfo : Form
{
private readonly GameScreen _main;
public PlayerInfo(GameScreen main)
{
_main = main;
InitializeComponent();
}
public void ConfirmPlayerInfo_Click(object sender, EventArgs e)
{
gm.P1Class = P1ClassChoice.Text;
gm.P1Name = P1TextBox.Text;
gm.P2Class = P2ClassChoice.Text;
gm.P2Name = P2TextBox.Text;
main.P1NameLabel.Text = gm.P1Name;
main.P1ClassLabel.Text = gm.P1Class;
main.P2NameLabel.Text = gm.P2Name;
main.P2ClassLabel.Text = gm.P2Class;
}
}
You also need to pass the reference when the PlayerInfo form is created
private void PlayerInfoButton_Click(object sender, EventArgs e)
{
PlayerInfo playerinfoload = new PlayerInfo(this); //pass ref to self
playerinfoload.Show();
}
Note that there are other better ways to do this, but this is the easiest that I can think of.
You can probably look at events or Mediator pattern if you want something better.

How to add a Canvas event from another class? WPF C#

I have a MainWindow class, and that is the main window of the app. I've created another class just below the MainWindow class, and I need to add an event to a Canvas that was created in MainWindow. The method I want to add as event is also in MainWindow.
public partial class MainWindow : Window
{
public void Moving(object sender, MouseEventArgs e)
{
bla bla;
}
public Canvas Getcanvas
{
get
{
return canvas;
}
}
}
public class Ka
{
public Ka()
{
MainWindow.Getcanvas.MouseMove += new MouseEventHandler(//HERE!!! I DONT KNOW WHAT GOES HERE, here should probably be MainWindow.Moving but I get error "An object reference.."
}
}
Please help me !
var mainWindowInstant = (MainWindow)App.Current.MainWindow;
mainWindowInstant.Getcanvas.MouseMove += new MouseEventHandler(...);

WPF application send parameters (MvvmLight with Page navigation)

I have a WPF application with Page navigation. I use MvvmLight toolkit. I have two pages: FirstPage (default page) and SecondPage. On FirstPage I navigate to SecondPage.
I want to pass a parameter to SecondPage.
//code on FirstPage
NavigationService.NavigateTo(new Uri("SecondPage.xaml", UriKind.Relative));
Messenger.Default.Send<string>("my mess");
//code on SecondPage
//constructor
public SecondPage()
{
Messenger.Default.Register<string>(this, GetMess);
}
private void GetMess(string obj)
{
}
When you first start it's not working. I can create an instance of SecondPage,
PageSecond page = new PageSecond();
but it is not beautiful. Help me please.
You are not following the MVVM principle with code like this in the code-behind. So following your start here is a simple (non-MVVM) way answer this:
Code on FirstPage -
Page secondPage = new SecondPage("Hello");
NavigationService.Navigate(secondPage);
Code on SecondPage
// Public parameterless constructor - needed for designer
public SecondPage()
{
InitializeComponent();
}
// Constructor with parameter
public SecondPage(string parameter)
{
InitializeComponent();
MyTextBox.Text = parameter;
}

Categories

Resources