从其他 Class 以编程方式访问 UWP 子元素

Accessing UWP Child Element Programmatically from Other Class

我有一个名为 MyPage.xaml 的 UWP XAML,如下所示

<Page
    x:Class="AffiliaTool.Lib.View.BrowserWindowView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:core="using:Microsoft.Xaml.Interactions.Core"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:i="using:Microsoft.Xaml.Interactivity"
    xmlns:local="using:AffiliaTool.Lib.View"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:tk="using:Microsoft.Toolkit.Uwp.UI.Controls"
    xmlns:vm="using:AffiliaTool.Lib.ViewModel"
    mc:Ignorable="d">
    <tk:TabView x:Name="TabViewBar" />
</Page>

并且从 其他 class,我想实例化 class 并像下面那样以编程方式操作 TabView

var mypage = new MyPage();
mypage.TabViewBar.Visibility = Windows.UI.Xaml.Visibility.Collapsed;

我曾经能够在编写 WPF 代码时从 other class 以编程方式访问 子元素 ,但为什么不能'我不是在 UWP 中而是在代码隐藏中这样做吗?无论如何要解决这个问题?

谢谢

可以。只需像这样添加 FieldModifier

<tk:TabView x:Name="TabViewBar" x:FieldModifier="public"/>

然后您可以从另一个 class 访问 TabViewBar。

希望对您有所帮助。