c# - 如何从两个类中获取某个 UI 控件
问题描述
我正在开发一个 UWP 应用程序,在两个单独的屏幕上具有两个窗口页面。
它包括:
- 主页.xaml
- 主页.xaml.cs
- Page2.xaml
- Page2.xaml.cs
我的场景:
- 在辅助屏幕(Page2)上移动时获取鼠标位置(X)。
- 将其写入第一个屏幕(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>
我想textblock
从main.xaml
. Page2.xaml.cs
我需要一个解决方案,我该怎么做?
解决方案
对于您的情况,更好的方法是通过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
官方文档。
推荐阅读
- javascript - 子 JavaScript 类如何引用父类中的属性?
- python - 如何测试异步流(协程、任务)?
- python - PySpark 在时间戳上使用 Min 函数选择错误的值
- pandas - 如何从此数据框中删除一行?
- java - java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“boolean java.lang.String.isEmpty()”
- sql-server - 如何知道当前有多少用户连接到 SQL Server
- python-2.7 - RawPy - 确定 ARW(原始传感器)图像的过滤器阵列/图像类型是否为“Bayer”
- spring - 使用 Spring Boot 的 Web API 通知
- python - 如何更高效地打开多个页面?
- laravel - 使用 Eloquent 保存或创建新记录(如果不存在)