如何从两个类中获取某个 UI 控件 [英] How to get a certain UI control from two classes

查看:26
本文介绍了如何从两个类中获取某个 UI 控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 UWP 应用程序,在两个单独的屏幕上有两个窗口页面.

I'm developing a UWP application with two window pages on two separate screens.

包括:

  • 主页.xaml
  • Mainpage.xaml.cs
  • Page2.xaml
  • Page2.xaml.cs

我的场景:

  1. 在辅助屏幕 (Page2) 上移动时获取鼠标位置 (X).
  2. 将其写在第一屏(MainPage)的文本块中,同时更改.

在 MainPage.xaml

<Page
    x:Class="LineDraw.MainPage"
    xmlns:local="using:LineDraw"
    ...
    ..

    <Grid>

    <TextBlock Name="textblock" Text="N/A" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="100" />

    </Grid>
</Page>

在 MainPage.xaml.cs

 public sealed partial class MainPage : Page
    {   
        public double Xpage2;

        public MainPage()
        {
            this.InitializeComponent();
            newpage();   
        }

        private async void newpage()
        { 
            int NewWindowid = 0;
            int Windowid = ApplicationView.GetForCurrentView().Id;
            CoreApplicationView newView = CoreApplication.CreateNewView();
            await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                Frame newframe = new Frame();
                newframe.Navigate(typeof(Page2), this); // this means existing MainPage object.
                Window.Current.Content = newframe;
                Window.Current.Activate();
                NewWindowid = ApplicationView.GetForCurrentView().Id;
            });

             bool available = ProjectionManager.ProjectionDisplayAvailable;

             ProjectionManager.ProjectionDisplayAvailableChanged += (s, e) =>
             {
               available = ProjectionManager.ProjectionDisplayAvailable;
             };

            await ProjectionManager.StartProjectingAsync(NewWindowid, Windowid);
        }

        public void copyX(double Xpage2)
        {
            textblock.Text = $"X = {Xpage2}";  
        }
}

在 Page2.xaml.cs 中

 public sealed partial class Page2 : Page
    {      
        MainPage mainpage;
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            mainpage = e.Parameter as MainPage;
            base.OnNavigatedTo(e);
        }

        public Page2()
        {
            this.InitializeComponent();    
            Window.Current.CoreWindow.PointerMoved += CoreWindow_PointerMoved;    
        }

        public void CoreWindow_PointerMoved(CoreWindow sender, PointerEventArgs args)
        {
            Point ptr = args.CurrentPoint.Position;
            double Xpage2 = ptr.X;
            mainpage.copyX(Xpage2);

        }

我做了上面的代码,但结果是以下错误:

I did the code above but the result is the following error:

System.Exception
  HResult=0x8001010E
  Message=The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>

我想从 Page2.xaml.cs 获取 main.xaml 中的 textblock(UI 控件).我需要一个解决方案,我该怎么做?

I want to get the textblock(UI control) in main.xaml from Page2.xaml.cs. I need a solution , How can I do that?

推荐答案

您正在尝试运行已在另一个上下文中运行的方法 (mainpage.copyX()).如果您可以在 CoreApplication 的 MainView 上运行此代码,我不知道您为什么创建了另一个视图.在 MainPage.xmal.cs 中:

You're trying to run a method (mainpage.copyX()) that has been running in another context. I don't know why you created another view, if you can just run this code on the MainView of CoreApplication. In MainPage.xmal.cs:

await CoreApplication.MainView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
     Frame newframe = new Frame();
     newframe.Navigate(typeof(Page2), this);
     Window.Current.Content = newframe;
     Window.Current.Activate();
     NewWindowid = ApplicationView.GetForCurrentView().Id;
});

await CoreApplication.MainView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
     Point ptr = args.CurrentPoint.Position;
     double Xpage2 = ptr.X;
     mainpage.copyX(Xpage2);
});

如果你创建另一个视图,你需要以某种方式共享这个资源,以便每个人都在同一个上下文中执行.

If you create another view, you need somehow share this resource in order for everyone execute on the same context.

这篇关于如何从两个类中获取某个 UI 控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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