首页 > 解决方案 > 如何从两个类中获取某个 UI 控件

问题描述

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

它包括:

我的场景:

  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);

        }

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

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>

我想textblockmain.xaml. Page2.xaml.cs我需要一个解决方案,我该怎么做?

标签: c#xamluwp

解决方案


对于您的情况,更好的方法是通过MessagingCenter. MessagingCenter用于在两个类之间传递参数。我已经根据您可以参考的代码创建了代码示例。

主页.Xaml.cs

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(OtherPage), 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);
    SubMessage();
}

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

    });
}

其他页面.Xaml.cs

public OtherPage()
{
    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;
    MessagingCenter.Send<OtherPage, string>(this, "Tag", Xpage2.ToString());
}

MessagingCenter您可以直接从上面的链接复制课程。更详细的可以参考MessagingCenter 官方文档


推荐阅读