【问题标题】:How to redirect class data into xaml page?如何将类数据重定向到 xaml 页面?
【发布时间】:2019-09-24 18:28:56
【问题描述】:

我在将数据从类传输到xaml 页面时遇到问题,在我的控制台数据中显示在类中,但它没有传递到UWP 中的xaml 页面

var commonCheckValues = new CommonCheckValues();
     if (!Common.GetCommonCheckVars(commonCheckValues))
         return;
var data = new PrintableReceiptData
       {
          TerminalName = commonCheckValues.Session.TerminalName,
          TableNumber = commonCheckValues.Order.Model.TableName,
         };

Frame frame = new Frame();
      frame.Navigate(typeof(demoProject.Scenario1Basic), data);
      Window.Current.Content = frame;
      Window.Current.Activate();

因此,它将数据显示到数据变量中,但是当我尝试作为参数传递时,它不会在 Scenario1Basic 页面上接收值。

任何建议表示赞赏。

【问题讨论】:

  • OnNavigatedTo覆盖方法中的数据你拿到了吗?
  • 我没有在 OnNavigatedTo 方法中获得任何数据。
  • protected override void OnNavigatedTo(NavigationEventArgs e) { // 我这里需要数据,但我拿不到 }
  • NavigationEventArgs 参数字段中是否有数据?
  • 不,但我需要这种方法的数据??

标签: c# .net sqlite xaml uwp


【解决方案1】:

如何将类数据重定向到xaml页面?

当您使用参数导航时,您可以在目标页面OnNavigatedTo方法中获取参数,如下所示。

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    var parameter = e.Parameter;
}

您还可以使用基于发布-订阅模型创建的MessagingCenter 重定向数据

第1页->第2页

Page2 需要订阅 Page1 类。

第2页

MessagingCenter.Subscribe<Page1, string>(this, "Tag", async (s, arg) =>
{
   await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        textblock.Text = arg;
    });

});

第1页

MessagingCenter.Send<Page1, string>(this, "Tag", "string parameter");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    • 2018-11-21
    • 1970-01-01
    相关资源
    最近更新 更多