【问题标题】:How to share data from one view to another?如何将数据从一个视图共享到另一个视图?
【发布时间】:2016-05-16 13:11:23
【问题描述】:

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

我有第一个名为 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));
        }
    }
}

而我希望这个参数存放在主类中,怎么做呢?

【问题讨论】:

    标签: c# xaml win-universal-app views


    【解决方案1】:

    OnNavigatedTo 事件中的第2 页 上检索如下值: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));
        }
    }
    

    【讨论】:

    • 好的,但是具体如何使用它 protected override void OnNavigatedTo(NavigationEventArgs e) { string passedParameter = e.Parameter.ToString(); x = 传递参数; }
    • 几乎正确,只需用我发布的单行替换该函数的主体;)
    • 好的,但是如何在这个方法之外访问这个变量参数呢?我有 var param = e.Parameter 作为字符串; x = 参数;而且我无法在此方法之外访问 x 或 param 变量?
    • 把它赋值给一个全局变量比!私有字符串 myGlobalParam。将它放在 onNavigatedTo 函数之外的某个地方。
    • 我已经有了,但它也不起作用。也许我必须在某个地方初始化这个方法?你能看看我的代码吗?我已经更新了。
    【解决方案2】:

    最近,我正在开发 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

    希望对您有所帮助。 :)

    【讨论】:

    • 谢谢雅各布 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 2011-03-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多