在WPF中的页面之间进行交流 [英] Communicate between pages in wpf

查看:86
本文介绍了在WPF中的页面之间进行交流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个页面和一个MainWindow.我在两个框架中加载页面.现在我要彼此执行方法.我该怎么做?

这是Page1.cs:

public partial class Page1 : Page
{
    public Method1()
    {
        doSomething;            
    }
}

这是Page2.cs:

public partial class Page2 : Page
{
    public Method2()
    {
        doSomethingElse;            
    }
}

在我的MainWindow中发生以下情况:

Frame1.Source = new Uri("/Source/Pages/Page1.xaml", UriKind.RelativeOrAbsolute);
Frame2.Source = new Uri("/Source/Pages/Page2.xaml", UriKind.RelativeOrAbsolute);

有什么方法可以执行Page1.cs中的Method2和Page2.cs中的Method1吗?

致谢

解决方案

一种方法是通过它们的公共父窗口.

查看此内容(进行相应修改)

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
}

来自此问题

将页面加载到另一页面后,您应该能够设置参考.

更好的是,您可能想让页面知道其窗口父级,并通过它访问另一个页面.无论哪种方式,都是不好的设计,我要告诉你.

这不是值得骄傲的解决方案,您最好调查一下MVVM并继续使用它. 让我知道它是否对您有用.

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.

这篇关于在WPF中的页面之间进行交流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆