如何将数据从一个视图共享到另一个视图?

How to share data from one view to another?

我在不同视图之间共享简单变量时遇到了一些问题。

我有第一个主视图 MainPage.xaml 和第二个主视图 Page2.xaml.

我想检查 MainPage.xaml 上的哪个单选按钮被选中,并将带有该日期的变量发送到 Page2.xaml.

主页:

namespace IDG
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {


        public string choice;

        public MainPage()
        {
            this.InitializeComponent();

        }

        private void bt_start_Click(object sender, RoutedEventArgs e)
        {

            if (rb_choice1.IsChecked == true)
            {
                choice = "choice1";

            }

            if (rb_quiz.IsChecked == true)
            {
                this.Frame.Navigate(typeof(Page2), choice);

            }

        }
    }
}    

和第2页:

namespace IDG
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class Page2 : Page
    {

        private string x;

        public Page2()
        {
            this.InitializeComponent();

        }



        protected override void OnNavigatedTo(NavigationEventArgs e)
       {
            var param = e.Parameter as string;
            x = param;
            textBlock1.Text = x;
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(MainPage));
        }
    }
}

而且我想把这个参数存入mainclass,怎么办?

On Page 2 in the OnNavigatedTo event 像这样检索值:var param = e.Parameter as string

编辑

将检索到的参数分配给 OnNavigatedTo 中的文本块。在构建页面时,x 的值为“”。

public sealed partial class Page2 : Page
{

    private string x="";

    public Page2()
    {
        this.InitializeComponent();
    }



    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        x = e.Parameter as string;
        textBlock1.Text = x;
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        this.Frame.Navigate(typeof(MainPage));
    }
}

最近,我正在处理 WPF 项目,但我们将它与 DevExpress 库一起使用,对于您的问题,使用 DevExpress 中的 Messenger 很容易解决。

我们只是在您要接收数据的地方注册信使,

public class Message {
    //... 
}
public class Recipient {
    public Recipient() {
        Messenger.Default.Register<string>(this, OnMessage1);
        Messenger.Default.Register<Message>(this, OnMessage2);
    }
    void SendMessages() {
        Messenger.Default.Send("test");
        Messenger.Default.Send(new Message());
    }
    void OnMessage1(string message) {
        //... 
    }
    void OnMessage2(Message message) {
        //... 
    }
} 

然后你可以从另一个视图发送它,

public class InheritedMessage : Message {
    //... 
}
public class Recipient {
    public Recipient() {
        //Inherited messages are not processed with this subscription 
        Messenger.Default.Register<Message>(
            recipient: this, 
            action: OnMessage);
        //Inherited messages are processed with this subscription 
        Messenger.Default.Register<Message>(
            recipient: this, 
            receiveInheritedMessagesToo: true,
            action: OnMessage);
    }
    void SendMessages() {
        Messenger.Default.Send(new Message());
        Messenger.Default.Send(new InheritedMessage());
    }
    void OnMessage(Message message) {
        //... 
    }
}

有了它,就可以在模块(或视图,但推荐使用MVVM)之间传递数据 如果您想了解更多有关DevExpress的信息,请通过https://documentation.devexpress.com/#WPF/CustomDocument17474

希望对您有所帮助。 :)